org.craftercms.search.commons.utils.MapUtils Maven / Gradle / Ivy
/*
* 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy