Java Map Merge mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)

Here you can find the source of mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)

Description

merge Maps

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static Map mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)
            throws Exception 

Method Source Code


//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    @SuppressWarnings("unchecked")
    public static Map mergeMaps(Map mainMap, Map defaultMap, String[] keys, boolean enforceValues)
            throws Exception {
        final String eLabel = "MapHelper.MergeMaps: ";
        // Create a new map to populate and return. We may not want to modify the originals.
        HashMap returnMap = new HashMap();
        // Cover the case where a null default map is passed in...that is acceptable
        if (defaultMap == null) {
            defaultMap = new HashMap();
        }/*from www .  ja v  a 2  s  .  c  o  m*/
        try {
            for (int i = 0; i < keys.length; i++) {
                if (!mainMap.containsKey(keys[i]) && !defaultMap.containsKey(keys[i])) {
                    if (enforceValues) {
                        throw new Exception("Required value does not exist for key: " + keys[i]);
                    }
                    continue;
                }
                returnMap.put(keys[i],
                        (mainMap.get(keys[i]) != null) ? mainMap.get(keys[i]) : defaultMap.get(keys[i]));
            }
            return returnMap;
        } catch (Exception e) {
            throw new Exception(eLabel + e);
        }
    }

    public static HashMap mergeMaps(Map toMergePrimary, Map toMergeSecondary, Object[] verifyKeys)
            throws Exception {
        final String eLabel = "MapHelper.mergeMaps: ";
        try {
            return (HashMap) mergeMaps(toMergePrimary, toMergeSecondary, new HashMap(), verifyKeys);
        } catch (Exception e) {
            throw new Exception(eLabel + e);
        }
    }

    @SuppressWarnings("unchecked")
    public static Map mergeMaps(Map toMergePrimary, Map toMergeSecondary, Map mergedMap, Object[] verifyKeys)
            throws Exception {
        final String eLabel = "MapHelper.mergeMaps: ";
        try {
            if (toMergePrimary == null)
                throw new Exception("toMergePrimary parameter must not be null!");
            if (mergedMap == null)
                throw new Exception("mergedMap parameter must not be null!");
            if (toMergeSecondary != null)
                mergedMap.putAll(toMergeSecondary);
            mergedMap.putAll(toMergePrimary);
            if (verifyKeys != null) {
                StringBuilder sb = new StringBuilder();
                int i = 0;
                for (Object key : verifyKeys) {
                    if (!mergedMap.containsKey(key)) {
                        if (i++ > 0)
                            sb.append(", ");
                        sb.append("Key does not exist: ").append(key);
                    }
                }
                if (sb.length() > 0)
                    throw new Exception(sb.toString());
            }
            return mergedMap;
        } catch (Exception e) {
            throw new Exception(eLabel + e);
        }
    }
}

Related

  1. mergeMap(Map primaryValues, Map defaultValues)
  2. mergeMap(Map first, Map second)
  3. mergeMapIntoMap(Map> source, Map> destination)
  4. mergeMapList(List> list)
  5. mergeMaps(Map m1, Map m2)
  6. mergeMaps(Map dest, Map inserts)
  7. mergeMaps(Map into, Map from, boolean override)
  8. mergeMaps(Map m1, Map m2)
  9. mergeMaps(Map map1, Map map2)