• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.commons.lang3.text;
18 
19 import java.util.Map;
20 
21 import org.apache.commons.lang3.SystemProperties;
22 
23 /**
24  * Lookup a String key to a String value.
25  * <p>
26  * This class represents the simplest form of a string to string map.
27  * It has a benefit over a map in that it can create the result on
28  * demand based on the key.
29  * </p>
30  * <p>
31  * This class comes complete with various factory methods.
32  * If these do not suffice, you can subclass and implement your own matcher.
33  * </p>
34  * <p>
35  * For example, it would be possible to implement a lookup that used the
36  * key as a primary key, and looked up the value on demand from the database.
37  * </p>
38  *
39  * @param <V> Unused.
40  * @since 2.2
41  * @deprecated As of 3.6, use Apache Commons Text
42  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/lookup/StringLookupFactory.html">
43  * StringLookupFactory</a> instead
44  */
45 @Deprecated
46 public abstract class StrLookup<V> {
47 
48     /**
49      * Lookup that always returns null.
50      */
51     private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
52 
53     /**
54      * Lookup based on system properties.
55      */
56     private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
57 
58     /**
59      * Returns a lookup which always returns null.
60      *
61      * @return a lookup that always returns null, not null
62      */
noneLookup()63     public static StrLookup<?> noneLookup() {
64         return NONE_LOOKUP;
65     }
66 
67     /**
68      * Returns a new lookup which uses a copy of the current
69      * {@link System#getProperties() System properties}.
70      * <p>
71      * If a security manager blocked access to system properties, then null will
72      * be returned from every lookup.
73      * </p>
74      * <p>
75      * If a null key is used, this lookup will throw a NullPointerException.
76      * </p>
77      *
78      * @return a lookup using system properties, not null
79      */
systemPropertiesLookup()80     public static StrLookup<String> systemPropertiesLookup() {
81         return SYSTEM_PROPERTIES_LOOKUP;
82     }
83 
84     /**
85      * Returns a lookup which looks up values using a map.
86      * <p>
87      * If the map is null, then null will be returned from every lookup.
88      * The map result object is converted to a string using toString().
89      * </p>
90      *
91      * @param <V> the type of the values supported by the lookup
92      * @param map  the map of keys to values, may be null
93      * @return a lookup using the map, not null
94      */
mapLookup(final Map<String, V> map)95     public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
96         return new MapStrLookup<>(map);
97     }
98 
99     /**
100      * Constructor.
101      */
StrLookup()102     protected StrLookup() {
103     }
104 
105     /**
106      * Looks up a String key to a String value.
107      * <p>
108      * The internal implementation may use any mechanism to return the value.
109      * The simplest implementation is to use a Map. However, virtually any
110      * implementation is possible.
111      * </p>
112      * <p>
113      * For example, it would be possible to implement a lookup that used the
114      * key as a primary key, and looked up the value on demand from the database
115      * Or, a numeric based implementation could be created that treats the key
116      * as an integer, increments the value and return the result as a string -
117      * converting 1 to 2, 15 to 16 etc.
118      * </p>
119      * <p>
120      * The {@link #lookup(String)} method always returns a String, regardless of
121      * the underlying data, by converting it as necessary. For example:
122      * </p>
123      * <pre>
124      * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
125      * map.put("number", Integer.valueOf(2));
126      * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
127      * </pre>
128      * @param key  the key to be looked up, may be null
129      * @return the matching value, null if no match
130      */
lookup(String key)131     public abstract String lookup(String key);
132 
133     /**
134      * Lookup implementation that uses a Map.
135      *
136      * @param <V> the type of mapped values.
137      */
138     static class MapStrLookup<V> extends StrLookup<V> {
139 
140         /** Map keys are variable names and value. */
141         private final Map<String, V> map;
142 
143         /**
144          * Creates a new instance backed by a Map.
145          *
146          * @param map  the map of keys to values, may be null
147          */
MapStrLookup(final Map<String, V> map)148         MapStrLookup(final Map<String, V> map) {
149             this.map = map;
150         }
151 
152         /**
153          * Looks up a String key to a String value using the map.
154          * <p>
155          * If the map is null, then null is returned.
156          * The map result object is converted to a string using toString().
157          * </p>
158          *
159          * @param key  the key to be looked up, may be null
160          * @return the matching value, null if no match
161          */
162         @Override
lookup(final String key)163         public String lookup(final String key) {
164             if (map == null) {
165                 return null;
166             }
167             final Object obj = map.get(key);
168             if (obj == null) {
169                 return null;
170             }
171             return obj.toString();
172         }
173     }
174 
175     /**
176      * Lookup implementation based on system properties.
177      */
178     private static class SystemPropertiesStrLookup extends StrLookup<String> {
179         /**
180          * {@inheritDoc} This implementation directly accesses system properties.
181          */
182         @Override
lookup(final String key)183         public String lookup(final String key) {
184             return SystemProperties.getProperty(key);
185         }
186     }
187 }
188