Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * This software is in the public domain under CC0 1.0 Universal plus a
 * Grant of Patent License.
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

import java.util.*;

public class Main {
    @SuppressWarnings("unchecked")
    public static void mergeNestedMap(Map<Object, Object> baseMap, Map<Object, Object> overrideMap,
            boolean overrideEmpty) {
        if (baseMap == null || overrideMap == null)
            return;
        for (Map.Entry<Object, Object> entry : overrideMap.entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (baseMap.containsKey(key)) {
                if (value == null) {
                    if (overrideEmpty)
                        baseMap.put(key, null);
                } else {
                    if (value instanceof CharSequence) {
                        if (overrideEmpty || ((CharSequence) value).length() > 0)
                            baseMap.put(key, value);
                    } else if (value instanceof Map) {
                        Object baseValue = baseMap.get(key);
                        if (baseValue != null && baseValue instanceof Map) {
                            mergeNestedMap((Map) baseValue, (Map) value, overrideEmpty);
                        } else {
                            baseMap.put(key, value);
                        }
                    } else if (value instanceof Collection) {
                        Object baseValue = baseMap.get(key);
                        if (baseValue != null && baseValue instanceof Collection) {
                            Collection baseCol = (Collection) baseValue;
                            Collection overrideCol = (Collection) value;
                            for (Object overrideObj : overrideCol) {
                                // NOTE: if we have a Collection of Map we have no way to merge the Maps without knowing the 'key' entries to use to match them
                                if (!baseCol.contains(overrideObj))
                                    baseCol.add(overrideObj);
                            }
                        } else {
                            baseMap.put(key, value);
                        }
                    } else {
                        // NOTE: no way to check empty, if not null not empty so put it
                        baseMap.put(key, value);
                    }
                }
            } else {
                baseMap.put(key, value);
            }
        }
    }
}