Java Map Merge mergeValue(Map obj, String key, Object value)

Here you can find the source of mergeValue(Map obj, String key, Object value)

Description

merge Value

License

Open Source License

Declaration

static void mergeValue(Map<String, Object> obj, String key, Object value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    static void mergeValue(Map<String, Object> obj, String key, Object value) {
        if (obj == null) {
            return;
        }// w ww  . j  a v a2  s . c om
        List<Object> values = (List<Object>) obj.get(key);
        if (values == null) {
            values = new ArrayList<Object>();
            obj.put(key, values);
        }
        if ("@list".equals(key) || (value instanceof Map && ((Map<String, Object>) value).containsKey("@list"))
                || !deepContains(values, value)) {
            values.add(value);
        }
    }

    public static boolean deepContains(List<Object> values, Object value) {
        for (final Object item : values) {
            if (deepCompare(item, value, false)) {
                return true;
            }
        }
        return false;
    }

    public static Boolean deepCompare(Object v1, Object v2, Boolean listOrderMatters) {
        if (v1 == null) {
            return v2 == null;
        } else if (v2 == null) {
            return v1 == null;
        } else if (v1 instanceof Map && v2 instanceof Map) {
            final Map<String, Object> m1 = (Map<String, Object>) v1;
            final Map<String, Object> m2 = (Map<String, Object>) v2;
            if (m1.size() != m2.size()) {
                return false;
            }
            for (final String key : m1.keySet()) {
                if (!m2.containsKey(key) || !deepCompare(m1.get(key), m2.get(key), listOrderMatters)) {
                    return false;
                }
            }
            return true;
        } else if (v1 instanceof List && v2 instanceof List) {
            final List<Object> l1 = (List<Object>) v1;
            final List<Object> l2 = (List<Object>) v2;
            if (l1.size() != l2.size()) {

                return false;
            }
            // used to mark members of l2 that we have already matched to avoid
            // matching the same item twice for lists that have duplicates
            final boolean alreadyMatched[] = new boolean[l2.size()];
            for (int i = 0; i < l1.size(); i++) {
                final Object o1 = l1.get(i);
                Boolean gotmatch = false;
                if (listOrderMatters) {
                    gotmatch = deepCompare(o1, l2.get(i), listOrderMatters);
                } else {
                    for (int j = 0; j < l2.size(); j++) {
                        if (!alreadyMatched[j] && deepCompare(o1, l2.get(j), listOrderMatters)) {
                            alreadyMatched[j] = true;
                            gotmatch = true;
                            break;
                        }
                    }
                }
                if (!gotmatch) {
                    return false;
                }
            }
            return true;
        } else {
            return v1.equals(v2);
        }
    }

    public static Boolean deepCompare(Object v1, Object v2) {
        return deepCompare(v1, v2, false);
    }
}

Related

  1. mergeOptions(Map> ssio, Map> allOptions)
  2. mergePermMap(Map> permMap, Map> subPermMap)
  3. mergePropertiesToMap(Properties properties, Map map, boolean overwrite)
  4. mergeResourceBundle(final Map map, final String path)
  5. mergeTemplate(String template, Map segments)
  6. mergeValueInMap(Map map, String name, String value)