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.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> the type of the values supported by the lookup
035 * @since 1.0
036 */
037public abstract class StrLookup<V> {
038
039    /**
040     * Lookup that always returns null.
041     */
042    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
043
044    /**
045     * Lookup based on system properties.
046     */
047    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
048
049    //-----------------------------------------------------------------------
050    /**
051     * Returns a lookup which always returns null.
052     *
053     * @return a lookup that always returns null, not null
054     */
055    public static StrLookup<?> noneLookup() {
056        return NONE_LOOKUP;
057    }
058
059    /**
060     * Returns a new lookup which uses a copy of the current
061     * {@link System#getProperties() System properties}.
062     * <p>
063     * If a security manager blocked access to system properties, then null will
064     * be returned from every lookup.
065     * <p>
066     * If a null key is used, this lookup will throw a NullPointerException.
067     *
068     * @return a lookup using system properties, not null
069     */
070    public static StrLookup<String> systemPropertiesLookup() {
071        return SYSTEM_PROPERTIES_LOOKUP;
072    }
073
074    /**
075     * Returns a lookup which looks up values using a map.
076     * <p>
077     * If the map is null, then null will be returned from every lookup.
078     * The map result object is converted to a string using toString().
079     *
080     * @param <V> the type of the values supported by the lookup
081     * @param map  the map of keys to values, may be null
082     * @return a lookup using the map, not null
083     */
084    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
085        return new MapStrLookup<>(map);
086    }
087
088    //-----------------------------------------------------------------------
089    /**
090     * Constructor.
091     */
092    protected StrLookup() {
093        super();
094    }
095
096    /**
097     * Looks up a String key to a String value.
098     * <p>
099     * The internal implementation may use any mechanism to return the value.
100     * The simplest implementation is to use a Map. However, virtually any
101     * implementation is possible.
102     * <p>
103     * For example, it would be possible to implement a lookup that used the
104     * key as a primary key, and looked up the value on demand from the database
105     * Or, a numeric based implementation could be created that treats the key
106     * as an integer, increments the value and return the result as a string -
107     * converting 1 to 2, 15 to 16 etc.
108     * <p>
109     * The {@link #lookup(String)} method always returns a String, regardless of
110     * the underlying data, by converting it as necessary. For example:
111     * <pre>
112     * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
113     * map.put("number", Integer.valueOf(2));
114     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
115     * </pre>
116     * @param key  the key to be looked up, may be null
117     * @return the matching value, null if no match
118     */
119    public abstract String lookup(String key);
120
121    //-----------------------------------------------------------------------
122    /**
123     * Lookup implementation that uses a Map.
124     */
125    static class MapStrLookup<V> extends StrLookup<V> {
126
127        /** Map keys are variable names and value. */
128        private final Map<String, V> map;
129
130        /**
131         * Creates a new instance backed by a Map.
132         *
133         * @param map  the map of keys to values, may be null
134         */
135        MapStrLookup(final Map<String, V> map) {
136            this.map = map;
137        }
138
139        /**
140         * Looks up a String key to a String value using the map.
141         * <p>
142         * If the map is null, then null is returned.
143         * The map result object is converted to a string using toString().
144         *
145         * @param key  the key to be looked up, may be null
146         * @return the matching value, null if no match
147         */
148        @Override
149        public String lookup(final String key) {
150            if (map == null) {
151                return null;
152            }
153            final Object obj = map.get(key);
154            if (obj == null) {
155                return null;
156            }
157            return obj.toString();
158        }
159    }
160
161    //-----------------------------------------------------------------------
162    /**
163     * Lookup implementation based on system properties.
164     */
165    private static class SystemPropertiesStrLookup extends StrLookup<String> {
166        /**
167         * {@inheritDoc} This implementation directly accesses system properties.
168         */
169        @Override
170        public String lookup(final String key) {
171            if (key.length() > 0) {
172                try {
173                    return System.getProperty(key);
174                } catch (final SecurityException scex) {
175                    // Squelched. All lookup(String) will return null.
176                    return null;
177                }
178            }
179            return null;
180        }
181    }
182}