    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         * {@inheritDoc} This implementation directly accesses system properties.
    093         */
    094        @Override
    095        public String lookup(final String key) {
    096            return SystemProperties.getProperty(key);
    097        }
    098    }
    099
    100    /**
    101     * Lookup that always returns null.
    102     */
    103    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
    104
    105    /**
    106     * Lookup based on system properties.
    107     */
    108    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
    109
    110    /**
    111     * Returns a lookup which looks up values using a map.
    112     * <p>
    113     * If the map is null, then null will be returned from every lookup.
    114     * The map result object is converted to a string using toString().
    115     * </p>
    116     *
    117     * @param <V> the type of the values supported by the lookup.
    118     * @param map  the map of keys to values, may be null.
    119     * @return a lookup using the map, not null.
    120     */
    121    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
    122        return new MapStrLookup<>(map);
    123    }
    124
    125    /**
    126     * Returns a lookup which always returns null.
    127     *
    128     * @return a lookup that always returns null, not null.
    129     */
    130    public static StrLookup<?> noneLookup() {
    131        return NONE_LOOKUP;
    132    }
    133
    134    /**
    135     * Returns a new lookup which uses a copy of the current
    136     * {@link System#getProperties() System properties}.
    137     * <p>
    138     * If a security manager blocked access to system properties, then null will
    139     * be returned from every lookup.
    140     * </p>
    141     * <p>
    142     * If a null key is used, this lookup will throw a NullPointerException.
    143     * </p>
    144     *
    145     * @return a lookup using system properties, not null.
    146     */
    147    public static StrLookup<String> systemPropertiesLookup() {
    148        return SYSTEM_PROPERTIES_LOOKUP;
    149    }
    150
    151    /**
    152     * Constructs a new instance.
    153     */
    154    protected StrLookup() {
    155    }
    156
    157    /**
    158     * Looks up a String key to a String value.
    159     * <p>
    160     * The internal implementation may use any mechanism to return the value.
    161     * The simplest implementation is to use a Map. However, virtually any
    162     * implementation is possible.
    163     * </p>
    164     * <p>
    165     * For example, it would be possible to implement a lookup that used the
    166     * key as a primary key, and looked up the value on demand from the database
    167     * Or, a numeric based implementation could be created that treats the key
    168     * as an integer, increments the value and return the result as a string -
    169     * converting 1 to 2, 15 to 16 etc.
    170     * </p>
    171     * <p>
    172     * The {@link #lookup(String)} method always returns a String, regardless of
    173     * the underlying data, by converting it as necessary. For example:
    174     * </p>
    175     * <pre>{@code
    176     * Map<String, Object> map = new HashMap<String, Object>();
    177     * map.put("number", Integer.valueOf(2));
    178     * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
    179     * }</pre>
    180     * @param key  the key to be looked up, may be null.
    181     * @return the matching value, null if no match.
    182     */
    183    public abstract String lookup(String key);
    184}