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 2018/04/19 18:01:49 UTC

[1/2] [text] [TEXT-122 ]Allow full customization with new API org.apache.commons.text.lookup.StringLookupFactory.interpolatorStringLookup(Map, StringLookup, boolean).

Repository: commons-text
Updated Branches:
  refs/heads/master c22ff71b6 -> cbe1d77c7


[TEXT-122 ]Allow full customization with new API
org.apache.commons.text.lookup.StringLookupFactory.interpolatorStringLookup(Map<String,
StringLookup>, StringLookup, boolean).

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

Branch: refs/heads/master
Commit: faa9a23e89f3a7f30f9c7503319b439b93ac64ee
Parents: 49db4cb
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Apr 19 11:58:24 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Apr 19 11:58:24 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |   1 +
 .../text/lookup/InterpolatorStringLookup.java   | 314 +++++++-------
 .../text/lookup/StringLookupFactory.java        | 412 ++++++++++---------
 ...titutorWithInterpolatorStringLookupTest.java |  28 ++
 4 files changed, 414 insertions(+), 341 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/faa9a23e/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 22c9896..d97ead2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove.
   <release version="1.4" date="2018-MM-DD" description="Release 1.4">
     <action issue="TEXT-119" type="fix" dev="pschumacher">Remove mention of SQL escaping from user guide</action>
     <action issue="TEXT-121" type="update" dev="ggregory" due-to="pschumacher">Update Java requirement from version 7 to 8.</action>
+    <action issue="TEXT-122" type="update" dev="ggregory">Allow full customization with new API org.apache.commons.text.lookup.StringLookupFactory.interpolatorStringLookup(Map&lt;String, StringLookup>, StringLookup, boolean).</action>    
   </release>
 
   <release version="1.3" date="2018-03-16" description="Release 1.3">

