1 package javolution.util; 2 3 import j2me.util.Collection; 4 import j2me.util.Map; 5 import j2me.util.Set; 6 import javolution.context.LocalContext; 7 8 48 public final class LocalMapimplements Map{ 49 50 53 private final LocalContext.Reference _mapRef 54 = new LocalContext.Reference(new FastMap().setShared(true)); 55 56 59 public LocalMap() { 60 } 61 62 68 public LocalMap setKeyComparator(FastComparator keyComparator) { 69 localMap().setKeyComparator(keyComparator); 70 return this; 71 } 72 73 79 public LocalMapsetValueComparator(FastComparator valueComparator) { 80 localMap().setValueComparator(valueComparator); 81 return this; 82 } 83 84 97 public Object putDefault(Object key, Object defaultValue) { 98 return (Object ) ((FastMap) _mapRef.getDefault()).put(key, 99 defaultValue); 100 } 101 106 public int size() { 107 return ((FastMap) _mapRef.get()).size(); 108 } 109 110 116 public boolean isEmpty() { 117 return ((FastMap) _mapRef.get()).isEmpty(); 118 } 119 120 128 public boolean containsKey(Object key) { 129 return ((FastMap) _mapRef.get()).containsKey(key); 130 } 131 132 141 public boolean containsValue(Object value) { 142 return ((FastMap) _mapRef.get()).containsValue(value); 143 } 144 145 153 public Object get(Object key) { 154 return (Object ) ((FastMap) _mapRef.get()).get(key); 155 } 156 157 168 public Object put(Object key, Object value) { 169 return (Object ) localMap().put(key, value); 170 } 171 172 179 public void putAll(Mapmap) { 180 localMap().putAll(map); 181 } 182 183 191 public Object remove(Object key) { 192 return put((Object )key, null); 193 } 194 195 199 public void clear() { 200 FastMap localMap = localMap(); 201 for (FastMap.Entry e = localMap.head(), end = localMap.tail(); (e = (FastMap.Entry) e.getNext()) != end;) { 202 e.setValue(null); 203 } 204 } 205 206 212 public SetkeySet() { 213 return localMap().keySet(); 214 } 215 216 223 public Collectionvalues() { 224 return localMap().values(); 225 } 226 227 234 public SetentrySet() { 235 return localMap().entrySet(); 236 } 237 238 244 private FastMaplocalMap() { 245 FastMap localMap = (FastMap) _mapRef.getLocal(); 246 return (localMap != null) ? localMap : newLocalMap(); 247 } 248 249 private FastMap newLocalMap() { 250 FastMap parentMap = (FastMap) _mapRef.get(); 251 FastMap localMap = FastMap.newInstance(); 252 localMap.setShared(true); 253 localMap.setKeyComparator(parentMap.getKeyComparator()); 254 localMap.setValueComparator(parentMap.getValueComparator()); 255 localMap.putAll(parentMap); 256 _mapRef.set(localMap); 257 return localMap; 258 } 259 } | Popular Tags |