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