You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/27 14:41:42 UTC
[commons-text] branch master updated (903e40e -> 4499606)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git.
from 903e40e Reuse StringUtils.substringAfter().
new 06a4648 Sort members.
new a34ccac Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String, V>).
new bb9b4e8 Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String, V>).
new 13d673a Annotate StringLookup as a FunctionalInterface.
new cec7345 Use factory class. Checkstyle. Add factory method.
new b0495d8 Replace internal MapStringLookup with FunctionStringLookup.
new 358f703 Replace internal SystemPropertyStringLookup with a FunctionStringLookup.
new d0e9ad7 Replace internal NullStringLookup with a FunctionStringLookup.
new 4499606 Replace internal EnvironmentVariableLookup with a FunctionStringLookup. Checkstyle.
The 9 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../lookup/EnvironmentVariableStringLookup.java | 70 ------------
.../commons/text/lookup/FunctionStringLookup.java | 115 ++++++++++++++++++++
.../text/lookup/InterpolatorStringLookup.java | 2 +-
.../text/lookup/JavaPlatformStringLookup.java | 2 +-
.../commons/text/lookup/MapStringLookup.java | 99 -----------------
.../commons/text/lookup/NullStringLookup.java | 47 ---------
.../apache/commons/text/lookup/StringLookup.java | 1 +
.../commons/text/lookup/StringLookupFactory.java | 117 +++++++++++++--------
.../text/lookup/SystemPropertyStringLookup.java | 55 ----------
...ubstitutorWithInterpolatorStringLookupTest.java | 27 +++++
.../EnvironmentVariableStringLookupTest.java | 8 +-
...okupTest.java => FunctionStringLookupTest.java} | 12 ++-
.../text/lookup/InterpolatorStringLookupTest.java | 2 +-
.../commons/text/lookup/NullStringLookupTest.java | 5 +-
.../text/lookup/StringLookupFactoryTest.java | 11 +-
.../lookup/SystemPropertyStringLookupTest.java | 6 +-
17 files changed, 244 insertions(+), 336 deletions(-)
delete mode 100644 src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java
create mode 100644 src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
delete mode 100644 src/main/java/org/apache/commons/text/lookup/MapStringLookup.java
delete mode 100644 src/main/java/org/apache/commons/text/lookup/NullStringLookup.java
delete mode 100644 src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java
rename src/test/java/org/apache/commons/text/lookup/{MapStringLookupTest.java => FunctionStringLookupTest.java} (74%)
[commons-text] 02/09: Add
org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function).
Posted by gg...@apache.org.
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
commit a34ccac32b2e4a0c7f8abc94b4881a3aa91b6c01
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:02:37 2020 -0400
Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String,
V>).
---
src/changes/changes.xml | 1 +
.../commons/text/lookup/FunctionStringLookup.java | 99 ++++++++++++++++++++++
.../commons/text/lookup/StringLookupFactory.java | 21 ++++-
.../text/lookup/FunctionStringLookupTest.java | 53 ++++++++++++
4 files changed, 170 insertions(+), 4 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 434c7f6..d43e07a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-177" type="update" dev="ggregory" due-to="Gary Gregory">Update from Apache Commons Lang 3.9 to 3.10.</action>
<action type="add" dev="ggregory">Add org.apache.commons.text.matcher.StringMatcher.isMatch(CharSequence, int).</action>
<action type="add" dev="ggregory">Add org.apache.commons.text.matcher.StringMatcher.isMatch(CharSequence, int, int, int).</action>
+ <action type="add" dev="ggregory">Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String, V>).</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.27.</action>
diff --git a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
new file mode 100644
index 0000000..464d1c5
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
@@ -0,0 +1,99 @@
+/*
+ * 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.commons.text.lookup;
+
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * A function-based lookup where the request for a lookup is answered by applying that function with a key.
+ *
+ * @param <V>
+ * A function's input type
+ *
+ * @since 1.9
+ */
+final class FunctionStringLookup<V> implements StringLookup {
+
+ /**
+ * Creates a new instance backed by a Function.
+ *
+ * @param <T>
+ * the function's input type
+ * @param function
+ * the function, may be null.
+ * @return a new instance backed by the given function.
+ */
+ static <T> FunctionStringLookup<T> on(final Function<String, T> function) {
+ return new FunctionStringLookup<>(function);
+ }
+
+ /**
+ * Function.
+ */
+ private final Function<String, V> function;
+
+ /**
+ * Creates a new instance backed by a Function.
+ *
+ * @param function
+ * the function, may be null.
+ */
+ private FunctionStringLookup(final Function<String, V> function) {
+ this.function = function;
+ }
+
+ /**
+ * Gets the function used in lookups.
+ *
+ * @return The function used in lookups.
+ */
+ Function<String, V> getFunction() {
+ return function;
+ }
+
+ /**
+ * Looks up a String key by applying the function.
+ * <p>
+ * If the function is null, then null is returned. The function result object is converted to a string using toString().
+ * </p>
+ *
+ * @param key
+ * the key to be looked up, may be null.
+ * @return The function result as a string, may be null.
+ */
+ @Override
+ public String lookup(final String key) {
+ if (function == null) {
+ return null;
+ }
+ final V obj;
+ try {
+ obj = function.apply(key);
+ } catch (final NullPointerException e) {
+ // Could be a ConcurrentHashMap and a null key request
+ return null;
+ }
+ return Objects.toString(obj, null);
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + " [map=" + function + "]";
+ }
+
+}
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 0eaa045..fc213d2 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -18,6 +18,7 @@
package org.apache.commons.text.lookup;
import java.util.Map;
+import java.util.function.Function;
import org.apache.commons.text.StringSubstitutor;
@@ -294,7 +295,7 @@ public final class StringLookupFactory {
stringLookupMap.put("base64", Base64DecoderStringLookup.INSTANCE);
for (final DefaultStringLookup stringLookup : DefaultStringLookup.values()) {
stringLookupMap.put(InterpolatorStringLookup.toKey(stringLookup.getKey()),
- stringLookup.getStringLookup());
+ stringLookup.getStringLookup());
}
}
}
@@ -536,6 +537,18 @@ public final class StringLookupFactory {
}
/**
+ * Returns a new function-based lookup where the request for a lookup is answered by applying the function with a lookup key.
+ *
+ * @param <V> the function input type.
+ * @param function the function.
+ * @return a new MapStringLookup.
+ * @since 1.9
+ */
+ public <V> StringLookup functionStringLookup(final Function<String, V> function) {
+ return FunctionStringLookup.on(function);
+ }
+
+ /**
* Returns a new InterpolatorStringLookup using the {@link StringLookupFactory default lookups}.
* <p>
* The lookups available to an interpolator are defined in
@@ -571,14 +584,14 @@ public final class StringLookupFactory {
* {@code stringLookupMap}:
* </p>
*
- * @param stringLookupMap the map of string lookups.
+ * @param stringLookupMap the map of string lookups.
* @param defaultStringLookup the default string lookup.
- * @param addDefaultLookups whether to use lookups as described above.
+ * @param addDefaultLookups whether to use lookups as described above.
* @return a new InterpolatorStringLookup.
* @since 1.4
*/
public StringLookup interpolatorStringLookup(final Map<String, StringLookup> stringLookupMap,
- final StringLookup defaultStringLookup, final boolean addDefaultLookups) {
+ final StringLookup defaultStringLookup, final boolean addDefaultLookups) {
return new InterpolatorStringLookup(stringLookupMap, defaultStringLookup, addDefaultLookups);
}
diff --git a/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java
new file mode 100644
index 0000000..1673096
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/FunctionStringLookupTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.commons.text.lookup;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link FunctionStringLookup}.
+ */
+public class FunctionStringLookupTest {
+
+ @Test
+ public void testConcurrentHashMapNull() {
+ final Map<String, Object> map = new ConcurrentHashMap<>();
+ Assertions.assertNull(FunctionStringLookup.on(k -> map.get(k)).lookup(null));
+ }
+
+ @Test
+ public void testHashMapNull() {
+ final Map<String, Object> map = new HashMap<>();
+ Assertions.assertNull(FunctionStringLookup.on(k -> map.get(k)).lookup(null));
+ }
+
+ @Test
+ public void testOne() {
+ final String key = "key";
+ final String value = "value";
+ final Map<String, String> map = new HashMap<>();
+ map.put(key, value);
+ Assertions.assertEquals(value, FunctionStringLookup.on(k -> map.get(k)).lookup(key));
+ }
+
+}
[commons-text] 09/09: Replace internal EnvironmentVariableLookup
with a FunctionStringLookup. Checkstyle.
Posted by gg...@apache.org.
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
commit 4499606ba73f4667d736b2816506485770d9f382
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:38:41 2020 -0400
Replace internal EnvironmentVariableLookup with a FunctionStringLookup.
Checkstyle.
---
.../lookup/EnvironmentVariableStringLookup.java | 70 ----------------------
.../text/lookup/JavaPlatformStringLookup.java | 2 +-
.../commons/text/lookup/StringLookupFactory.java | 15 +++--
.../EnvironmentVariableStringLookupTest.java | 8 +--
.../text/lookup/StringLookupFactoryTest.java | 9 +--
.../lookup/SystemPropertyStringLookupTest.java | 6 +-
6 files changed, 24 insertions(+), 86 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java b/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java
deleted file mode 100644
index 78758ef..0000000
--- a/src/main/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookup.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.commons.text.lookup;
-
-import org.apache.commons.text.StringSubstitutor;
-
-/**
- * Looks up keys from environment variables.
- * <p>
- * Using a {@link StringLookup} from the {@link StringLookupFactory}:
- * </p>
- *
- * <pre>
- * StringLookupFactory.INSTANCE.dateStringLookup().lookup("USER");
- * </pre>
- * <p>
- * Using a {@link StringSubstitutor}:
- * </p>
- *
- * <pre>
- * StringSubstitutor.createInterpolator().replace("... ${env:USER} ..."));
- * </pre>
- * <p>
- * The above examples convert (on Linux) {@code "USER"} to the current user name. On Windows 10, you would use
- * {@code "USERNAME"} to the same effect.
- * </p>
- *
- * @since 1.3
- */
-final class EnvironmentVariableStringLookup extends AbstractStringLookup {
-
- /**
- * Defines the singleton for this class.
- */
- static final EnvironmentVariableStringLookup INSTANCE = new EnvironmentVariableStringLookup();
-
- /**
- * No need to build instances for now.
- */
- private EnvironmentVariableStringLookup() {
- // empty
- }
-
- /**
- * Looks up the value of the given environment variable.
- *
- * @param key the key to be looked up, may be null
- * @return The value of the environment variable.
- * @see System#getenv(String)
- */
- @Override
- public String lookup(final String key) {
- // getenv throws NullPointerException if {@code name} is {@code null}
- return key != null ? System.getenv(key) : null;
- }
-}
diff --git a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
index defe498..2f8da70 100644
--- a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
@@ -146,7 +146,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup {
* @return a system property value.
*/
private String getSystemProperty(final String name) {
- return StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(name);
+ return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(name);
}
/**
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 3cced40..d87b2f3 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -67,7 +67,7 @@ import org.apache.commons.text.StringSubstitutor;
* </tr>
* <tr>
* <td>{@value #KEY_ENV}</td>
- * <td>{@link EnvironmentVariableStringLookup}</td>
+ * <td>{@link FunctionStringLookup}</td>
* <td>{@link #environmentVariableStringLookup()}</td>
* <td>1.3</td>
* </tr>
@@ -149,6 +149,12 @@ public final class StringLookupFactory {
public static final StringLookupFactory INSTANCE = new StringLookupFactory();
/**
+ * Defines the singleton for this class.
+ */
+ static final FunctionStringLookup<String> INSTANCE_ENVIRONMENT_VARIABLES = FunctionStringLookup
+ .on(key -> System.getenv(key));
+
+ /**
* Defines the FunctionStringLookup singleton that always returns null.
*/
static final FunctionStringLookup<String> INSTANCE_NULL = FunctionStringLookup.on(key -> null);
@@ -156,7 +162,8 @@ public final class StringLookupFactory {
/**
* Defines the FunctionStringLookup singleton for looking up system properties.
*/
- static final FunctionStringLookup<String> INSTANCE_SYSTEM_PROPERTY = FunctionStringLookup.on(key -> System.getProperty(key));
+ static final FunctionStringLookup<String> INSTANCE_SYSTEM_PROPERTIES = FunctionStringLookup
+ .on(key -> System.getProperty(key));
/**
* Default lookup key for interpolation {@value #KEY_BASE64_DECODER}.
@@ -516,7 +523,7 @@ public final class StringLookupFactory {
* @return The EnvironmentVariableStringLookup singleton instance.
*/
public StringLookup environmentVariableStringLookup() {
- return EnvironmentVariableStringLookup.INSTANCE;
+ return StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES;
}
/**
@@ -876,7 +883,7 @@ public final class StringLookupFactory {
* @return The SystemPropertyStringLookup singleton instance.
*/
public StringLookup systemPropertyStringLookup() {
- return StringLookupFactory.INSTANCE_SYSTEM_PROPERTY;
+ return StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES;
}
/**
diff --git a/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java
index b0a9c33..4b398a6 100644
--- a/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/EnvironmentVariableStringLookupTest.java
@@ -22,23 +22,23 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
- * Tests {@link EnvironmentVariableStringLookup}.
+ * Tests {@link StringLookupFactory#INSTANCE_ENVIRONMENT_VARIABLES}.
*/
public class EnvironmentVariableStringLookupTest {
@Test
public void testNull() {
- Assertions.assertNull(EnvironmentVariableStringLookup.INSTANCE.lookup(null));
+ Assertions.assertNull(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES.lookup(null));
}
@Test
public void testOne() {
if (SystemUtils.IS_OS_WINDOWS) {
final String key = "PATH";
- Assertions.assertEquals(System.getenv(key), EnvironmentVariableStringLookup.INSTANCE.lookup(key));
+ Assertions.assertEquals(System.getenv(key), StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES.lookup(key));
} else if (SystemUtils.IS_OS_LINUX) {
final String key = "USER";
- Assertions.assertEquals(System.getenv(key), EnvironmentVariableStringLookup.INSTANCE.lookup(key));
+ Assertions.assertEquals(System.getenv(key), StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES.lookup(key));
}
}
diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
index f4b23b1..ad82c59 100644
--- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
@@ -41,7 +41,7 @@ public class StringLookupFactoryTest {
assertTrue(stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_LOCALHOST)));
assertTrue(stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_PROPERTIES)));
assertTrue(
- stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_RESOURCE_BUNDLE)));
+ stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_RESOURCE_BUNDLE)));
assertTrue(stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_SCRIPT)));
assertTrue(stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_SYS)));
assertTrue(stringLookupMap.containsKey(InterpolatorStringLookup.toKey(StringLookupFactory.KEY_URL)));
@@ -73,15 +73,16 @@ public class StringLookupFactoryTest {
Assertions.assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup());
Assertions.assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup());
Assertions.assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());
- Assertions.assertSame(EnvironmentVariableStringLookup.INSTANCE,
- stringLookupFactory.environmentVariableStringLookup());
+ Assertions.assertSame(StringLookupFactory.INSTANCE_ENVIRONMENT_VARIABLES,
+ stringLookupFactory.environmentVariableStringLookup());
Assertions.assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
Assertions.assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
Assertions.assertSame(LocalHostStringLookup.INSTANCE, stringLookupFactory.localHostStringLookup());
Assertions.assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
- Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTY, stringLookupFactory.systemPropertyStringLookup());
+ Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES,
+ stringLookupFactory.systemPropertyStringLookup());
Assertions.assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
Assertions.assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
Assertions.assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
diff --git a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
index 3e769f6..57ba0b6 100644
--- a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
@@ -21,19 +21,19 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
- * Tests {@link StringLookupFactory#INSTANCE_SYSTEM_PROPERTY}.
+ * Tests {@link StringLookupFactory#INSTANCE_SYSTEM_PROPERTIES}.
*/
public class SystemPropertyStringLookupTest {
@Test
public void testNull() {
- Assertions.assertNull(StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(null));
+ Assertions.assertNull(StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(null));
}
@Test
public void testUserName() {
final String key = "user.name";
- Assertions.assertEquals(System.getProperty(key), StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(key));
+ Assertions.assertEquals(System.getProperty(key), StringLookupFactory.INSTANCE_SYSTEM_PROPERTIES.lookup(key));
}
}
[commons-text] 07/09: Replace internal SystemPropertyStringLookup
with a FunctionStringLookup.
Posted by gg...@apache.org.
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
commit 358f7032e39ffaf2be9a47307d84deb00670f36a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:28:50 2020 -0400
Replace internal SystemPropertyStringLookup with a FunctionStringLookup.
---
.../commons/text/lookup/FunctionStringLookup.java | 3 +-
.../text/lookup/JavaPlatformStringLookup.java | 2 +-
.../commons/text/lookup/StringLookupFactory.java | 9 +++-
.../text/lookup/SystemPropertyStringLookup.java | 55 ----------------------
.../text/lookup/StringLookupFactoryTest.java | 2 +-
.../lookup/SystemPropertyStringLookupTest.java | 6 +--
6 files changed, 14 insertions(+), 63 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 90e76a9..d262337 100644
--- a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
@@ -99,7 +99,8 @@ final class FunctionStringLookup<V> implements StringLookup {
final V obj;
try {
obj = function.apply(key);
- } catch (final NullPointerException e) {
+ } catch (final SecurityException | NullPointerException | IllegalArgumentException e) {
+ // Squelched. All lookup(String) will return null.
// Could be a ConcurrentHashMap and a null key request
return null;
}
diff --git a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
index 0d2cb32..defe498 100644
--- a/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/JavaPlatformStringLookup.java
@@ -146,7 +146,7 @@ final class JavaPlatformStringLookup extends AbstractStringLookup {
* @return a system property value.
*/
private String getSystemProperty(final String name) {
- return SystemPropertyStringLookup.INSTANCE.lookup(name);
+ return StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(name);
}
/**
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 6589068..5233fc6 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -109,7 +109,7 @@ import org.apache.commons.text.StringSubstitutor;
* </tr>
* <tr>
* <td>{@value #KEY_SYS}</td>
- * <td>{@link SystemPropertyStringLookup}</td>
+ * <td>{@link FunctionStringLookup}</td>
* <td>{@link #systemPropertyStringLookup()}</td>
* <td>1.3</td>
* </tr>
@@ -149,6 +149,11 @@ public final class StringLookupFactory {
public static final StringLookupFactory INSTANCE = new StringLookupFactory();
/**
+ * Defines the singleton for this class.
+ */
+ static final FunctionStringLookup<String> INSTANCE_SYSTEM_PROPERTY = FunctionStringLookup.on(key -> System.getProperty(key));
+
+ /**
* Default lookup key for interpolation {@value #KEY_BASE64_DECODER}.
*
* @since 1.6
@@ -866,7 +871,7 @@ public final class StringLookupFactory {
* @return The SystemPropertyStringLookup singleton instance.
*/
public StringLookup systemPropertyStringLookup() {
- return SystemPropertyStringLookup.INSTANCE;
+ return StringLookupFactory.INSTANCE_SYSTEM_PROPERTY;
}
/**
diff --git a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java b/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java
deleted file mode 100644
index 5261c93..0000000
--- a/src/main/java/org/apache/commons/text/lookup/SystemPropertyStringLookup.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.commons.text.lookup;
-
-/**
- * Looks up keys from system properties where the key is a system property name.
- *
- * @since 1.3
- */
-final class SystemPropertyStringLookup extends AbstractStringLookup {
-
- /**
- * Defines the singleton for this class.
- */
- static final SystemPropertyStringLookup INSTANCE = new SystemPropertyStringLookup();
-
- /**
- * No need to build instances for now.
- */
- private SystemPropertyStringLookup() {
- // empty
- }
-
- /**
- * Looks up the value for the key from system properties where the key is a system property name.
- *
- * @param key
- * the key to be looked up where the key is a system property name, may be null
- * @return The value associated with the key.
- * @see System#getProperty(String)
- */
- @Override
- public String lookup(final String key) {
- try {
- return System.getProperty(key);
- } catch (final SecurityException | NullPointerException | IllegalArgumentException e) {
- // Squelched. All lookup(String) will return null.
- return null;
- }
- }
-}
diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
index 5bf07fd..3f209cb 100644
--- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
@@ -81,7 +81,7 @@ public class StringLookupFactoryTest {
Assertions.assertSame(NullStringLookup.INSTANCE, stringLookupFactory.nullStringLookup());
Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
- Assertions.assertSame(SystemPropertyStringLookup.INSTANCE, stringLookupFactory.systemPropertyStringLookup());
+ Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTY, stringLookupFactory.systemPropertyStringLookup());
Assertions.assertSame(UrlDecoderStringLookup.INSTANCE, stringLookupFactory.urlDecoderStringLookup());
Assertions.assertSame(UrlEncoderStringLookup.INSTANCE, stringLookupFactory.urlEncoderStringLookup());
Assertions.assertSame(UrlStringLookup.INSTANCE, stringLookupFactory.urlStringLookup());
diff --git a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
index 4ab7e6e..3e769f6 100644
--- a/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/SystemPropertyStringLookupTest.java
@@ -21,19 +21,19 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
- * Tests {@link SystemPropertyStringLookup}.
+ * Tests {@link StringLookupFactory#INSTANCE_SYSTEM_PROPERTY}.
*/
public class SystemPropertyStringLookupTest {
@Test
public void testNull() {
- Assertions.assertNull(SystemPropertyStringLookup.INSTANCE.lookup(null));
+ Assertions.assertNull(StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(null));
}
@Test
public void testUserName() {
final String key = "user.name";
- Assertions.assertEquals(System.getProperty(key), SystemPropertyStringLookup.INSTANCE.lookup(key));
+ Assertions.assertEquals(System.getProperty(key), StringLookupFactory.INSTANCE_SYSTEM_PROPERTY.lookup(key));
}
}
[commons-text] 01/09: Sort members.
Posted by gg...@apache.org.
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
commit 06a4648e2432d0e17cca48dbac47a38e49fac3b6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 09:36:58 2020 -0400
Sort members.
---
.../commons/text/lookup/StringLookupFactory.java | 66 +++++++++++-----------
1 file changed, 33 insertions(+), 33 deletions(-)
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 b083ee7..0eaa045 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -448,6 +448,39 @@ public final class StringLookupFactory {
}
/**
+ * Returns the DnsStringLookup singleton instance where the lookup key is one of:
+ * <ul>
+ * <li><b>name</b>: for the local host name, for example {@code EXAMPLE} but also {@code EXAMPLE.apache.org}.</li>
+ * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
+ * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
+ * </ul>
+ *
+ * <p>
+ * Using a {@link StringLookup} from the {@link StringLookupFactory}:
+ * </p>
+ *
+ * <pre>
+ * StringLookupFactory.INSTANCE.dnsStringLookup().lookup("address|apache.org");
+ * </pre>
+ * <p>
+ * Using a {@link StringSubstitutor}:
+ * </p>
+ *
+ * <pre>
+ * StringSubstitutor.createInterpolator().replace("... ${dns:address|apache.org} ..."));
+ * </pre>
+ * <p>
+ * The above examples convert {@code "address|apache.org"} to {@code "95.216.24.32} (or {@code "40.79.78.1"}).
+ * </p>
+ *
+ * @return the DateStringLookup singleton instance.
+ * @since 1.8
+ */
+ public StringLookup dnsStringLookup() {
+ return DnsStringLookup.INSTANCE;
+ }
+
+ /**
* Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable
* name.
* <p>
@@ -644,39 +677,6 @@ public final class StringLookupFactory {
}
/**
- * Returns the DnsStringLookup singleton instance where the lookup key is one of:
- * <ul>
- * <li><b>name</b>: for the local host name, for example {@code EXAMPLE} but also {@code EXAMPLE.apache.org}.</li>
- * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
- * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
- * </ul>
- *
- * <p>
- * Using a {@link StringLookup} from the {@link StringLookupFactory}:
- * </p>
- *
- * <pre>
- * StringLookupFactory.INSTANCE.dnsStringLookup().lookup("address|apache.org");
- * </pre>
- * <p>
- * Using a {@link StringSubstitutor}:
- * </p>
- *
- * <pre>
- * StringSubstitutor.createInterpolator().replace("... ${dns:address|apache.org} ..."));
- * </pre>
- * <p>
- * The above examples convert {@code "address|apache.org"} to {@code "95.216.24.32} (or {@code "40.79.78.1"}).
- * </p>
- *
- * @return the DateStringLookup singleton instance.
- * @since 1.8
- */
- public StringLookup dnsStringLookup() {
- return DnsStringLookup.INSTANCE;
- }
-
- /**
* Returns a new map-based lookup where the request for a lookup is answered with the value for that key.
*
* @param <V> the map value type.
[commons-text] 04/09: Annotate StringLookup as a
FunctionalInterface.
Posted by gg...@apache.org.
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
commit 13d673ac6ab766e10b239af04f89c82b92062e65
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:05:59 2020 -0400
Annotate StringLookup as a FunctionalInterface.
---
src/main/java/org/apache/commons/text/lookup/StringLookup.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/org/apache/commons/text/lookup/StringLookup.java b/src/main/java/org/apache/commons/text/lookup/StringLookup.java
index 3201c5e..d89fdf2 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookup.java
@@ -30,6 +30,7 @@ package org.apache.commons.text.lookup;
*
* @since 1.3
*/
+@FunctionalInterface
public interface StringLookup {
/**
[commons-text] 03/09: Add
org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function).
Posted by gg...@apache.org.
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
commit bb9b4e8e15518ea38f917b837b1d8238356f85a1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:03:25 2020 -0400
Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String,
V>).
---
...ubstitutorWithInterpolatorStringLookupTest.java | 27 ++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index 2c94e5f..890e94f 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -36,6 +36,11 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
testCustomMapWithDefaults(true);
}
+ @Test
+ public void testCustomFunctionWithDefaults() {
+ testCustomFunctionWithDefaults(true);
+ }
+
private void testCustomMapWithDefaults(final boolean addDefaultLookups) {
final String key = "key";
final String value = "value";
@@ -53,12 +58,34 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
Assertions.assertEquals("value", strSubst.replace("${customLookup:key}"));
}
+ private void testCustomFunctionWithDefaults(final boolean addDefaultLookups) {
+ final String key = "key";
+ final String value = "value";
+ final Map<String, String> map = new HashMap<>();
+ map.put(key, value);
+ final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.functionStringLookup(k -> map.get(k));
+ final Map<String, StringLookup> stringLookupMap = new HashMap<>();
+ stringLookupMap.put("customLookup", mapStringLookup);
+ final StringSubstitutor strSubst = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
+ if (addDefaultLookups) {
+ final String spKey = "user.name";
+ Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
+ }
+ Assertions.assertEquals("value", strSubst.replace("${customLookup:key}"));
+ }
+
@Test
public void testCustomMapWithoutDefaults() {
testCustomMapWithDefaults(false);
}
@Test
+ public void testCustomFunctionWithoutDefaults() {
+ testCustomFunctionWithDefaults(false);
+ }
+
+ @Test
public void testDefaultInterpolator() {
// Used to cut and paste into the docs.
// @formatter:off
[commons-text] 08/09: Replace internal NullStringLookup with a
FunctionStringLookup.
Posted by gg...@apache.org.
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
commit d0e9ad75e800988604f3b0d927fde90e0eed4e17
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:33:32 2020 -0400
Replace internal NullStringLookup with a FunctionStringLookup.
---
.../commons/text/lookup/NullStringLookup.java | 47 ----------------------
.../commons/text/lookup/StringLookupFactory.java | 9 ++++-
.../commons/text/lookup/NullStringLookupTest.java | 5 ++-
.../text/lookup/StringLookupFactoryTest.java | 2 +-
4 files changed, 11 insertions(+), 52 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java b/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java
deleted file mode 100644
index f873e55..0000000
--- a/src/main/java/org/apache/commons/text/lookup/NullStringLookup.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.commons.text.lookup;
-
-/**
- * Always returns null.
- *
- * @since 1.3
- */
-final class NullStringLookup extends AbstractStringLookup {
-
- /**
- * Defines the singleton for this class.
- */
- static final NullStringLookup INSTANCE = new NullStringLookup();
-
- /**
- * No need to build instances for now.
- */
- private NullStringLookup() {
- // empty
- }
-
- /**
- * Always returns null.
- */
- @Override
- public String lookup(final String key) {
- return null;
- }
-
-}
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 5233fc6..3cced40 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -149,7 +149,12 @@ public final class StringLookupFactory {
public static final StringLookupFactory INSTANCE = new StringLookupFactory();
/**
- * Defines the singleton for this class.
+ * Defines the FunctionStringLookup singleton that always returns null.
+ */
+ static final FunctionStringLookup<String> INSTANCE_NULL = FunctionStringLookup.on(key -> null);
+
+ /**
+ * Defines the FunctionStringLookup singleton for looking up system properties.
*/
static final FunctionStringLookup<String> INSTANCE_SYSTEM_PROPERTY = FunctionStringLookup.on(key -> System.getProperty(key));
@@ -712,7 +717,7 @@ public final class StringLookupFactory {
* @return The NullStringLookup singleton instance.
*/
public StringLookup nullStringLookup() {
- return NullStringLookup.INSTANCE;
+ return StringLookupFactory.INSTANCE_NULL;
}
/**
diff --git a/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java
index fa5fc47..cb2feb5 100644
--- a/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/NullStringLookupTest.java
@@ -21,13 +21,14 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
- * Tests {@link NullStringLookup}.
+ * Tests {@link StringLookupFactory#INSTANCE_NULL}.
*/
public class NullStringLookupTest {
@Test
public void test() {
- Assertions.assertEquals(null, NullStringLookup.INSTANCE.lookup("EverythingIsNull"));
+ Assertions.assertEquals(null, StringLookupFactory.INSTANCE_NULL.lookup("EverythingIsNull"));
+ Assertions.assertEquals(null, StringLookupFactory.INSTANCE_NULL.lookup(null));
}
}
diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
index 3f209cb..f4b23b1 100644
--- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
@@ -78,7 +78,7 @@ public class StringLookupFactoryTest {
Assertions.assertSame(InterpolatorStringLookup.INSTANCE, stringLookupFactory.interpolatorStringLookup());
Assertions.assertSame(JavaPlatformStringLookup.INSTANCE, stringLookupFactory.javaPlatformStringLookup());
Assertions.assertSame(LocalHostStringLookup.INSTANCE, stringLookupFactory.localHostStringLookup());
- Assertions.assertSame(NullStringLookup.INSTANCE, stringLookupFactory.nullStringLookup());
+ Assertions.assertSame(StringLookupFactory.INSTANCE_NULL, stringLookupFactory.nullStringLookup());
Assertions.assertSame(ResourceBundleStringLookup.INSTANCE, stringLookupFactory.resourceBundleStringLookup());
Assertions.assertSame(ScriptStringLookup.INSTANCE, stringLookupFactory.scriptStringLookup());
Assertions.assertSame(StringLookupFactory.INSTANCE_SYSTEM_PROPERTY, stringLookupFactory.systemPropertyStringLookup());
[commons-text] 06/09: Replace internal MapStringLookup with
FunctionStringLookup.
Posted by gg...@apache.org.
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
commit b0495d88e37e30ca932bf17c85b08cd95fe5f164
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:19:53 2020 -0400
Replace internal MapStringLookup with FunctionStringLookup.
---
.../commons/text/lookup/MapStringLookup.java | 99 ----------------------
.../commons/text/lookup/StringLookupFactory.java | 5 +-
.../commons/text/lookup/MapStringLookupTest.java | 51 -----------
3 files changed, 3 insertions(+), 152 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java
deleted file mode 100644
index d7d9a94..0000000
--- a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * 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.commons.text.lookup;
-
-import java.util.Map;
-import java.util.Objects;
-
-/**
- * A map-based lookup where the request for a lookup is answered with the value for that key.
- *
- * @param <V>
- * A map's value type
- *
- * @since 1.3
- */
-final class MapStringLookup<V> implements StringLookup {
-
- /**
- * Creates a new instance backed by a Map. Used by the default lookup.
- *
- * @param <T>
- * the map's value type.
- * @param map
- * the map of keys to values, may be null.
- * @return a new instance backed by the given map.
- */
- static <T> MapStringLookup<T> on(final Map<String, T> map) {
- return new MapStringLookup<>(map);
- }
-
- /**
- * Map keys are variable names and value.
- */
- private final Map<String, V> map;
-
- /**
- * Creates a new instance backed by a Map. Used by the default lookup.
- *
- * @param map
- * the map of keys to values, may be null.
- */
- private MapStringLookup(final Map<String, V> map) {
- this.map = map;
- }
-
- /**
- * Gets the map used in lookups.
- *
- * @return The map used in lookups.
- */
- Map<String, V> getMap() {
- return map;
- }
-
- /**
- * Looks up a String key to a String value using the map.
- * <p>
- * If the map is null, then null is returned. The map result object is converted to a string using toString().
- * </p>
- *
- * @param key
- * the key to be looked up, may be null.
- * @return The matching value, null if no match
- */
- @Override
- public String lookup(final String key) {
- if (map == null) {
- return null;
- }
- final V obj;
- try {
- obj = map.get(key);
- } catch (final NullPointerException e) {
- // Could be a ConcurrentHashMap and a null key request
- return null;
- }
- return Objects.toString(obj, null);
- }
-
- @Override
- public String toString() {
- return super.toString() + " [map=" + map + "]";
- }
-
-}
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 fc213d2..6589068 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -537,7 +537,8 @@ public final class StringLookupFactory {
}
/**
- * Returns a new function-based lookup where the request for a lookup is answered by applying the function with a lookup key.
+ * Returns a new function-based lookup where the request for a lookup is answered by applying the function with a
+ * lookup key.
*
* @param <V> the function input type.
* @param function the function.
@@ -697,7 +698,7 @@ public final class StringLookupFactory {
* @return a new MapStringLookup.
*/
public <V> StringLookup mapStringLookup(final Map<String, V> map) {
- return MapStringLookup.on(map);
+ return FunctionStringLookup.on(map);
}
/**
diff --git a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java
deleted file mode 100644
index d358b24..0000000
--- a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.commons.text.lookup;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests {@link MapStringLookup}.
- */
-public class MapStringLookupTest {
-
- @Test
- public void testConcurrentHashMapNull() {
- Assertions.assertNull(MapStringLookup.on(new ConcurrentHashMap<String, Object>()).lookup(null));
- }
-
- @Test
- public void testHashMapNull() {
- Assertions.assertNull(MapStringLookup.on(new HashMap<String, Object>()).lookup(null));
- }
-
- @Test
- public void testOne() {
- final String key = "key";
- final String value = "value";
- final Map<String, String> map = new HashMap<>();
- map.put(key, value);
- Assertions.assertEquals(value, MapStringLookup.on(map).lookup(key));
- }
-
-}
[commons-text] 05/09: Use factory class. Checkstyle. Add factory
method.
Posted by gg...@apache.org.
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
commit cec7345489f251978962670d77f6d4e5d2032499
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:17:47 2020 -0400
Use factory class. Checkstyle. Add factory method.
---
.../commons/text/lookup/FunctionStringLookup.java | 21 ++++++++++++++++++---
.../text/lookup/InterpolatorStringLookup.java | 2 +-
.../text/lookup/InterpolatorStringLookupTest.java | 2 +-
3 files changed, 20 insertions(+), 5 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 464d1c5..90e76a9 100644
--- a/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/FunctionStringLookup.java
@@ -16,6 +16,7 @@
*/
package org.apache.commons.text.lookup;
+import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
@@ -43,6 +44,20 @@ final class FunctionStringLookup<V> implements StringLookup {
}
/**
+ * Creates a new instance backed by a Map. Used by the default lookup.
+ *
+ * @param <T>
+ * the map's value type.
+ * @param map
+ * the map of keys to values, may be null.
+ * @return a new instance backed by the given map.
+ */
+ static <T> FunctionStringLookup<T> on(final Map<String, T> map) {
+ return on(key -> map.get(key));
+ }
+
+
+ /**
* Function.
*/
private final Function<String, V> function;
@@ -69,11 +84,11 @@ final class FunctionStringLookup<V> implements StringLookup {
/**
* Looks up a String key by applying the function.
* <p>
- * If the function is null, then null is returned. The function result object is converted to a string using toString().
+ * If the function is null, then null is returned. The function result object is converted to a string using
+ * toString().
* </p>
*
- * @param key
- * the key to be looked up, may be null.
+ * @param key the key to be looked up, may be null.
* @return The function result as a string, may be null.
*/
@Override
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 48a7065..b00148c 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -67,7 +67,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
* the default map for string lookups.
*/
<V> InterpolatorStringLookup(final Map<String, V> defaultMap) {
- this(MapStringLookup.on(defaultMap == null ? new HashMap<String, V>() : defaultMap));
+ this(StringLookupFactory.INSTANCE.mapStringLookup(defaultMap == null ? new HashMap<String, V>() : defaultMap));
}
/**
diff --git a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java
index 1172523..19bb50f 100644
--- a/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/InterpolatorStringLookupTest.java
@@ -65,7 +65,7 @@ public class InterpolatorStringLookupTest {
public void testLookup() {
final Map<String, String> map = new HashMap<>();
map.put(TESTKEY, TESTVAL);
- final StringLookup lookup = new InterpolatorStringLookup(MapStringLookup.on(map));
+ final StringLookup lookup = new InterpolatorStringLookup(StringLookupFactory.INSTANCE.mapStringLookup(map));
String value = lookup.lookup(TESTKEY);
assertEquals(TESTVAL, value);
value = lookup.lookup("ctx:" + TESTKEY);