001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.text;
018
019import java.util.Map;
020
021/**
022 * Lookup a String key to a String value.
023 * <p>
024 * This class represents the simplest form of a string to string map.
025 * It has a benefit over a map in that it can create the result on
026 * demand based on the key.
027 * <p>
028 * This class comes complete with various factory methods.
029 * If these do not suffice, you can subclass and implement your own matcher.
030 * <p>
031 * For example, it would be possible to implement a lookup that used the
032 * key as a primary key, and looked up the value on demand from the database.
033 *
034 * @param <V> Unused.
035 * @since 2.2
036 * @deprecated as of 3.6, use commons-text
037 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringLookupFactory.html">
038 * StringLookupFactory</a> instead
039 */
040@Deprecated
041public abstract class StrLookup<V> {
042
043    /**
044     * Lookup that always returns null.
045     */
046    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
047
048    /**
049     * Lookup based on system properties.
050     */
051    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
052
053    //-----------------------------------------------------------------------
054    /**
055     * Returns a lookup which always returns null.
056     *
057     * @return a lookup that always returns null, not null
058     */
059    public static StrLookup<?> noneLookup() {
060        return NONE_LOOKUP;
061    }
062
063    /**
064     * Returns a new lookup which uses a copy of the current
065     * {@link System#getProperties() System properties}.
066     * <p>
067     * If a security manager blocked access to system properties, then null will
068     * be returned from every lookup.
069     * <p>
070     * If a null key is used, this lookup will throw a NullPointerException.
071     *
072     * @return a lookup using system properties, not null
073     */
074    public static StrLookup<String> systemPropertiesLookup() {
075        return SYSTEM_PROPERTIES_LOOKUP;
076    }
077
078    /**
079     * Returns a lookup which looks up values using a map.
080     * <p>
081     * If the map is null, then null will be returned from every lookup.
082     * The map result object is converted to a string using toString().
083     *
084     * @param <V> the type of the values supported by the lookup
085     * @param map  the map of keys to values, may be null
086     * @return a lookup using the map, not null
087     */
088    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
089        return new MapStrLookup<>(map);
090    }
091
092    //-----------------------------------------------------------------------
093    /**
094     * Constructor.
095     */
096    protected StrLookup() {
097        super();
098    }
099
100    /**
101     * Looks up a String key to a String value.
102     * <p>
103     * The internal implementation may use any mechanism to return the value.
104     * The simplest implementation is to use a Map. However, virtually any
105     * implementation is possible.
106     * <p>
107     * For example, it would be possible to implement a lookup that used the
108     * key as a primary key, and looked up the value on demand from the database
109     * Or, a numeric based implementation could be created that treats the key
110     * as an integer, increments the value and return the result as a string -
111     * converting 1 to 2, 15 to 16 etc.
112     * <p>
113     * The {@link #lookup(String)} method always returns a String, regardless of
114     * the underlying data, by converting it as necessary. For example:
115     * <pre>
116     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
117     * map.put("number", Integer.valueOf(2));
118     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
119     * </pre>
120     * @param key  the key to be looked up, may be null
121     * @return the matching value, null if no match
122     */
123    public abstract String lookup(String key);
124
125    //-----------------------------------------------------------------------
126    /**
127     * Lookup implementation that uses a Map.
128     */
129    static class MapStrLookup<V> extends StrLookup<V> {
130
131        /** Map keys are variable names and value. */
132        private final Map<String, V> map;
133
134        /**
135         * Creates a new instance backed by a Map.
136         *
137         * @param map  the map of keys to values, may be null
138         */
139        MapStrLookup(final Map<String, V> map) {
140            this.map = map;
141        }
142
143        /**
144         * Looks up a String key to a String value using the map.
145         * <p>
146         * If the map is null, then null is returned.
147         * The map result object is converted to a string using toString().
148         *
149         * @param key  the key to be looked up, may be null
150         * @return the matching value, null if no match
151         */
152        @Override
153        public String lookup(final String key) {
154            if (map == null) {
155                return null;
156            }
157            final Object obj = map.get(key);
158            if (obj == null) {
159                return null;
160            }
161            return obj.toString();
162        }
163    }
164
165    //-----------------------------------------------------------------------
166    /**
167     * Lookup implementation based on system properties.
168     */
169    private static class SystemPropertiesStrLookup extends StrLookup<String> {
170        /**
171         * {@inheritDoc} This implementation directly accesses system properties.
172         */
173        @Override
174        public String lookup(final String key) {
175            if (!key.isEmpty()) {
176                try {
177                    return System.getProperty(key);
178                } catch (final SecurityException scex) {
179                    // Squelched. All lookup(String) will return null.
180                }
181            }
182            return null;
183        }
184    }
185}