You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by no...@apache.org on 2022/11/09 04:32:11 UTC
[solr] branch main updated: Refactored the toMap() method to Utils
This is an automated email from the ASF dual-hosted git repository.
noble pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 1f37c05b10e Refactored the toMap() method to Utils
1f37c05b10e is described below
commit 1f37c05b10e6b385d3950bfb3d9d4eaee301aa42
Author: Noble Paul <no...@gmail.com>
AuthorDate: Wed Nov 9 15:31:55 2022 +1100
Refactored the toMap() method to Utils
---
.../src/java/org/apache/solr/common/MapWriter.java | 51 +++-------------------
.../java/org/apache/solr/common/util/Utils.java | 49 ++++++++++++++++++++-
2 files changed, 55 insertions(+), 45 deletions(-)
diff --git a/solr/solrj/src/java/org/apache/solr/common/MapWriter.java b/solr/solrj/src/java/org/apache/solr/common/MapWriter.java
index 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.java
index 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;