http://git-wip-us.apache.org/repos/asf/commons-text/blob/faa9a23e/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
----------------------------------------------------------------------
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 798c234..e5c16a0 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -1,149 +1,165 @@
-/*
- * 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.Locale;
-import java.util.Map;
-
-/**
- * Proxies other {@link StringLookup}s using a keys within ${} markers using the format "${StringLookup:Key}".
- * <p>
- * The following lookups are used by default:
- * </p>
- * <ul>
- * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
- * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
- * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
- * <li>"date" for the {@link DateStringLookup}.</li>
- * </ul>
- */
-class InterpolatorStringLookup extends AbstractStringLookup {
-
-    /** Constant for the prefix separator. */
-    private static final char PREFIX_SEPARATOR = ':';
-
-    /** The default string lookup. */
-    private final StringLookup defaultStringLookup;
-
-    /** The map of String lookups keyed by prefix. */
-    private final Map<String, StringLookup> stringLookupMap = new HashMap<>();
-
-    /**
-     * Creates an instance using only lookups that work without initial properties and are stateless.
-     * <p>
-     * The following lookups are installed:
-     * </p>
-     * <ul>
-     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
-     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
-     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
-     * <li>"date" for the {@link DateStringLookup}.</li>
-     * </ul>
-     */
-    InterpolatorStringLookup() {
-        this((Map<String, String>) null);
-    }
-
-    /**
-     * Creates an instance using only lookups that work without initial properties and are stateless.
-     * <p>
-     * The following lookups are installed:
-     * </p>
-     * <ul>
-     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
-     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
-     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
-     * <li>"date" for the {@link DateStringLookup}.</li>
-     * </ul>
-     *
-     * @param <V>
-     *            the map's value type.
-     * @param defaultMap
-     *            the default map for string lookups.
-     */
-    <V> InterpolatorStringLookup(final Map<String, V> defaultMap) {
-        this(MapStringLookup.on(defaultMap == null ? new HashMap<String, V>() : defaultMap));
-        stringLookupMap.put("sys", SystemPropertyStringLookup.INSTANCE);
-        stringLookupMap.put("env", EnvironmentVariableStringLookup.INSTANCE);
-        stringLookupMap.put("java", JavaPlatformStringLookup.INSTANCE);
-        stringLookupMap.put("date", DateStringLookup.INSTANCE);
-        stringLookupMap.put("localhost", LocalHostStringLookup.INSTANCE);
-    }
-
-    /**
-     * Creates an instance with the given lookup.
-     *
-     * @param defaultStringLookup
-     *            the default lookup.
-     */
-    InterpolatorStringLookup(final StringLookup defaultStringLookup) {
-        this.defaultStringLookup = defaultStringLookup;
-    }
-
-    /**
-     * Gets the lookup map.
-     *
-     * @return the lookup map.
-     */
-    public Map<String, StringLookup> getStringLookupMap() {
-        return stringLookupMap;
-    }
-
-    /**
-     * Resolves the specified variable. This implementation will try to extract a variable prefix from the given
-     * variable name (the first colon (':') is used as prefix separator). It then passes the name of the variable with
-     * the prefix stripped to the lookup object registered for this prefix. If no prefix can be found or if the
-     * associated lookup object cannot resolve this variable, the default lookup object will be used.
-     *
-     * @param var
-     *            the name of the variable whose value is to be looked up
-     * @return the value of this variable or <b>null</b> if it cannot be resolved
-     */
-    @Override
-    public String lookup(String var) {
-        if (var == null) {
-            return null;
-        }
-
-        final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
-        if (prefixPos >= 0) {
-            final String prefix = var.substring(0, prefixPos).toLowerCase(Locale.US);
-            final String name = var.substring(prefixPos + 1);
-            final StringLookup lookup = stringLookupMap.get(prefix);
-            String value = null;
-            if (lookup != null) {
-                value = lookup.lookup(name);
-            }
-
-            if (value != null) {
-                return value;
-            }
-            var = var.substring(prefixPos + 1);
-        }
-        if (defaultStringLookup != null) {
-            return defaultStringLookup.lookup(var);
-        }
-        return null;
-    }
-
-    @Override
-    public String toString() {
-        return getClass().getName() + " [stringLookupMap=" + stringLookupMap + ", defaultStringLookup="
-                + defaultStringLookup + "]";
-    }
-}
+/*
+ * 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.Locale;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Proxies other {@link StringLookup}s using a keys within ${} markers using the format "${StringLookup:Key}".
+ * <p>
+ * The following lookups are used by default:
+ * </p>
+ * <ul>
+ * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+ * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+ * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+ * <li>"date" for the {@link DateStringLookup}.</li>
+ * </ul>
+ */
+class InterpolatorStringLookup extends AbstractStringLookup {
+
+    /** Constant for the prefix separator. */
+    private static final char PREFIX_SEPARATOR = ':';
+
+    /** The default string lookup. */
+    private final StringLookup defaultStringLookup;
+
+    /** The map of String lookups keyed by prefix. */
+    private final Map<String, StringLookup> stringLookupMap;
+
+    /**
+     * Creates an instance using only lookups that work without initial properties and are stateless.
+     * <p>
+     * The following lookups are installed:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * </ul>
+     */
+    InterpolatorStringLookup() {
+        this((Map<String, String>) null);
+    }
+
+    /**
+     * Creates an instance using only lookups that work without initial properties and are stateless.
+     * <p>
+     * The following lookups are installed:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * </ul>
+     *
+     * @param <V>
+     *            the map's value type.
+     * @param defaultMap
+     *            the default map for string lookups.
+     */
+    <V> InterpolatorStringLookup(final Map<String, V> defaultMap) {
+        this(MapStringLookup.on(defaultMap == null ? new HashMap<String, V>() : defaultMap));
+    }
+
+    /**
+     * Creates an instance with the given lookup.
+     *
+     * @param defaultStringLookup
+     *            the default lookup.
+     */
+    InterpolatorStringLookup(final StringLookup defaultStringLookup) {
+        this(new HashMap<>(), defaultStringLookup, true);
+    }
+
+    /**
+     * Creates a fully customized instance.
+     */
+    InterpolatorStringLookup(final Map<String, StringLookup> stringLookupMap, final StringLookup defaultStringLookup,
+            final boolean addDefaultLookups) {
+        super();
+        this.defaultStringLookup = defaultStringLookup;
+        this.stringLookupMap = new HashMap<>(stringLookupMap.size());
+        for (final Entry<String, StringLookup> entry : stringLookupMap.entrySet()) {
+            this.stringLookupMap.put(entry.getKey().toLowerCase(Locale.ROOT), entry.getValue());
+        }
+        if (addDefaultLookups) {
+            this.stringLookupMap.put("sys", SystemPropertyStringLookup.INSTANCE);
+            this.stringLookupMap.put("env", EnvironmentVariableStringLookup.INSTANCE);
+            this.stringLookupMap.put("java", JavaPlatformStringLookup.INSTANCE);
+            this.stringLookupMap.put("date", DateStringLookup.INSTANCE);
+            this.stringLookupMap.put("localhost", LocalHostStringLookup.INSTANCE);
+        }
+    }
+
+    /**
+     * Gets the lookup map.
+     *
+     * @return the lookup map.
+     */
+    public Map<String, StringLookup> getStringLookupMap() {
+        return stringLookupMap;
+    }
+
+    /**
+     * Resolves the specified variable. This implementation will try to extract a variable prefix from the given
+     * variable name (the first colon (':') is used as prefix separator). It then passes the name of the variable with
+     * the prefix stripped to the lookup object registered for this prefix. If no prefix can be found or if the
+     * associated lookup object cannot resolve this variable, the default lookup object will be used.
+     *
+     * @param var
+     *            the name of the variable whose value is to be looked up
+     * @return the value of this variable or <b>null</b> if it cannot be resolved
+     */
+    @Override
+    public String lookup(String var) {
+        if (var == null) {
+            return null;
+        }
+
+        final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
+        if (prefixPos >= 0) {
+            final String prefix = var.substring(0, prefixPos).toLowerCase(Locale.ROOT);
+            final String name = var.substring(prefixPos + 1);
+            final StringLookup lookup = stringLookupMap.get(prefix);
+            String value = null;
+            if (lookup != null) {
+                value = lookup.lookup(name);
+            }
+
+            if (value != null) {
+                return value;
+            }
+            var = var.substring(prefixPos + 1);
+        }
+        if (defaultStringLookup != null) {
+            return defaultStringLookup.lookup(var);
+        }
+        return null;
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getName() + " [stringLookupMap=" + stringLookupMap + ", defaultStringLookup="
+                + defaultStringLookup + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/faa9a23e/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 ac4d4c9..78a1ab3 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -1,192 +1,220 @@
-/*
- * 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;
-
-/**
- * Provides access to lookups defined in this package.
- *
- * @since 1.3
- */
-public final class StringLookupFactory {
-
-    /**
-     * Defines the singleton for this class.
-     */
-    public static final StringLookupFactory INSTANCE = new StringLookupFactory();
-
-    /**
-     * No need to build instances for now.
-     */
-    private StringLookupFactory() {
-        // empty
-    }
-
-    /**
-     * Returns the DateStringLookup singleton instance to format the current date with the format given in the key in a
-     * format compatible with {@link java.text.SimpleDateFormat}.
-     *
-     * @return the DateStringLookup singleton instance.
-     */
-    public StringLookup dateStringLookup() {
-        return DateStringLookup.INSTANCE;
-    }
-
-    /**
-     * Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable
-     * name.
-     *
-     * @return the EnvironmentVariableStringLookup singleton instance.
-     */
-    public StringLookup environmentVariableStringLookup() {
-        return EnvironmentVariableStringLookup.INSTANCE;
-    }
-
-    /**
-     * Returns a new InterpolatorStringLookup.
-     * <p>
-     * The following lookups are used by default:
-     * </p>
-     * <ul>
-     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
-     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
-     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
-     * <li>"date" for the {@link DateStringLookup}.</li>
-     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
-     * </ul>
-     *
-     * @return a new InterpolatorStringLookup.
-     */
-    public StringLookup interpolatorStringLookup() {
-        return new InterpolatorStringLookup();
-    }
-
-    /**
-     * Returns a new InterpolatorStringLookup.
-     * <p>
-     * The following lookups are used by default:
-     * </p>
-     * <ul>
-     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
-     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
-     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
-     * <li>"date" for the {@link DateStringLookup}.</li>
-     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
-     * </ul>
-     *
-     * @param <V>
-     *            the value type the default string lookup's map.
-     * @param map
-     *            the default map for string lookups.
-     * @return a new InterpolatorStringLookup.
-     */
-    public <V> StringLookup interpolatorStringLookup(final Map<String, V> map) {
-        return new InterpolatorStringLookup(map);
-    }
-
-    /**
-     * Returns a new InterpolatorStringLookup.
-     * <p>
-     * The following lookups are used by default:
-     * </p>
-     * <ul>
-     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
-     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
-     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
-     * <li>"date" for the {@link DateStringLookup}.</li>
-     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
-     * </ul>
-     *
-     * @param defaultStringLookup
-     *            the default string lookup.
-     * @return a new InterpolatorStringLookup.
-     */
-    public StringLookup interpolatorStringLookup(final StringLookup defaultStringLookup) {
-        return new InterpolatorStringLookup(defaultStringLookup);
-    }
-
-    /**
-     * Returns the JavaPlatformStringLookup singleton instance.
-     *
-     * @return the JavaPlatformStringLookup singleton instance.
-     */
-    public StringLookup javaPlatformStringLookup() {
-        return JavaPlatformStringLookup.INSTANCE;
-    }
-
-    /**
-     * Returns the LocalHostStringLookup singleton instance where the lookup key is one of:
-     * <ul>
-     * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</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>
-     *
-     * @return the DateStringLookup singleton instance.
-     */
-    public StringLookup localHostStringLookup() {
-        return LocalHostStringLookup.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.
-     * @param map
-     *            the map.
-     * @return a new MapStringLookup.
-     */
-    public <V> StringLookup mapStringLookup(final Map<String, V> map) {
-        return MapStringLookup.on(map);
-    }
-
-    /**
-     * Returns the NullStringLookup singleton instance which always returns null.
-     *
-     * @return the NullStringLookup singleton instance.
-     */
-    public StringLookup nullStringLookup() {
-        return NullStringLookup.INSTANCE;
-    }
-
-    /**
-     * Returns the ResourceBundleStringLookup singleton instance.
-     * <p>
-     * Looks up the value for a given key in the format "BundleName:BundleKey".
-     * </p>
-     * <p>
-     * For example: "com.domain.messages:MyKey".
-     * </p>
-     *
-     * @return the ResourceBundleStringLookup singleton instance.
-     */
-    public StringLookup resourceBundleStringLookup() {
-        return ResourceBundleStringLookup.INSTANCE;
-    }
-
-    /**
-     * Returns the SystemPropertyStringLookup singleton instance where the lookup key is a system property name.
-     *
-     * @return the SystemPropertyStringLookup singleton instance.
-     */
-    public StringLookup systemPropertyStringLookup() {
-        return SystemPropertyStringLookup.INSTANCE;
-    }
-}
+/*
+ * 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;
+
+/**
+ * Provides access to lookups defined in this package.
+ *
+ * @since 1.3
+ */
+public final class StringLookupFactory {
+
+    /**
+     * Defines the singleton for this class.
+     */
+    public static final StringLookupFactory INSTANCE = new StringLookupFactory();
+
+    /**
+     * No need to build instances for now.
+     */
+    private StringLookupFactory() {
+        // empty
+    }
+
+    /**
+     * Returns the DateStringLookup singleton instance to format the current date with the format given in the key in a
+     * format compatible with {@link java.text.SimpleDateFormat}.
+     *
+     * @return the DateStringLookup singleton instance.
+     */
+    public StringLookup dateStringLookup() {
+        return DateStringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable
+     * name.
+     *
+     * @return the EnvironmentVariableStringLookup singleton instance.
+     */
+    public StringLookup environmentVariableStringLookup() {
+        return EnvironmentVariableStringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns a new InterpolatorStringLookup.
+     * <p>
+     * The following lookups are used by default:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
+     * </ul>
+     *
+     * @return a new InterpolatorStringLookup.
+     */
+    public StringLookup interpolatorStringLookup() {
+        return new InterpolatorStringLookup();
+    }
+
+    /**
+     * Returns a new InterpolatorStringLookup.
+     * <p>
+     * The following lookups are used by default:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
+     * </ul>
+     *
+     * @param <V>
+     *            the value type the default string lookup's map.
+     * @param map
+     *            the default map for string lookups.
+     * @return a new InterpolatorStringLookup.
+     */
+    public <V> StringLookup interpolatorStringLookup(final Map<String, V> map) {
+        return new InterpolatorStringLookup(map);
+    }
+
+    /**
+     * Returns a new InterpolatorStringLookup.
+     * <p>
+     * The following lookups are used by default:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
+     * </ul>
+     *
+     * @param defaultStringLookup
+     *            the default string lookup.
+     * @return a new InterpolatorStringLookup.
+     */
+    public StringLookup interpolatorStringLookup(final StringLookup defaultStringLookup) {
+        return new InterpolatorStringLookup(defaultStringLookup);
+    }
+
+    /**
+     * Returns a new InterpolatorStringLookup.
+     * <p>
+     * If {@code addDefaultLookups} is true, the following lookups are used in addition to the ones provided in
+     * {@code stringLookupMap}:
+     * </p>
+     * <ul>
+     * <li>"sys" for the {@link SystemPropertyStringLookup}.</li>
+     * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
+     * <li>"java" for the {@link JavaPlatformStringLookup}.</li>
+     * <li>"date" for the {@link DateStringLookup}.</li>
+     * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li>
+     * </ul>
+     * 
+     * @param stringLookupMap
+     *            the map of string lookups.
+     * @param defaultStringLookup
+     *            the default string lookup.
+     * @param addDefaultLookups
+     *            whether to use lookups as described above.
+     * @return a new InterpolatorStringLookup.
+     * @since 1.4
+     */
+    public StringLookup interpolatorStringLookup(Map<String, StringLookup> stringLookupMap,
+            final StringLookup defaultStringLookup, final boolean addDefaultLookups) {
+        return new InterpolatorStringLookup(stringLookupMap, defaultStringLookup, addDefaultLookups);
+    }
+
+    /**
+     * Returns the JavaPlatformStringLookup singleton instance.
+     *
+     * @return the JavaPlatformStringLookup singleton instance.
+     */
+    public StringLookup javaPlatformStringLookup() {
+        return JavaPlatformStringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns the LocalHostStringLookup singleton instance where the lookup key is one of:
+     * <ul>
+     * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</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>
+     *
+     * @return the DateStringLookup singleton instance.
+     */
+    public StringLookup localHostStringLookup() {
+        return LocalHostStringLookup.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.
+     * @param map
+     *            the map.
+     * @return a new MapStringLookup.
+     */
+    public <V> StringLookup mapStringLookup(final Map<String, V> map) {
+        return MapStringLookup.on(map);
+    }
+
+    /**
+     * Returns the NullStringLookup singleton instance which always returns null.
+     *
+     * @return the NullStringLookup singleton instance.
+     */
+    public StringLookup nullStringLookup() {
+        return NullStringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns the ResourceBundleStringLookup singleton instance.
+     * <p>
+     * Looks up the value for a given key in the format "BundleName:BundleKey".
+     * </p>
+     * <p>
+     * For example: "com.domain.messages:MyKey".
+     * </p>
+     *
+     * @return the ResourceBundleStringLookup singleton instance.
+     */
+    public StringLookup resourceBundleStringLookup() {
+        return ResourceBundleStringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns the SystemPropertyStringLookup singleton instance where the lookup key is a system property name.
+     *
+     * @return the SystemPropertyStringLookup singleton instance.
+     */
+    public StringLookup systemPropertyStringLookup() {
+        return SystemPropertyStringLookup.INSTANCE;
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/faa9a23e/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index 0eb1b3d..33157b7 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -22,6 +22,7 @@ import java.net.UnknownHostException;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.text.lookup.StringLookup;
 import org.apache.commons.text.lookup.StringLookupFactory;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -29,6 +30,33 @@ import org.junit.jupiter.api.Test;
 public class StringSubstitutorWithInterpolatorStringLookupTest {
 
     @Test
+    public void testCustomMapWithDefaults() {
+        testCustomMapWithDefaults(true);
+    }
+
+    private void testCustomMapWithDefaults(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.mapStringLookup(map);
+        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 testLocalHostLookup_Address() throws UnknownHostException {
         final StringSubstitutor strSubst = new StringSubstitutor(
                 StringLookupFactory.INSTANCE.interpolatorStringLookup());


[2/2] [text] Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/commons-text.git

Posted by gg...@apache.org.
Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/commons-text.git

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

Branch: refs/heads/master
Commit: cbe1d77c7ee0b661622f3383741a0a674834a756
Parents: faa9a23 c22ff71
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Apr 19 12:01:18 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Apr 19 12:01:18 2018 -0600

----------------------------------------------------------------------
 pom.xml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------