Internal refactoring
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 50dd18d..026ba4b 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 @@
* @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 1a8124e..7a63ef6 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 @@
* @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 @@
* @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 e2d717d..54cd61d 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.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;
@@ -417,6 +418,18 @@
}
/**
+ * 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.
*/
private StringLookupFactory() {