This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new f0ba80ba Internal refactoring
f0ba80ba is described below
commit f0ba80ba392346bf3d18adefd23b502ad30fd7cb
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Jun 2 06:34:40 2022 -0400
Internal refactoring
---
.../apache/commons/text/lookup/FunctionStringLookup.java | 3 +--
.../commons/text/lookup/InterpolatorStringLookup.java | 5 +++--
.../org/apache/commons/text/lookup/StringLookupFactory.java | 13 +++++++++++++
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git
a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
index 50dd18d2..026ba4bc 100644
--- a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
@@ -16,7 +16,6 @@
*/
package org.apache.commons.text.lookup;
-import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
@@ -49,7 +48,7 @@ final class FunctionStringLookup<V> extends
AbstractStringLookup {
* @return a new instance backed by the given map.
*/
static <V> FunctionStringLookup<V> on(final Map<String, V> map) {
- return on((map == null ? Collections.<String, V>emptyMap() :
map)::get);
+ return on(StringLookupFactory.toMap(map)::get);
}
/**
diff --git
a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
index 1a8124e8..7a63ef61 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.text.lookup;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -88,7 +89,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
* @param defaultMap the default map for string lookups.
*/
<V> InterpolatorStringLookup(final Map<String, V> defaultMap) {
- this(StringLookupFactory.INSTANCE.mapStringLookup(defaultMap == null ?
new HashMap<>() : defaultMap));
+ this(StringLookupFactory.INSTANCE.mapStringLookup(defaultMap));
}
/**
@@ -97,7 +98,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
* @param defaultStringLookup the default lookup.
*/
InterpolatorStringLookup(final StringLookup defaultStringLookup) {
- this(new HashMap<>(), defaultStringLookup, true);
+ this(Collections.emptyMap(), defaultStringLookup, true);
}
/**
diff --git
a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
index e2d717da..54cd61de 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
+import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
@@ -416,6 +417,18 @@ public final class StringLookupFactory {
return properties;
}
+ /**
+ * Returns the given map if the input is non-null or an empty immutable
map if the input is null.
+ *
+ * @param <K> the class of the map keys
+ * @param <V> the class of the map values
+ * @param map The map to test
+ * @return the given map if the input is non-null or an empty immutable
map if the input is null.
+ */
+ static <K, V> Map<K, V> toMap(final Map<K, V> map) {
+ return map == null ? Collections.emptyMap() : map;
+ }
+
/**
* No need to build instances for now.
*/