// Path: src/main/java/com/godaddy/logging/CommonKeys.java
// public class CommonKeys {
//     public static final String LOG_MESSAGE_KEY = "_message";
// 
//     public static final String UNNAMED_VALUES_KEY = "_unnamed_values";
// }
// 
// Path: src/main/java/com/godaddy/logging/LogContext.java
// public abstract class LogContext<T>{}


import com.godaddy.logging.CommonKeys;
import com.godaddy.logging.LogContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
 * Copyright (c) 2015 GoDaddy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.godaddy.logging.messagebuilders.providers;



public class StringMessageBuilderProvider extends JsonMessageBuilderProvider {

    @Override
    public Object formatPayload(final LogContext<List<Map<String, Object>>> runningLogContext) {
        return new StringMessageFormatter(getContextMap(runningLogContext)).getFormattedPayload();
    }

    private static class StringMessageFormatter {
        private final Map<String, Object> contextMap;

        private final StringBuilder messageBuilder = new StringBuilder();

        private static final String SEPARATOR = "; ";

        public StringMessageFormatter(final Map<String, Object> contextMap) {
            this.contextMap = contextMap;
        }

        public Object getFormattedPayload() {
            // Clear out the message builder.
            messageBuilder.setLength(0);

            // Leveraging TreeMap so that log data is consistently sorted.
            Map<String, Object> jsonMap = new TreeMap<>();

            jsonMap.putAll(contextMap);

            addLogMessageToFormattedString(jsonMap);

            buildFormattedContext(0, jsonMap, "");

            trimLastSeparator();

            return messageBuilder.toString();
        }

        private void trimLastSeparator() {
            final int startOfSeparatorIndex = messageBuilder.length() - SEPARATOR.length();

// Path: src/main/java/com/godaddy/logging/CommonKeys.java
// public class CommonKeys {
//     public static final String LOG_MESSAGE_KEY = "_message";
// 
//     public static final String UNNAMED_VALUES_KEY = "_unnamed_values";
// }
// 
// Path: src/main/java/com/godaddy/logging/LogContext.java
// public abstract class LogContext<T>{}


// Path: src/main/java/com/godaddy/logging/messagebuilders/providers/StringMessageBuilderProvider.java
import com.godaddy.logging.CommonKeys;
import com.godaddy.logging.LogContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Copyright (c) 2015 GoDaddy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.godaddy.logging.messagebuilders.providers;



public class StringMessageBuilderProvider extends JsonMessageBuilderProvider {

    @Override
    public Object formatPayload(final LogContext<List<Map<String, Object>>> runningLogContext) {
        return new StringMessageFormatter(getContextMap(runningLogContext)).getFormattedPayload();
    }

    private static class StringMessageFormatter {
        private final Map<String, Object> contextMap;

        private final StringBuilder messageBuilder = new StringBuilder();

        private static final String SEPARATOR = "; ";

        public StringMessageFormatter(final Map<String, Object> contextMap) {
            this.contextMap = contextMap;
        }

        public Object getFormattedPayload() {
            // Clear out the message builder.
            messageBuilder.setLength(0);

            // Leveraging TreeMap so that log data is consistently sorted.
            Map<String, Object> jsonMap = new TreeMap<>();

            jsonMap.putAll(contextMap);

            addLogMessageToFormattedString(jsonMap);

            buildFormattedContext(0, jsonMap, "");

            trimLastSeparator();

            return messageBuilder.toString();
        }

        private void trimLastSeparator() {
            final int startOfSeparatorIndex = messageBuilder.length() - SEPARATOR.length();

            if (startOfSeparatorIndex > 0) {