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 *      https://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;
020import java.util.Objects;
021
022import org.apache.commons.lang3.SystemProperties;
023
024/**
025 * Lookup a String key to a String value.
026 * <p>
027 * This class represents the simplest form of a string to string map.
028 * It has a benefit over a map in that it can create the result on
029 * demand based on the key.
030 * </p>
031 * <p>
032 * This class comes complete with various factory methods.
033 * If these do not suffice, you can subclass and implement your own matcher.
034 * </p>
035 * <p>
036 * For example, it would be possible to implement a lookup that used the
037 * key as a primary key, and looked up the value on demand from the database.
038 * </p>
039 *
040 * @param <V> Unused.
041 * @since 2.2
042 * @deprecated As of <a href="https://commons.apache.org/proper/commons-lang/changes-report.html#a3.6">3.6</a>, use Apache Commons Text
043 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/lookup/StringLookupFactory.html">
044 * StringLookupFactory</a>.
045 */
046@Deprecated
047public abstract class StrLookup<V> {
048
049    /**
050     * Lookup implementation that uses a Map.
051     *
052     * @param <V> the type of mapped values.
053     */
054    private static final class MapStrLookup<V> extends StrLookup<V> {
055
056        /** Map keys are variable names and value. */
057        private final Map<String, V> map;
058
059        /**
060         * Creates a new instance backed by a Map.
061         *
062         * @param map  the map of keys to values, may be null.
063         */
064        MapStrLookup(final Map<String, V> map) {
065            this.map = map;
066        }
067
068        /**
069         * Looks up a String key to a String value using the map.
070         * <p>
071         * If the map is null, then null is returned.
072         * The map result object is converted to a string using toString().
073         * </p>
074         *
075         * @param key  the key to be looked up, may be null.
076         * @return the matching value, null if no match.
077         */
078        @Override
079        public String lookup(final String key) {
080            if (map == null) {
081                return null;
082            }
083            return Objects.toString(map.get(key), null);
084        }
085    }
086
087    /**
088     * Lookup implementation based on system properties.
089     */
090    private static final class SystemPropertiesStrLookup extends StrLookup<String> {
091
092        /**
093         * {@inheritDoc} This implementation directly accesses system properties.
094         */
095        @Override
096        public String lookup(final String key) {
097            return SystemProperties.getProperty(key);
098        }
099    }
100
101    /**
102     * Lookup that always returns null.
103     */
104    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
105
106    /**
107     * Lookup based on system properties.
108     */
109    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
110
111    /**
112     * Returns a lookup which looks up values using a map.
113     * <p>
114     * If the map is null, then null will be returned from every lookup.
115     * The map result object is converted to a string using toString().
116     * </p>
117     *
118     * @param <V> the type of the values supported by the lookup.
119     * @param map  the map of keys to values, may be null.
120     * @return a lookup using the map, not null.
121     */
122    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
123        return new MapStrLookup<>(map);
124    }
125
126    /**
127     * Returns a lookup which always returns null.
128     *
129     * @return a lookup that always returns null, not null.
130     */
131    public static StrLookup<?> noneLookup() {
132        return NONE_LOOKUP;
133    }
134
135    /**
136     * Returns a new lookup which uses a copy of the current
137     * {@link System#getProperties() System properties}.
138     * <p>
139     * If a security manager blocked access to system properties, then null will
140     * be returned from every lookup.
141     * </p>
142     * <p>
143     * If a null key is used, this lookup will throw a NullPointerException.
144     * </p>
145     *
146     * @return a lookup using system properties, not null.
147     */
148    public static StrLookup<String> systemPropertiesLookup() {
149        return SYSTEM_PROPERTIES_LOOKUP;
150    }
151
152    /**
153     * Constructs a new instance.
154     */
155    protected StrLookup() {
156    }
157
158    /**
159     * Looks up a String key to a String value.
160     * <p>
161     * The internal implementation may use any mechanism to return the value.
162     * The simplest implementation is to use a Map. However, virtually any
163     * implementation is possible.
164     * </p>
165     * <p>
166     * For example, it would be possible to implement a lookup that used the
167     * key as a primary key, and looked up the value on demand from the database
168     * Or, a numeric based implementation could be created that treats the key
169     * as an integer, increments the value and return the result as a string -
170     * converting 1 to 2, 15 to 16 etc.
171     * </p>
172     * <p>
173     * The {@link #lookup(String)} method always returns a String, regardless of
174     * the underlying data, by converting it as necessary. For example:
175     * </p>
176     * <pre>{@code
177     * Map<String, Object> map = new HashMap<String, Object>();
178     * map.put("number", Integer.valueOf(2));
179     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
180     * }</pre>
181     *
182     * @param key  the key to be looked up, may be null.
183     * @return the matching value, null if no match.
184     */
185    public abstract String lookup(String key);
186}