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 agents.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 Apache Software Foundation
|
---|
35 | * @since 2.2
|
---|
36 | * @version $Id: StrLookup.java 905636 2010-02-02 14:03:32Z niallp $
|
---|
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 | * <p>
|
---|
118 | * The {@link #lookup(String)} method always returns a String, regardless of
|
---|
119 | * the underlying data, by converting it as necessary. For example:
|
---|
120 | * <pre>
|
---|
121 | * Map map = new HashMap();
|
---|
122 | * map.put("number", new Integer(2));
|
---|
123 | * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
|
---|
124 | * </pre>
|
---|
125 | * @param key the key to be looked up, may be null
|
---|
126 | * @return the matching value, null if no match
|
---|
127 | */
|
---|
128 | public abstract String lookup(String key);
|
---|
129 |
|
---|
130 | //-----------------------------------------------------------------------
|
---|
131 | /**
|
---|
132 | * Lookup implementation that uses a Map.
|
---|
133 | */
|
---|
134 | static class MapStrLookup extends StrLookup {
|
---|
135 |
|
---|
136 | /** Map keys are variable names and value. */
|
---|
137 | private final Map map;
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Creates a new instance backed by a Map.
|
---|
141 | *
|
---|
142 | * @param map the map of keys to values, may be null
|
---|
143 | */
|
---|
144 | MapStrLookup(Map map) {
|
---|
145 | this.map = map;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Looks up a String key to a String value using the map.
|
---|
150 | * <p>
|
---|
151 | * If the map is null, then null is returned.
|
---|
152 | * The map result object is converted to a string using toString().
|
---|
153 | *
|
---|
154 | * @param key the key to be looked up, may be null
|
---|
155 | * @return the matching value, null if no match
|
---|
156 | */
|
---|
157 | public String lookup(String key) {
|
---|
158 | if (map == null) {
|
---|
159 | return null;
|
---|
160 | }
|
---|
161 | Object obj = map.get(key);
|
---|
162 | if (obj == null) {
|
---|
163 | return null;
|
---|
164 | }
|
---|
165 | return obj.toString();
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|