commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject [text] [TEXT-148] Add an enum to the lookup package that lists all StringLookups.
Date Mon, 15 Oct 2018 03:22:04 GMT
Repository: commons-text
Updated Branches:
  refs/heads/master e6c8d7e01 -> 349dd18f8


[TEXT-148] Add an enum to the lookup package that lists all
StringLookups.

Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/349dd18f
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/349dd18f
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/349dd18f

Branch: refs/heads/master
Commit: 349dd18f8b398c5074fb4bbaa0908f7738d38cfd
Parents: e6c8d7e
Author: Gary Gregory <ggregory@rocketsoftware.com>
Authored: Sun Oct 14 21:22:01 2018 -0600
Committer: Gary Gregory <ggregory@rocketsoftware.com>
Committed: Sun Oct 14 21:22:01 2018 -0600

----------------------------------------------------------------------
 .../text/lookup/DefaultStringLookup.java        | 147 +++++++++++++++++++
 .../text/lookup/StringLookupFactory.java        |  19 +--
 .../text/lookup/DefaultStringLookupTest.java    |  70 +++++++++
 3 files changed, 220 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/349dd18f/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
new file mode 100644
index 0000000..737129e
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
@@ -0,0 +1,147 @@
+/*
+ * 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;
+
+/**
+ * An enumeration defining {@link StringLookup} objects available through {@link StringLookupFactory}.
+ * <p>
+ * This enum was adapted from Apache Commons Configuration 2.4-SNAPSHOT.
+ * </p>
+ * 
+ * @see StringLookupFactory
+ * @see StringLookup
+ * @since 1.7
+ */
+public enum DefaultStringLookup {
+
+    /**
+     * The lookup for Base64 decoding.
+     */
+    BASE64_DECODER(StringLookupFactory.KEY_BASE64_DECODER, StringLookupFactory.INSTANCE.base64DecoderStringLookup()),
+
+    /**
+     * The lookup for Base64 decoding.
+     */
+    BASE64_ENCODER(StringLookupFactory.KEY_BASE64_ENCODER, StringLookupFactory.INSTANCE.base64EncoderStringLookup()),
+
+    /**
+     * The lookup for constants.
+     */
+    CONST(StringLookupFactory.KEY_CONST, StringLookupFactory.INSTANCE.constantStringLookup()),
+
+    /**
+     * The lookup for dates.
+     */
+    DATE(StringLookupFactory.KEY_DATE, StringLookupFactory.INSTANCE.dateStringLookup()),
+
+    /**
+     * The lookup for environment properties.
+     */
+    ENVIRONMENT(StringLookupFactory.KEY_ENV, StringLookupFactory.INSTANCE.environmentVariableStringLookup()),
+
+    /**
+     * The lookup for files.
+     */
+    FILE(StringLookupFactory.KEY_FILE, StringLookupFactory.INSTANCE.fileStringLookup()),
+
+    /**
+     * The lookup for Java platform information.
+     */
+    JAVA(StringLookupFactory.KEY_JAVA, StringLookupFactory.INSTANCE.javaPlatformStringLookup()),
+
+    /**
+     * The lookup for localhost information.
+     */
+    LOCAL_HOST(StringLookupFactory.KEY_LOCALHOST, StringLookupFactory.INSTANCE.localHostStringLookup()),
+
+    /**
+     * The lookup for properties.
+     */
+    PROPERTIES(StringLookupFactory.KEY_PROPERTIES, StringLookupFactory.INSTANCE.propertiesStringLookup()),
+
+    /**
+     * The lookup for resource bundles.
+     */
+    RESOURCE_BUNDLE(StringLookupFactory.KEY_RESOURCE_BUNDLE, StringLookupFactory.INSTANCE.resourceBundleStringLookup()),
+
+    /**
+     * The lookup for scripts.
+     */
+    SCRIPT(StringLookupFactory.KEY_SCRIPT, StringLookupFactory.INSTANCE.scriptStringLookup()),
+
+    /**
+     * The lookup for system properties.
+     */
+    SYSTEM_PROPERTIES(StringLookupFactory.KEY_SYS, StringLookupFactory.INSTANCE.systemPropertyStringLookup()),
+
+    /**
+     * The lookup for URLs.
+     */
+    URL(StringLookupFactory.KEY_URL, StringLookupFactory.INSTANCE.urlStringLookup()),
+
+    /**
+     * The lookup for URL decoding.
+     */
+    URL_DECODER(StringLookupFactory.KEY_URL_DECODER, StringLookupFactory.INSTANCE.urlDecoderStringLookup()),
+
+    /**
+     * The lookup for URL decoding.
+     */
+    URL_ENCODER(StringLookupFactory.KEY_URL_ENCODER, StringLookupFactory.INSTANCE.urlEncoderStringLookup()),
+
+    /**
+     * The lookup for URL decoding.
+     */
+    XML(StringLookupFactory.KEY_XML, StringLookupFactory.INSTANCE.xmlStringLookup());
+
+    /** The associated lookup instance. */
+    private final StringLookup lookup;
+
+    /** The prefix under which the associated lookup object is registered. */
+    private final String key;
+
+    /**
+     * Creates a new instance of {@link DefaultStringLookup} and sets the key and the associated
lookup instance.
+     *
+     * @param prefix
+     *            the prefix
+     * @param lookup
+     *            the {@link StringLookup} instance
+     */
+    private DefaultStringLookup(final String prefix, final StringLookup lookup) {
+        this.key = prefix;
+        this.lookup = lookup;
+    }
+
+    /**
+     * Returns the standard {@link StringLookup} instance of this kind.
+     *
+     * @return the associated {@link StringLookup} object
+     */
+    public StringLookup getStringLookup() {
+        return lookup;
+    }
+
+    /**
+     * Returns the standard prefix for the lookup object of this kind.
+     *
+     * @return the prefix
+     */
+    public String getKey() {
+        return key;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/349dd18f/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
----------------------------------------------------------------------
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 b80b15b..256044c 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -278,22 +278,9 @@ public final class StringLookupFactory {
         if (stringLookupMap != null) {
             // "base64" is deprecated in favor of KEY_BASE64_DECODER.
             stringLookupMap.put("base64", Base64DecoderStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_BASE64_DECODER, Base64DecoderStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_BASE64_ENCODER, Base64EncoderStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_CONST, ConstantStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_DATE, DateStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_ENV, EnvironmentVariableStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_FILE, FileStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_JAVA, JavaPlatformStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_LOCALHOST, LocalHostStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_PROPERTIES, PropertiesStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_RESOURCE_BUNDLE, ResourceBundleStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_SCRIPT, ScriptStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_SYS, SystemPropertyStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_URL, UrlStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_URL_DECODER, UrlDecoderStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_URL_ENCODER, UrlEncoderStringLookup.INSTANCE);
-            stringLookupMap.put(KEY_XML, XmlStringLookup.INSTANCE);
+            for (DefaultStringLookup stringLookup : DefaultStringLookup.values()) {
+                stringLookupMap.put(stringLookup.getKey(), stringLookup.getStringLookup());
+            }
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-text/blob/349dd18f/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java
new file mode 100644
index 0000000..3cc9711
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/DefaultStringLookupTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertSame;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link DefaultStringLookup}.
+ */
+public class DefaultStringLookupTest {
+
+    @Test
+    public void testEnumValues() {
+        final Map<String, StringLookup> stringLookupMap = new HashMap<>();
+        StringLookupFactory.INSTANCE.addDefaultStringLookups(stringLookupMap);
+        // Loop through all enums
+        for (DefaultStringLookup stringLookup : DefaultStringLookup.values()) {
+            assertSame(stringLookupMap.get(stringLookup.getKey()), stringLookupMap.get(stringLookup.getKey()));
+        }
+    }
+
+    @Test
+    public void testIndividualEnums() {
+        assertSame(DefaultStringLookup.BASE64_DECODER.getStringLookup(),
+                StringLookupFactory.INSTANCE.base64DecoderStringLookup());
+        assertSame(DefaultStringLookup.BASE64_ENCODER.getStringLookup(),
+                StringLookupFactory.INSTANCE.base64EncoderStringLookup());
+        assertSame(DefaultStringLookup.CONST.getStringLookup(), StringLookupFactory.INSTANCE.constantStringLookup());
+        assertSame(DefaultStringLookup.DATE.getStringLookup(), StringLookupFactory.INSTANCE.dateStringLookup());
+        assertSame(DefaultStringLookup.ENVIRONMENT.getStringLookup(),
+                StringLookupFactory.INSTANCE.environmentVariableStringLookup());
+        assertSame(DefaultStringLookup.FILE.getStringLookup(), StringLookupFactory.INSTANCE.fileStringLookup());
+        assertSame(DefaultStringLookup.JAVA.getStringLookup(), StringLookupFactory.INSTANCE.javaPlatformStringLookup());
+        assertSame(DefaultStringLookup.LOCAL_HOST.getStringLookup(),
+                StringLookupFactory.INSTANCE.localHostStringLookup());
+        assertSame(DefaultStringLookup.PROPERTIES.getStringLookup(),
+                StringLookupFactory.INSTANCE.propertiesStringLookup());
+        assertSame(DefaultStringLookup.RESOURCE_BUNDLE.getStringLookup(),
+                StringLookupFactory.INSTANCE.resourceBundleStringLookup());
+        assertSame(DefaultStringLookup.SCRIPT.getStringLookup(), StringLookupFactory.INSTANCE.scriptStringLookup());
+        assertSame(DefaultStringLookup.SYSTEM_PROPERTIES.getStringLookup(),
+                StringLookupFactory.INSTANCE.systemPropertyStringLookup());
+        assertSame(DefaultStringLookup.URL.getStringLookup(), StringLookupFactory.INSTANCE.urlStringLookup());
+        assertSame(DefaultStringLookup.URL_DECODER.getStringLookup(),
+                StringLookupFactory.INSTANCE.urlDecoderStringLookup());
+        assertSame(DefaultStringLookup.URL_ENCODER.getStringLookup(),
+                StringLookupFactory.INSTANCE.urlEncoderStringLookup());
+        assertSame(DefaultStringLookup.XML.getStringLookup(), StringLookupFactory.INSTANCE.xmlStringLookup());
+    }
+
+}


Mime
View raw message