KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > collection > MapAccessor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.collection;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.springframework.util.Assert;
22
23 /**
24  * A simple, generic decorator for getting attributes out of a map. May be
25  * instantiated directly or used as a base class as a convenience.
26  *
27  * @author Keith Donald
28  */

29 public class MapAccessor implements MapAdaptable {
30
31     /**
32      * The target map.
33      */

34     private Map JavaDoc map;
35
36     /**
37      * Creates a new attribute map accessor.
38      * @param map the map
39      */

40     public MapAccessor(Map JavaDoc map) {
41         Assert.notNull(map, "The map to decorate is required");
42         this.map = map;
43     }
44
45     // implementing MapAdaptable
46

47     public Map JavaDoc asMap() {
48         return map;
49     }
50
51     /**
52      * Returns a value in the map, returning the defaultValue if no value was
53      * found.
54      * @param key the key
55      * @param defaultValue the default
56      * @return the attribute value
57      */

58     public Object JavaDoc get(Object JavaDoc key, Object JavaDoc defaultValue) {
59         if (!map.containsKey(key)) {
60             return defaultValue;
61         }
62         return map.get(key);
63     }
64
65     /**
66      * Returns a value in the map, asserting it is of the required type if
67      * present and returning <code>null</code> if not found.
68      * @param key the key
69      * @param requiredType the required type
70      * @return the value
71      * @throws IllegalArgumentException if the key is present but the value is
72      * not of the required type
73      */

74     public Object JavaDoc get(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
75         return get(key, requiredType, null);
76     }
77
78     /**
79      * Returns a value in the map of the specified type, returning the
80      * defaultValue if no value is found.
81      * @param key the key
82      * @param requiredType the required type
83      * @param defaultValue the default
84      * @return the attribute value
85      * @throws IllegalArgumentException if the key is present but the value is
86      * not of the required type
87      */

88     public Object JavaDoc get(Object JavaDoc key, Class JavaDoc requiredType, Object JavaDoc defaultValue) {
89         if (!map.containsKey(key)) {
90             return defaultValue;
91         }
92         return assertKeyValueOfType(key, requiredType);
93     }
94
95     /**
96      * Returns a value in the map, throwing an exception if the attribute is not
97      * present and of the correct type.
98      * @param key the key
99      * @return the value
100      */

101     public Object JavaDoc getRequired(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
102         assertContainsKey(key);
103         return map.get(key);
104     }
105
106     /**
107      * Returns an value in the map, asserting it is present and of the required
108      * type.
109      * @param key the key
110      * @param requiredType the required type
111      * @return the value
112      */

113     public Object JavaDoc getRequired(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
114         assertContainsKey(key);
115         return assertKeyValueOfType(key, requiredType);
116     }
117
118     /**
119      * Returns a string value in the map, returning <code>null</code> if no
120      * value was found.
121      * @param key the key
122      * @return the string value
123      * @throws IllegalArgumentException if the key is present but the value is
124      * not a string
125      */

126     public String JavaDoc getString(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
127         return getString(key, null);
128     }
129
130     /**
131      * Returns a string value in the map, returning the defaultValue if no value
132      * was found.
133      * @param key the key
134      * @param defaultValue the default
135      * @return the string value
136      * @throws IllegalArgumentException if the key is present but the value is
137      * not a string
138      */

139     public String JavaDoc getString(Object JavaDoc key, String JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
140         if (!map.containsKey(key)) {
141             return defaultValue;
142         }
143         return (String JavaDoc)assertKeyValueOfType(key, String JavaDoc.class);
144     }
145
146     /**
147      * Returns a string value in the map, throwing an exception if the attribute
148      * is not present and of the correct type.
149      * @param key the key
150      * @return the string value
151      * @throws IllegalArgumentException if the key is not present or present but
152      * the value is not a string
153      */

154     public String JavaDoc getRequiredString(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
155         assertContainsKey(key);
156         return (String JavaDoc)assertKeyValueOfType(key, String JavaDoc.class);
157     }
158
159     /**
160      * Returns a collection value in the map, returning <code>null</code> if
161      * no value was found.
162      * @param key the key
163      * @return the collection value
164      * @throws IllegalArgumentException if the key is present but the value is
165      * not a collection
166      */

167     public Collection JavaDoc getCollection(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
168         if (!map.containsKey(key)) {
169             return null;
170         }
171         return (Collection JavaDoc)assertKeyValueOfType(key, Collection JavaDoc.class);
172     }
173
174     /**
175      * Returns a collection value in the map, asserting it is of the required
176      * type if present and returning <code>null</code> if not found.
177      * @param key the key
178      * @return the collection value
179      * @throws IllegalArgumentException if the key is present but the value is
180      * not a collection
181      */

182     public Collection JavaDoc getCollection(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
183         if (!map.containsKey(key)) {
184             return null;
185         }
186         assertAssignableTo(Collection JavaDoc.class, requiredType);
187         return (Collection JavaDoc)assertKeyValueOfType(key, requiredType);
188     }
189
190     /**
191      * Returns a collection value in the map, throwing an exception if not
192      * found.
193      * @param key the key
194      * @return the collection value
195      * @throws IllegalArgumentException if the key is not present or present but
196      * the value is not a collection
197      */

198     public Collection JavaDoc getRequiredCollection(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
199         assertContainsKey(key);
200         return (Collection JavaDoc)assertKeyValueOfType(key, Collection JavaDoc.class);
201     }
202
203     /**
204      * Returns a collection value in the map, asserting it is of the required
205      * type if present and throwing an exception if not found.
206      * @param key the key
207      * @return the collection value
208      * @throws IllegalArgumentException if the key is not present or present but
209      * the value is not a collection of the required type
210      */

211     public Collection JavaDoc getRequiredCollection(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
212         assertContainsKey(key);
213         assertAssignableTo(Collection JavaDoc.class, requiredType);
214         return (Collection JavaDoc)assertKeyValueOfType(key, requiredType);
215     }
216
217     /**
218      * Returns a array value in the map, asserting it is of the required type if
219      * present and returning <code>null</code> if not found.
220      * @param key the key
221      * @return the array value
222      * @throws IllegalArgumentException if the key is present but the value is
223      * not an array of the required type
224      */

225     public Object JavaDoc[] getArray(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
226         assertAssignableTo(Object JavaDoc[].class, requiredType);
227         if (!map.containsKey(key)) {
228             return null;
229         }
230         return (Object JavaDoc[])assertKeyValueOfType(key, requiredType);
231     }
232
233     /**
234      * Returns an array value in the map, asserting it is of the required type
235      * if present and throwing an exception if not found.
236      * @param key the key
237      * @return the array value
238      * @throws IllegalArgumentException if the key is not present or present but
239      * the value is not a array of the required type
240      */

241     public Object JavaDoc[] getRequiredArray(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
242         assertContainsKey(key);
243         assertAssignableTo(Object JavaDoc[].class, requiredType);
244         return (Object JavaDoc[])assertKeyValueOfType(key, requiredType);
245     }
246
247     /**
248      * Returns a number value in the map that is of the specified type,
249      * returning <code>null</code> if no value was found.
250      * @param key the key
251      * @param requiredType the required number type
252      * @return the numbervalue
253      * @throws IllegalArgumentException if the key is present but the value is
254      * not a number of the required type
255      */

256     public Number JavaDoc getNumber(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
257         return getNumber(key, requiredType, null);
258     }
259
260     /**
261      * Returns a number attribute value in the map of the specified type,
262      * returning the defaultValue if no value was found.
263      * @param key the attribute name
264      * @return the number value
265      * @param defaultValue the default
266      * @throws IllegalArgumentException if the key is present but the value is
267      * not a number of the required type
268      */

269     public Number JavaDoc getNumber(Object JavaDoc key, Class JavaDoc requiredType, Number JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
270         if (!map.containsKey(key)) {
271             return defaultValue;
272         }
273         assertAssignableTo(Number JavaDoc.class, requiredType);
274         return (Number JavaDoc)assertKeyValueOfType(key, requiredType);
275     }
276
277     /**
278      * Returns a number value in the map, throwing an exception if the attribute
279      * is not present and of the correct type.
280      * @param key the key
281      * @return the number value
282      * @throws IllegalArgumentException if the key is not present or present but
283      * the value is not a number of the required type
284      */

285     public Number JavaDoc getRequiredNumber(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
286         assertContainsKey(key);
287         return (Number JavaDoc)assertKeyValueOfType(key, requiredType);
288     }
289
290     /**
291      * Returns an integer value in the map, returning <code>null</code> if no
292      * value was found.
293      * @param key the key
294      * @return the integer value
295      * @throws IllegalArgumentException if the key is present but the value is
296      * not an integer
297      */

298     public Integer JavaDoc getInteger(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
299         return getInteger(key, null);
300     }
301
302     /**
303      * Returns an integer value in the map, returning the defaultValue if no
304      * value was found.
305      * @param key the key
306      * @param defaultValue the default
307      * @return the integer value
308      * @throws IllegalArgumentException if the key is present but the value is
309      * not an integer
310      */

311     public Integer JavaDoc getInteger(Object JavaDoc key, Integer JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
312         return (Integer JavaDoc)getNumber(key, Integer JavaDoc.class, defaultValue);
313     }
314
315     /**
316      * Returns an integer value in the map, throwing an exception if the value
317      * is not present and of the correct type.
318      * @param key the attribute name
319      * @return the integer attribute value
320      * @throws IllegalArgumentException if the key is not present or present but
321      * the value is not an integer
322      */

323     public Integer JavaDoc getRequiredInteger(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
324         return (Integer JavaDoc)getRequiredNumber(key, Integer JavaDoc.class);
325     }
326
327     /**
328      * Returns a long value in the map, returning <code>null</code> if no
329      * value was found.
330      * @param key the key
331      * @return the long value
332      * @throws IllegalArgumentException if the key is present but not a long
333      */

334     public Long JavaDoc getLong(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
335         return getLong(key, null);
336     }
337
338     /**
339      * Returns a long value in the map, returning the defaultValue if no value
340      * was found.
341      * @param key the key
342      * @param defaultValue the default
343      * @return the long attribute value
344      * @throws IllegalArgumentException if the key is present but the value is
345      * not a long
346      */

347     public Long JavaDoc getLong(Object JavaDoc key, Long JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
348         return (Long JavaDoc)getNumber(key, Long JavaDoc.class, defaultValue);
349     }
350
351     /**
352      * Returns a long value in the map, throwing an exception if the value is
353      * not present and of the correct type.
354      * @param key the key
355      * @return the long attribute value
356      * @throws IllegalArgumentException if the key is not present or present but
357      * the value is not a long
358      */

359     public Long JavaDoc getRequiredLong(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
360         return (Long JavaDoc)getRequiredNumber(key, Long JavaDoc.class);
361     }
362
363     /**
364      * Returns a boolean value in the map, returning <code>null</code> if no
365      * value was found.
366      * @param key the key
367      * @return the boolean value
368      * @throws IllegalArgumentException if the key is present but the value is
369      * not a boolean
370      */

371     public Boolean JavaDoc getBoolean(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
372         return getBoolean(key, null);
373     }
374
375     /**
376      * Returns a boolean value in the map, returning the defaultValue if no
377      * value was found.
378      * @param key the key
379      * @param defaultValue the default
380      * @return the boolean value
381      * @throws IllegalArgumentException if the key is present but the value is
382      * not a boolean
383      */

384     public Boolean JavaDoc getBoolean(Object JavaDoc key, Boolean JavaDoc defaultValue) throws IllegalArgumentException JavaDoc {
385         if (!map.containsKey(key)) {
386             return defaultValue;
387         }
388         return (Boolean JavaDoc)assertKeyValueOfType(key, Boolean JavaDoc.class);
389     }
390
391     /**
392      * Returns a boolean value in the map, throwing an exception if the value is
393      * not present and of the correct type.
394      * @param key the attribute
395      * @return the boolean value
396      * @throws IllegalArgumentException if the key is not present or present but
397      * the value is not a boolean
398      */

399     public Boolean JavaDoc getRequiredBoolean(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
400         assertContainsKey(key);
401         return (Boolean JavaDoc)assertKeyValueOfType(key, Boolean JavaDoc.class);
402     }
403
404     /**
405      * Asserts that the attribute is present in the attribute map.
406      * @param key the key
407      * @throws IllegalArgumentException if the key is not present
408      */

409     public void assertContainsKey(Object JavaDoc key) throws IllegalArgumentException JavaDoc {
410         if (!map.containsKey(key)) {
411             throw new IllegalArgumentException JavaDoc("Required attribute '" + key
412                     + "' is not present in map; attributes present are [" + asMap() + "]");
413         }
414     }
415
416     /**
417      * Indicates if the attribute is present in the attribute map and of the
418      * required type.
419      * @param key the attribute name
420      * @return true if present and of the required type, false if not present.
421      */

422     public boolean containsKey(Object JavaDoc key, Class JavaDoc requiredType) throws IllegalArgumentException JavaDoc {
423         if (map.containsKey(key)) {
424             assertKeyValueOfType(key, requiredType);
425             return true;
426         }
427         else {
428             return false;
429         }
430     }
431
432     /**
433      * Assert that value of the mak key is of the required type.
434      * @param key the attribute name
435      * @param requiredType the required attribute value type
436      * @return the attribute value
437      */

438     public Object JavaDoc assertKeyValueOfType(Object JavaDoc key, Class JavaDoc requiredType) {
439         return assertKeyValueInstanceOf(key, map.get(key), requiredType);
440     }
441
442     /**
443      * Assert that the key value is an instance of the required type.
444      * @param key the key
445      * @param value the value
446      * @param requiredType the required type
447      * @return the value
448      */

449     public Object JavaDoc assertKeyValueInstanceOf(Object JavaDoc key, Object JavaDoc value, Class JavaDoc requiredType) {
450         Assert.notNull(requiredType, "The required type to assert is required");
451         if (!requiredType.isInstance(value)) {
452             throw new IllegalArgumentException JavaDoc("Map key '" + key + "' has value [" + value
453                     + "] that is not of expected type [" + requiredType + "], instead it is of type ["
454                     + (value != null ? value.getClass().getName() : "null") + "]");
455         }
456         return value;
457     }
458
459     private void assertAssignableTo(Class JavaDoc clazz, Class JavaDoc requiredType) {
460         Assert.isTrue(clazz.isAssignableFrom(requiredType), "The provided required type must be assignable to ["
461                 + clazz + "]");
462     }
463 }
Popular Tags