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

com.aliyuncs.utils.FlattenMapUtil Maven / Gradle / Ivy

Go to download

Aliyun Open API SDK for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

The newest version!
package com.aliyuncs.utils;

import java.util.*;

public class FlattenMapUtil {
    public static List> toListMap(Map flattenMap, String prefix) {
        MapUtils mapUtils = new MapUtils();
        return mapUtils.convertMapToListMap(flattenMap, prefix);
    }

    public static Map toMap(Map flattenMap, String prefix) {
        MapUtils mapUtils = new MapUtils();
        return mapUtils.convertMapToMap(flattenMap, prefix);
    }

    public static Object put(Map flattenMap, Object object, String[] subKeys, int subKeysIndex) {
        if (subKeysIndex >= subKeys.length) {
            return object;
        }
        String key = subKeys[subKeysIndex];

        if (key.endsWith("]")) {
            int index = parseIndex(key);
            if (index == -1) {
                return null;
            }

            ArrayList arrayList;

            if (object == null) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < subKeysIndex; i++) {
                    sb.append(subKeys[i]).append(".");
                }
                sb.append(key);
                int length = parseLength(flattenMap, sb.toString());
                if (length == -1) {
                    return null;
                }
                arrayList = new ArrayList(Collections.nCopies(length, null));
            } else {
                arrayList = (ArrayList) object;
            }

            if (subKeys.length == subKeysIndex + 1) {
                arrayList.set(index, flattenMap.get(stringJoin(".", subKeys)));
                return arrayList;
            } else {
                arrayList.set(index, put(flattenMap, arrayList.get(index), subKeys, subKeysIndex + 1));
                return arrayList;
            }

        } else {
            HashMap hashMap;
            if (object == null) {
                hashMap = new HashMap();
            } else {
                hashMap = (HashMap) object;
            }
            if (subKeys.length == subKeysIndex + 1) {
                hashMap.put(key, flattenMap.get(stringJoin(".", subKeys)));
                return hashMap;
            } else {
                hashMap.put(key, put(flattenMap, hashMap.get(key), subKeys, subKeysIndex + 1));
                return hashMap;
            }
        }
    }

    public static Object putForMap(Map flattenMap, Object object, String[] subKeys, int subKeysIndex) {
        if (subKeysIndex >= subKeys.length) {
            return object;
        }
        String key = subKeys[subKeysIndex];

        if (key.endsWith("]")) {
            int index = parseIndex(key);
            if (index == -1) {
                return null;
            }

            if (object != null && !(object instanceof HashMap)) {
                return null;
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < subKeysIndex; i++) {
                sb.append(subKeys[i]).append(".");
            }
            sb.append(key);
            int length = parseLength(flattenMap, sb.toString());
            if (length == -1) {
                return null;
            }

            String name = parseArrayName(key);

            ArrayList arrayList;
            HashMap hashMap;
            if (object != null) {
                hashMap = (HashMap) object;

                if (!(hashMap.get(name) instanceof ArrayList)) {
                    arrayList = new ArrayList(Collections.nCopies(length, null));
                    hashMap.put(name, arrayList);
                } else {
                    arrayList = (ArrayList) hashMap.get(name);
                }
            } else {
                hashMap = new HashMap();
                arrayList = new ArrayList(Collections.nCopies(length, null));
                hashMap.put(name, arrayList);
                object = hashMap;
            }

            if (subKeys.length == subKeysIndex + 1) {
                arrayList.set(index, flattenMap.get(stringJoin(".", subKeys)));
                return object;
            } else {
                arrayList.set(index, putForMap(flattenMap, arrayList.get(index), subKeys, subKeysIndex + 1));
                return object;
            }

        } else {
            HashMap hashMap;
            if (object == null) {
                hashMap = new HashMap();
            } else {
                hashMap = (HashMap) object;
            }
            if (subKeys.length == subKeysIndex + 1) {
                hashMap.put(key, flattenMap.get(stringJoin(".", subKeys)));
                return hashMap;
            } else {
                hashMap.put(key, putForMap(flattenMap, hashMap.get(key), subKeys, subKeysIndex + 1));
                return hashMap;
            }
        }
    }

    public static int parseIndex(String key) {
        int start = key.indexOf("[");
        int end = key.indexOf("]");
        if (start == -1 || end == -1 || end <= start) {
            return -1;
        }

        try {
            return Integer.parseInt(key.substring(start + 1, end));
        } catch (Exception e) {
            return -1;
        }
    }

    public static int parseLength(Map flattenMap, String key) {
        int end = key.lastIndexOf("[");
        if (end == -1) {
            return -1;
        }

        try {
            return Integer.parseInt(flattenMap.get(key.substring(0, end) + ".Length"));
        } catch (Exception e) {
            return -1;
        }
    }

    public static String parseArrayName(String key) {
        if (key == null) {
            return null;
        }
        int end = key.lastIndexOf("[");
        if (end == -1) {
            return null;
        }
        return key.substring(0, end);
    }

    public static String stringJoin(String delimiter, String... sequences) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < sequences.length; i++) {
            sb.append(sequences[i]);
            if (i < sequences.length - 1) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }
}