mirror of https://github.com/apache/lucene.git
SOLR-11522: Moved the _get methods to a separate interafce and keep MapWriter clean
This commit is contained in:
parent
d799fd53c7
commit
576d28f643
|
@ -23,7 +23,6 @@ import java.util.ArrayList;
|
|||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
import org.apache.solr.common.util.Utils;
|
||||
|
@ -33,7 +32,7 @@ import org.apache.solr.common.util.Utils;
|
|||
* This avoids creating map instances and is supposed to be memory efficient.
|
||||
* If the entries are primitives, unnecessary boxing is also avoided.
|
||||
*/
|
||||
public interface MapWriter extends MapSerializable {
|
||||
public interface MapWriter extends MapSerializable , NavigableObject {
|
||||
|
||||
default String jsonStr(){
|
||||
return Utils.toJSONString(this);
|
||||
|
@ -81,51 +80,6 @@ public interface MapWriter extends MapSerializable {
|
|||
|
||||
void writeMap(EntryWriter ew) throws IOException;
|
||||
|
||||
/**Get a child object value using json path
|
||||
*
|
||||
* @param path the full path to that object such as a/b/c[4]/d etc
|
||||
* @param def the default
|
||||
* @return the found value or default
|
||||
*/
|
||||
default Object _get(String path, Object def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : v;
|
||||
}
|
||||
|
||||
default String _getStr(String path, String def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : String.valueOf(v);
|
||||
}
|
||||
|
||||
default void _forEachEntry(String path, BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, path, fun);
|
||||
}
|
||||
|
||||
default void _forEachEntry(List<String> path, BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, path, fun);
|
||||
}
|
||||
|
||||
default void _forEachEntry(BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, fun);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a child object value using json path
|
||||
*
|
||||
* @param path the full path to that object such as ["a","b","c[4]","d"] etc
|
||||
* @param def the default
|
||||
* @return the found value or default
|
||||
*/
|
||||
default Object _get(List<String> path, Object def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : v;
|
||||
}
|
||||
|
||||
default String _getStr(List<String> path, String def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : String.valueOf(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface to push one entry at a time to the output.
|
||||
* The order of the keys is not defined, but we assume they are distinct -- don't call {@code put} more than once
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* 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.common;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.apache.solr.common.util.Utils;
|
||||
|
||||
/**This class contains helper methods for navigating deeply nested Objects. Keep in mind that
|
||||
* it may be expensive depending on the underlying implementation. each level needs an extra lookup
|
||||
* and the lookup may be as expensive as O(log(n)) to O(o) depending on the underlying impl
|
||||
*
|
||||
*/
|
||||
public interface NavigableObject {
|
||||
/**Get a child object value using json path. This usually ends up in String split operations
|
||||
* use a list of strings where performance is important
|
||||
*
|
||||
* @param path the full path to that object such as a/b/c[4]/d etc
|
||||
* @param def the default
|
||||
* @return the found value or default
|
||||
*/
|
||||
default Object _get(String path, Object def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : v;
|
||||
}
|
||||
|
||||
/**get the value as a String. useful in tests
|
||||
*
|
||||
* @param path the full path
|
||||
* @param def default value
|
||||
*/
|
||||
default String _getStr(String path, String def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : String.valueOf(v);
|
||||
}
|
||||
|
||||
/**Iterate through the entries of a navigable Object at a certain path
|
||||
* @param path the json path
|
||||
*/
|
||||
default void _forEachEntry(String path, BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, path, fun);
|
||||
}
|
||||
|
||||
/**Iterate through the entries of a navigable Object at a certain path
|
||||
* @param path the json path
|
||||
*/
|
||||
default void _forEachEntry(List<String> path, BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, path, fun);
|
||||
}
|
||||
|
||||
/**Iterate through each entry in this object
|
||||
*/
|
||||
default void _forEachEntry(BiConsumer fun) {
|
||||
Utils.forEachMapEntry(this, fun);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a child object value using json path
|
||||
*
|
||||
* @param path the full path to that object such as ["a","b","c[4]","d"] etc
|
||||
* @param def the default
|
||||
* @return the found value or default
|
||||
*/
|
||||
default Object _get(List<String> path, Object def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : v;
|
||||
}
|
||||
|
||||
default String _getStr(List<String> path, String def) {
|
||||
Object v = Utils.getObjectByPath(this, false, path);
|
||||
return v == null ? def : String.valueOf(v);
|
||||
}
|
||||
}
|
|
@ -107,14 +107,14 @@ public class Utils {
|
|||
return mutable ? copy : Collections.unmodifiableMap(copy);
|
||||
}
|
||||
|
||||
public static void forEachMapEntry(MapWriter mw, String path, BiConsumer fun) {
|
||||
Object o = Utils.getObjectByPath(mw, false, path);
|
||||
forEachMapEntry(o, fun);
|
||||
public static void forEachMapEntry(Object o, String path, BiConsumer fun) {
|
||||
Object val = Utils.getObjectByPath(o, false, path);
|
||||
forEachMapEntry(val, fun);
|
||||
}
|
||||
|
||||
public static void forEachMapEntry(MapWriter mw, List<String> path, BiConsumer fun) {
|
||||
Object o = Utils.getObjectByPath(mw, false, path);
|
||||
forEachMapEntry(o, fun);
|
||||
public static void forEachMapEntry(Object o, List<String> path, BiConsumer fun) {
|
||||
Object val = Utils.getObjectByPath(o, false, path);
|
||||
forEachMapEntry(val, fun);
|
||||
}
|
||||
|
||||
public static void forEachMapEntry(Object o, BiConsumer fun) {
|
||||
|
|
Loading…
Reference in New Issue