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.text;
18  
19  import java.util.Map;
20  
21  /**
22   * Lookup a String key to a String value.
23   * <p>
24   * This class represents the simplest form of a string to string map.
25   * It has a benefit over a map in that it can create the result on
26   * demand based on the key.
27   * <p>
28   * This class comes complete with various factory methods.
29   * If these do not suffice, you can subclass and implement your own matcher.
30   * <p>
31   * For example, it would be possible to implement a lookup that used the
32   * key as a primary key, and looked up the value on demand from the database
33   *
34   * @since 1.0
35   */
36  public abstract class StrLookup<V> {
37  
38      /**
39       * Lookup that always returns null.
40       */
41      private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
42  
43      /**
44       * Lookup based on system properties.
45       */
46      private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
47  
48      //-----------------------------------------------------------------------
49      /**
50       * Returns a lookup which always returns null.
51       *
52       * @return a lookup that always returns null, not null
53       */
54      public static StrLookup<?> noneLookup() {
55          return NONE_LOOKUP;
56      }
57  
58      /**
59       * Returns a new lookup which uses a copy of the current
60       * {@link System#getProperties() System properties}.
61       * <p>
62       * If a security manager blocked access to system properties, then null will
63       * be returned from every lookup.
64       * <p>
65       * If a null key is used, this lookup will throw a NullPointerException.
66       *
67       * @return a lookup using system properties, not null
68       */
69      public static StrLookup<String> systemPropertiesLookup() {
70          return SYSTEM_PROPERTIES_LOOKUP;
71      }
72  
73      /**
74       * Returns a lookup which looks up values using a map.
75       * <p>
76       * If the map is null, then null will be returned from every lookup.
77       * The map result object is converted to a string using toString().
78       *
79       * @param <V> the type of the values supported by the lookup
80       * @param map  the map of keys to values, may be null
81       * @return a lookup using the map, not null
82       */
83      public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
84          return new MapStrLookup<>(map);
85      }
86  
87      //-----------------------------------------------------------------------
88      /**
89       * Constructor.
90       */
91      protected StrLookup() {
92          super();
93      }
94  
95      /**
96       * Looks up a String key to a String value.
97       * <p>
98       * The internal implementation may use any mechanism to return the value.
99       * 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(final String key) {
170             if (key.length() > 0) {
171                 try {
172                     return System.getProperty(key);
173                 } catch (final SecurityException scex) {
174                     // Squelched. All lookup(String) will return null.
175                 }
176             }
177             return null;
178         }
179     }
180 }