01: package com.jamonapi.utils;
02:
03: import java.util.*;
04:
05: /**
06: * Case Insensitive HashMap() - If the maps key is a string then the following keys are all considered equal:
07: * myKey<br>
08: * MYKEY<br>
09: * MyKey<br>
10: * ...
11: *
12: * Other than that this class works like a regular HashMap.
13: */
14: public class AppMap extends java.util.HashMap {
15:
16: public Object put(Object key, Object object) {
17: return super .put(convertKey(key), object);
18: }
19:
20: public boolean containsKey(Object key) {
21: // The normal case is done first as a performance optimization. It seems to make checks around 30% faster
22: // due to only converting the case of the string comparison only when required.
23: return super .containsKey(key)
24: || super .containsKey(convertKey(key));
25: }
26:
27: public Object get(Object key) {
28: return super .get(convertKey(key));
29: }
30:
31: public static Object get(Map map, Object key)
32: throws AppBaseException {
33: Object object = map.get(key);
34:
35: if (object != null)
36: return object;
37: else
38: throw new AppBaseException(key
39: + " does not exist in the HashMap.");
40: }
41:
42: protected Object convertKey(Object key) {
43: if (key instanceof String && key != null)
44: key = key.toString().toLowerCase();
45:
46: return key;
47:
48: }
49:
50: public static Map createInstance() {
51: return new AppMap();
52: }
53:
54: }
|