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.lang.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 * @author Stephen Colebourne
35 * @since 2.2
36 * @version $Id: StrLookup.java 592077 2007-11-05 16:47:10Z mbenson $
37 */
38 public abstract class StrLookup {
39
40 /**
41 * Lookup that always returns null.
42 */
43 private static final StrLookup NONE_LOOKUP;
44 /**
45 * Lookup that uses System properties.
46 */
47 private static final StrLookup SYSTEM_PROPERTIES_LOOKUP;
48 static {
49 NONE_LOOKUP = new MapStrLookup(null);
50 StrLookup lookup = null;
51 try {
52 lookup = new MapStrLookup(System.getProperties());
53 } catch (SecurityException ex) {
54 lookup = NONE_LOOKUP;
55 }
56 SYSTEM_PROPERTIES_LOOKUP = lookup;
57 }
58
59 //-----------------------------------------------------------------------
60 /**
61 * Returns a lookup which always returns null.
62 *
63 * @return a lookup that always returns null, not null
64 */
65 public static StrLookup noneLookup() {
66 return NONE_LOOKUP;
67 }
68
69 /**
70 * Returns a lookup which uses {@link System#getProperties() System properties}
71 * to lookup the key to value.
72 * <p>
73 * If a security manager blocked access to system properties, then null will
74 * be returned from every lookup.
75 * <p>
76 * If a null key is used, this lookup will throw a NullPointerException.
77 *
78 * @return a lookup using system properties, not null
79 */
80 public static StrLookup 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 *
90 * @param map the map of keys to values, may be null
91 * @return a lookup using the map, not null
92 */
93 public static StrLookup mapLookup(Map map) {
94 return new MapStrLookup(map);
95 }
96
97 //-----------------------------------------------------------------------
98 /**
99 * Constructor.
100 */
101 protected StrLookup() {
102 super();
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 * For example, it would be possible to implement a lookup that used the
113 * key as a primary key, and looked up the value on demand from the database
114 * Or, a numeric based implementation could be created that treats the key
115 * as an integer, increments the value and return the result as a string -
116 * converting 1 to 2, 15 to 16 etc.
117 *
118 * @param key the key to be looked up, may be null
119 * @return the matching value, null if no match
120 */
121 public abstract String lookup(String key);
122
123 //-----------------------------------------------------------------------
124 /**
125 * Lookup imnplementation that uses a Map.
126 */
127 static class MapStrLookup extends StrLookup {
128
129 /** Map keys are variable names and value. */
130 private final Map map;
131
132 /**
133 * Creates a new instance backed by a Map.
134 *
135 * @param map the map of keys to values, may be null
136 */
137 MapStrLookup(Map map) {
138 this.map = map;
139 }
140
141 /**
142 * Looks up a String key to a String value using the map.
143 * <p>
144 * If the map is null, then null is returned.
145 * The map result object is converted to a string using toString().
146 *
147 * @param key the key to be looked up, may be null
148 * @return the matching value, null if no match
149 */
150 public String lookup(String key) {
151 if (map == null) {
152 return null;
153 }
154 Object obj = map.get(key);
155 if (obj == null) {
156 return null;
157 }
158 return obj.toString();
159 }
160 }
161 }