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

org.craftercms.search.commons.utils.MapUtils Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright (C) 2007-2021 Crafter Software Corporation. All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.craftercms.search.commons.utils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static java.util.Arrays.asList;
import static org.apache.commons.collections4.ListUtils.union;
import static org.apache.commons.collections4.MapUtils.isEmpty;

/**
 * @author joseross
 */
public abstract class MapUtils {

    @SuppressWarnings("unchecked")
    public static Map mergeMaps(Map a, Map b) {
        if (isEmpty(a)) {
            return b;
        }

        if (isEmpty(b)) {
            return a;
        }

        TreeMap map = new TreeMap<>(a);
        b.forEach((key, value) -> map.merge(key, value, (oldValue, newValue) -> {
            if (oldValue instanceof Map && newValue instanceof Map) {
                return mergeMaps((Map) oldValue, (Map) newValue);
            } else if (oldValue instanceof Map || newValue instanceof Map) {
                // can't be merged, just return the original
                return oldValue;
            } else if (oldValue instanceof List && newValue instanceof List) {
                return union((List) oldValue, (List) newValue);
            } else if (oldValue instanceof List) {
                List list = new LinkedList<>((List) oldValue);
                list.add(newValue);
                return list;
            } else if (newValue instanceof List) {
                List list = new LinkedList<>((List) newValue);
                list.add(oldValue);
                return list;
            } else {
                // single properties are not merged, only overwritten
                return newValue;
            }
        }));

        return map;
    }

}