/*
 * 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}".
 */
public class InterpolatorStringLookup extends AbstractStringLookup {

    /** Constant for the prefix separator. */
    private static final char PREFIX_SEPARATOR = ':';

    private final StringLookup defaultStringLookup;

    private final Map<String, StringLookup> stringLookupMap = new HashMap<>();

    /**
     * Create the default Interpolator using only Lookups that work without an event.
     */
    public InterpolatorStringLookup() {
        this((Map<String, String>) null);
    }

    /**
     * Creates the Interpolator using only Lookups that work without an event and initial properties.
     * <p>
     * The following lookups are installed:
     * </p>
     * <ul>
     * <li>"sys" for the SystemPropertyStringLookup.</li>
     * <li>"env" for the EnvironmentVariableStringLookup.</li>
     * <li>"java" for the JavaPlatformStringLookup.</li>
     * <li>"date" for the DateStringLookup.</li>
     * </ul>
     * 
     * @param defaultMap
     *            the default map for string lookups.
     */
    public InterpolatorStringLookup(final Map<String, String> defaultMap) {
        this(new MapStringLookup<>(defaultMap == null ? new HashMap<String, String>() : defaultMap));
        // TODO: Use a service loader
        stringLookupMap.put("sys", SystemPropertyStringLookup.INSTANCE);
        stringLookupMap.put("env", EnvironmentVariableStringLookup.INSTANCE);
        stringLookupMap.put("java", JavaPlatformStringLookup.INSTANCE);
        stringLookupMap.put("date", DateStringLookup.INSTANCE);
    }

    public InterpolatorStringLookup(final StringLookup defaultStringLookup) {
        this.defaultStringLookup = defaultStringLookup;
    }

    public Map<String, StringLookup> getStrLookupMap() {
        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 + "]";
    }
}

@@ -1392,33 +1392,21 @@
 eate
- the default Interpolator
+s an instance
  usi
@@ -1405,33 +1405,33 @@
 ance using only 
-L
+l
 ookups that work
@@ -1443,17 +1443,373 @@
 out 
-an event.
+initial properties and are stateless.%0D%0A     * %3Cp%3E%0D%0A     * The following lookups are installed:%0D%0A     * %3C/p%3E%0D%0A     * %3Cul%3E%0D%0A     * %3Cli%3E%22sys%22 for the SystemPropertyStringLookup.%3C/li%3E%0D%0A     * %3Cli%3E%22env%22 for the EnvironmentVariableStringLookup.%3C/li%3E%0D%0A     * %3Cli%3E%22java%22 for the JavaPlatformStringLookup.%3C/li%3E%0D%0A     * %3Cli%3E%22date%22 for the DateStringLookup.%3C/li%3E%0D%0A     * %3C/ul%3E
 %0D%0A  
@@ -1936,24 +1936,19 @@
 tes 
-the Interpolator
+an instance
  usi
@@ -1955,17 +1955,17 @@
 ng only 
-L
+l
 ookups t
@@ -1985,21 +1985,8 @@
 out 
-an event and 
 init
@@ -1999,16 +1999,34 @@
 operties
+ and are stateless
 .%0D%0A     
@@ -2958,32 +2958,184 @@
 NCE);%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Creates an instance with the given lookup.%0D%0A     * %0D%0A     * @param defaultStringLookup%0D%0A     *            the default lookup.%0D%0A     */%0D%0A
     public Inter
@@ -3259,24 +3259,112 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Gets the lookup map.%0D%0A     * %0D%0A     * @return the lookup map.%0D%0A     */%0D%0A
     public M
@@ -3394,16 +3394,19 @@
 %3E getStr
+ing
 LookupMa

Add an interpolator string lookup. Javadoc.