org.apache.solr.request.json.ObjectUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.request.json;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.SolrException;
public class ObjectUtil {
public static class ConflictHandler {
protected boolean isList(
Map container,
List path,
String key,
Object current,
Object previous) {
return key != null && ("fields".equals(key) || "filter".equals(key));
}
public void handleConflict(
Map container,
List path,
String key,
Object current,
Object previous) {
boolean handleAsList = isList(container, path, key, current, previous);
if (handleAsList) {
container.put(key, makeList(current, previous));
return;
}
if (previous instanceof Map && current instanceof Map) {
@SuppressWarnings({"unchecked"})
Map prevMap = (Map) previous;
@SuppressWarnings({"unchecked"})
Map currMap = (Map) current;
if (prevMap.size() == 0) return;
mergeMap(prevMap, currMap, path);
container.put(key, prevMap);
return;
}
// if we aren't handling as a list, and we aren't handling as a map, then just overwrite (i.e.
// nothing else to do)
}
// merges srcMap onto targetMap (i.e. changes targetMap but not srcMap)
public void mergeMap(
Map targetMap, Map srcMap, List path) {
if (srcMap.size() == 0) return;
// to keep ordering correct, start with prevMap and merge in currMap
for (Map.Entry srcEntry : srcMap.entrySet()) {
String subKey = srcEntry.getKey();
Object subVal = srcEntry.getValue();
Object subPrev = targetMap.put(subKey, subVal);
if (subPrev != null) {
// recurse
path.add(subKey);
handleConflict(targetMap, path, subKey, subVal, subPrev);
path.remove(path.size() - 1);
}
}
}
protected List