Apache Solr open-source search software
SOLR-11522: Moved the _get methods to a separate interafce and keep MapWriter clean
noble576d28f643a8
diff --git a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java b/solr/solrj/src/java/org/apache/solr/common/MapWriter.javaindex d2f14d8480b..d6c9efb5cb5 100644--- a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java+++ b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java@@ -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 oncediff --git a/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.javanew file mode 100644index 00000000000..be50a17ea42--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/common/NavigableObject.java@@ -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);+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java b/solr/solrj/src/java/org/apache/solr/common/util/Utils.javaindex bdfd03eea88..a1b34a2290c 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/Utils.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/Utils.java@@ -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) {