1 4 5 9 10 package org.openlaszlo.utils; 11 12 import java.util.*; 13 14 24 public class ComparisonMap extends HashMap { 25 26 27 private HashMap keyMap; 28 29 public Set normalizedKeySet() { 30 return keyMap.keySet(); 31 } 32 33 public ComparisonMap() { 34 super(); 35 keyMap = new HashMap(); 36 } 37 38 public ComparisonMap(int initialCapacity) { 39 super(initialCapacity); 40 keyMap = new HashMap(initialCapacity); 41 } 42 43 public ComparisonMap(int initialCapacity, float loadFactor) { 44 super(initialCapacity, loadFactor); 45 keyMap = new HashMap(initialCapacity, loadFactor); 46 } 47 48 public ComparisonMap(Map m) { 49 this(m.size()); 50 putAll(m); 51 } 52 53 private Object caseInsensitiveKey(Object key) { 54 return key instanceof String ? ((String )key).toLowerCase() : key; 55 } 56 57 public void clear() { 58 keyMap.clear(); 59 super.clear(); 60 } 61 62 public Object clone() { 63 return new ComparisonMap(this); 64 } 65 66 public boolean containsKey(Object key) { 67 return keyMap.containsKey(caseInsensitiveKey(key)); 68 } 69 70 71 72 73 74 public Object get(Object key) { 75 key = caseInsensitiveKey(key); 76 if (keyMap.containsKey(key)) { 77 return super.get(keyMap.get(key)); 78 } else { 79 return null; 80 } 81 } 82 83 84 85 86 87 public Object put (Object key, Object value) { 88 keyMap.put(caseInsensitiveKey(key), key); 89 return super.put(key, value); 90 } 91 92 public void putAll(Map m) { 93 for (Iterator i = m.entrySet().iterator(); i.hasNext(); ) { 94 Map.Entry entry = (Map.Entry) i.next(); 95 put(entry.getKey(), entry.getValue()); 96 } 97 } 98 99 public Object remove(Object key) { 100 key = caseInsensitiveKey(key); 101 if (keyMap.containsKey(key)) { 102 return super.remove(keyMap.remove(key)); 103 } else { 104 return null; 105 } 106 } 107 108 109 110 111 112 } 113 | Popular Tags |