Java Map Merge mergeDefaults(Map content, Map defaults)

Here you can find the source of mergeDefaults(Map content, Map defaults)

Description

Merge defaults.

License

Open Source License

Parameter

Parameter Description
content the content
defaults the defaults

Declaration

@SuppressWarnings({ "unchecked" })
public static void mergeDefaults(Map<String, Object> content,
        Map<String, Object> defaults) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from w  w  w.j a  v a  2 s  . co  m
     * Merge defaults.
     *
     * @param content the content
     * @param defaults the defaults
     */
    @SuppressWarnings({ "unchecked" })
    public static void mergeDefaults(Map<String, Object> content,
            Map<String, Object> defaults) {
        for (Map.Entry<String, Object> defaultEntry : defaults.entrySet()) {
            if (!content.containsKey(defaultEntry.getKey())) {

                content.put(defaultEntry.getKey(), defaultEntry.getValue());
            } else {

                if (content.get(defaultEntry.getKey()) instanceof Map
                        && defaultEntry.getValue() instanceof Map) {
                    mergeDefaults(
                            (Map<String, Object>) content.get(defaultEntry
                                    .getKey()),
                            (Map<String, Object>) defaultEntry.getValue());
                } else if (content.get(defaultEntry.getKey()) instanceof List
                        && defaultEntry.getValue() instanceof List) {

                    List<Object> mergedList = new ArrayList<Object>();
                    mergedList.addAll((Collection<Object>) defaultEntry
                            .getValue());
                    mergedList.addAll((Collection<Object>) content
                            .get(defaultEntry.getKey()));
                    content.put(defaultEntry.getKey(), mergedList);
                }
            }
        }
    }
}

Related

  1. mergeConfig(Map userConfig, Map referenceConfig)
  2. mergeCounterMap(Map map, String key, Object value)
  3. mergeCountMaps(Map base, Map other)
  4. mergeCounts(Map counter, String key)
  5. mergeCustomProperties(Map copyTo, Map copyFrom)
  6. mergeEntries( Map dest, Iterable> entries)
  7. mergeEnv(Map newEnv, Map env)
  8. mergeEnvironmentVariables(Map environmentSource, Map environmentDestination, String environmentVariable)
  9. mergeFamilies(Map familySource, Map familyTarget)