All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyuncs.v5.utils.MapUtils Maven / Gradle / Ivy

package com.aliyuncs.v5.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtils {

    public List> convertMapToListMap(Map flattenMap, String prefix) {
        List> list = new ArrayList>();
        for (Map.Entry entry : flattenMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key.contains(prefix)) {
                String[] keys = key.replace(prefix, "").split("\\.");
                String pre = prefix + keys[0];
                if (keys[0].contains("[")) {
                    String mapKey = key.replace(pre + ".", "");
                    int index = Integer.parseInt(keys[0].replace("[", "").replace("]", ""));
                    list = setList(list, index, mapKey, value);
                }
            }
        }
        return list;
    }

    public Map convertMapToMap(Map flattenMap, String prefix) {
        Map map = new HashMap();
        for (Map.Entry entry : flattenMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key.contains(prefix)) {
                String[] keys = key.replace(prefix, "").split("\\.");
                String pre = prefix + keys[0];
                String mapKey = key.replace(pre + ".", "");
                map = setMap(map, mapKey, value);
            }
        }
        return map;
    }

    public static String getMapString(Map map) {
        if (map == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder("{");
        for (Map.Entry entry : map.entrySet()) {
            sb.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
        }
        sb.append("}");
        return sb.toString();
    }

    private List> setList(List targetList, int index, String key, String value) {
        List list = targetList;
        if (null != key && key.contains("[") && !key.contains(".")) {
            if (null == list) {
                list = new ArrayList();
            }
            while (list.size() <= index) {
                list.add("");
            }
            list.set(index, value);
        } else {
            if (null == list) {
                list = new ArrayList>();
            }
            while (list.size() <= index) {
                list.add(new HashMap());
            }
            list.set(index, setMap((Map) list.get(index), key, value));
        }
        return list;
    }

    private Map setMap(Map targetMap, String key, String value) {
        Map map = targetMap;
        if (null == map) {
            map = new HashMap();
        }
        if (key.contains("[")) {
            String[] keys = key.split("\\.");
            String listKey = key.substring(0, key.indexOf("["));
            int index = Integer.parseInt(key.substring(key.indexOf("[") + 1, key.indexOf("]")));
            List listObj = (List) map.get(listKey);
            listObj = setList(listObj, index, key.replace(keys[0] + ".", ""), value);
            map.put(listKey, listObj);
        } else if (key.contains(".")) {
            String[] keys = key.split("\\.");
            String mapKey = keys[0];
            // exclude *.Length
            if (!(map.get(mapKey) instanceof List) && !"Length".equals(keys[1])) {
                map.put(mapKey, setMap((Map) map.get(mapKey),
                        key.replace(keys[0] + ".", ""), value));
            }
        } else {
            map.put(key, value);
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy