View Javadoc
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  import java.util.Objects;
21  
22  import org.apache.commons.lang3.SystemProperties;
23  
24  /**
25   * Lookup a String key to a String value.
26   * <p>
27   * This class represents the simplest form of a string to string map.
28   * It has a benefit over a map in that it can create the result on
29   * demand based on the key.
30   * </p>
31   * <p>
32   * This class comes complete with various factory methods.
33   * If these do not suffice, you can subclass and implement your own matcher.
34   * </p>
35   * <p>
36   * For example, it would be possible to implement a lookup that used the
37   * key as a primary key, and looked up the value on demand from the database.
38   * </p>
39   *
40   * @param <V> Unused.
41   * @since 2.2
42   * @deprecated As of 3.6, use Apache Commons Text
43   * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/lookup/StringLookupFactory.html">
44   * StringLookupFactory</a> instead
45   */
46  @Deprecated
47  public abstract class StrLookup<V> {
48  
49      /**
50       * Lookup implementation that uses a Map.
51       *
52       * @param <V> the type of mapped values.
53       */
54      static class MapStrLookup<V> extends StrLookup<V> {
55  
56          /** Map keys are variable names and value. */
57          private final Map<String, V> map;
58  
59          /**
60           * Creates a new instance backed by a Map.
61           *
62           * @param map  the map of keys to values, may be null
63           */
64          MapStrLookup(final Map<String, V> map) {
65              this.map = map;
66          }
67  
68          /**
69           * Looks up a String key to a String value using the map.
70           * <p>
71           * If the map is null, then null is returned.
72           * The map result object is converted to a string using toString().
73           * </p>
74           *
75           * @param key  the key to be looked up, may be null
76           * @return the matching value, null if no match
77           */
78          @Override
79          public String lookup(final String key) {
80              if (map == null) {
81                  return null;
82              }
83              return Objects.toString(map.get(key), null);
84          }
85      }
86  
87      /**
88       * Lookup implementation based on system properties.
89       */
90      private static final class SystemPropertiesStrLookup extends StrLookup<String> {
91          /**
92           * {@inheritDoc} This implementation directly accesses system properties.
93           */
94          @Override
95          public String lookup(final String key) {
96              return SystemProperties.getProperty(key);
97          }
98      }
99  
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      * Constructor.
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>
176      * Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
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 }