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 <garydgreg...@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)); } }