Apache Solr open-source search software
Refactored the toMap() method to Utils
Noble Paul1f37c05b10e6
diff --git a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java b/solr/solrj/src/java/org/apache/solr/common/MapWriter.javaindex b9005057c38..72146dd1bae 100644--- a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java+++ b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java@@ -18,13 +18,11 @@package org.apache.solr.common;import java.io.IOException;-import java.util.ArrayList;import java.util.Collections;-import java.util.LinkedHashMap;-import java.util.List;import java.util.Map;import java.util.function.BiConsumer;import java.util.function.BiPredicate;+import java.util.function.Supplier;import org.apache.solr.common.util.Utils;/**@@ -39,46 +37,8 @@ public interface MapWriter extends MapSerializable, NavigableObject {}@Override- @SuppressWarnings({"unchecked", "rawtypes"})default Map<String, Object> toMap(Map<String, Object> map) {- try {- writeMap(- new EntryWriter() {- @Override- public EntryWriter put(CharSequence k, Object v) {- if (v instanceof MapWriter) v = ((MapWriter) v).toMap(new LinkedHashMap<>());- if (v instanceof IteratorWriter) v = ((IteratorWriter) v).toList(new ArrayList<>());- if (v instanceof Iterable) {- List lst = new ArrayList();- for (Object vv : (Iterable) v) {- if (vv instanceof MapWriter) vv = ((MapWriter) vv).toMap(new LinkedHashMap<>());- if (vv instanceof IteratorWriter)- vv = ((IteratorWriter) vv).toList(new ArrayList<>());- lst.add(vv);- }- v = lst;- }- if (v instanceof Map) {- Map map = new LinkedHashMap();- for (Map.Entry<?, ?> entry : ((Map<?, ?>) v).entrySet()) {- Object vv = entry.getValue();- if (vv instanceof MapWriter) vv = ((MapWriter) vv).toMap(new LinkedHashMap<>());- if (vv instanceof IteratorWriter)- vv = ((IteratorWriter) vv).toList(new ArrayList<>());- map.put(entry.getKey(), vv);- }- v = map;- }- map.put(k == null ? null : k.toString(), v);- // note: It'd be nice to assert that there is no previous value at 'k' but it's- // possible the passed map is already populated and the intention is to overwrite.- return this;- }- });- } catch (IOException e) {- throw new RuntimeException(e);- }- return map;+ return Utils.convertToMap(this, map);}void writeMap(EntryWriter ew) throws IOException;@@ -117,8 +77,11 @@ public interface MapWriter extends MapSerializable, NavigableObject {return this;}- default EntryWriter putStringIfNotNull(CharSequence k, Object v) throws IOException {- if (v != null) put(k, String.valueOf(v));+ default EntryWriter putIfNotNull(CharSequence k, Supplier<Object> v) throws IOException {+ Object val = v == null ? null : v.get();+ if (val != null) {+ putIfNotNull(k, val);+ }return this;}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 3e883ed7a95..8c062b00680 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@@ -965,7 +965,54 @@ public class Utils {}}- private static Map<Class<?>, List<FieldWriter>> storedReflectData = new ConcurrentHashMap<>();+ @SuppressWarnings({"unchecked", "rawtypes"})+ public static Map<String, Object> convertToMap(MapWriter m, Map<String, Object> map) {+ try {+ m.writeMap(+ new MapWriter.EntryWriter() {+ @Override+ public MapWriter.EntryWriter put(CharSequence k, Object v) {+ return writeEntry(k, v);+ }++ private MapWriter.EntryWriter writeEntry(CharSequence k, Object v) {+ if (v instanceof MapWriter) v = ((MapWriter) v).toMap(new LinkedHashMap<>());+ if (v instanceof IteratorWriter) v = ((IteratorWriter) v).toList(new ArrayList<>());+ if (v instanceof Iterable) {+ List lst = new ArrayList();+ for (Object vv : (Iterable) v) {+ if (vv instanceof MapWriter) vv = ((MapWriter) vv).toMap(new LinkedHashMap<>());+ if (vv instanceof IteratorWriter)+ vv = ((IteratorWriter) vv).toList(new ArrayList<>());+ lst.add(vv);+ }+ v = lst;+ }+ if (v instanceof Map) {+ Map map = new LinkedHashMap();+ for (Map.Entry<?, ?> entry : ((Map<?, ?>) v).entrySet()) {+ Object vv = entry.getValue();+ if (vv instanceof MapWriter) vv = ((MapWriter) vv).toMap(new LinkedHashMap<>());+ if (vv instanceof IteratorWriter)+ vv = ((IteratorWriter) vv).toList(new ArrayList<>());+ map.put(entry.getKey(), vv);+ }+ v = map;+ }+ map.put(k == null ? null : k.toString(), v);+ // note: It'd be nice to assert that there is no previous value at 'k' but it's+ // possible the passed map is already populated and the intention is to overwrite.+ return this;+ }+ });+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ return map;+ }++ private static final Map<Class<?>, List<FieldWriter>> storedReflectData =+ new ConcurrentHashMap<>();interface FieldWriter {void write(MapWriter.EntryWriter ew, Object inst) throws Throwable;