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:49 UTC

[commons-text] 07/09: Replace internal SystemPropertyStringLookup with a FunctionStringLookup.

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));
     }
 
 }