package com.akamai.csi.multireducers;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * MapToProperties
 */
public class MapToProperties {

    private static String utf8 = Charsets.UTF_8.name();

    static Map<String, String> deserialize(String s) {
        Map<String, String> m = Maps.newHashMap();
        String[] keyvals = s.split(";");
        for (String keyval : keyvals) {
            String[] parts = keyval.split("=");
            try {
                m.put(URLDecoder.decode(parts[0], utf8), URLDecoder.decode(parts[1], utf8));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return m;
    }

    static String serialize(Map<String, String> m) {
        List<String> parts = Lists.newArrayList();
        for (Map.Entry<String, String> entry : m.entrySet()) {
            try {
                String key = URLEncoder.encode(entry.getKey(), utf8);
                String value = URLEncoder.encode(entry.getValue(), utf8);
                parts.add(key + "=" + value);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return Joiner.on(";").join(parts);
    }

}

@@ -451,16 +451,112 @@
 .name();
+%0A    private static String dummy = %22com.akamai.csi.multireducers.mulitoutputformat.placeholder%22;
 %0A%0A    st
@@ -780,24 +780,174 @@
 split(%22=%22);%0A
+            String key = parts%5B0%5D;%0A            String value = %22%22;%0A            if (parts.length %3E 1) %7B%0A                value = parts%5B1%5D;%0A            %7D%0A
             
@@ -992,24 +992,19 @@
 .decode(
-parts%5B0%5D
+key
 , utf8),
@@ -1022,24 +1022,21 @@
 .decode(
-parts%5B1%5D
+value
 , utf8))
@@ -1155,32 +1155,57 @@
     %7D%0A        %7D%0A
+        m.remove(dummy);%0A
         return m
@@ -1313,24 +1313,118 @@
 rrayList();%0A
+        // This is to ensure we never return the empty string%0A        parts.add(dummy + %22=%22);%0A
         for 

add dummy value, so that every map will contain something