1 19 package bak.pcj.adapter; 20 21 import bak.pcj.map.LongKeyMap; 22 import bak.pcj.map.LongKeyMapIterator; 23 import bak.pcj.hash.DefaultLongHashFunction; 24 import bak.pcj.util.Exceptions; 25 26 import java.util.Iterator ; 27 import java.util.Collection ; 28 import java.util.AbstractSet ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 47 public class LongKeyMapToMapAdapter implements Map { 48 49 50 protected LongKeyMap map; 51 52 62 public LongKeyMapToMapAdapter(LongKeyMap map) { 63 if (map == null) 64 Exceptions.nullArgument("map"); 65 this.map = map; 66 } 67 68 75 public void clear() 76 { map.clear(); } 77 78 96 public boolean containsKey(Object key) 97 { return map.containsKey(((Long )key).longValue()); } 98 99 110 public boolean containsValue(Object value) { 111 return map.containsValue(value); 112 } 113 114 122 public Set entrySet() { 123 return new EntrySet(); 124 } 125 126 136 public boolean equals(Object obj) { 137 if (!(obj instanceof Map)) 138 return false; 139 Map m = (Map)obj; 140 if (m.size() != map.size()) 141 return false; 142 Iterator i = m.entrySet().iterator(); 143 while (i.hasNext()) { 144 Map.Entry e = (Map.Entry)i.next(); 145 if (e.getKey() == null) 146 return false; 147 if (e.getValue() == null) 148 return false; 149 if ( !get(e.getKey()).equals(e.getValue()) ) 150 return false; 151 } 152 return true; 153 } 154 155 171 public Object get(Object key) { 172 long k = ((Long )key).longValue(); 173 return map.get(k); 174 } 175 176 182 public int hashCode() 183 { return map.hashCode(); } 184 185 191 public boolean isEmpty() 192 { return map.isEmpty(); } 193 194 202 public Set keySet() { 203 return new LongSetToSetAdapter(map.keySet()); 204 } 205 206 232 public Object put(Object key, Object value) { 233 long k = ((Long )key).longValue(); 234 return map.put(k, value); 235 } 236 237 258 public void putAll(Map map) { 259 Iterator i = map.entrySet().iterator(); 260 while (i.hasNext()) { 261 Map.Entry e = (Map.Entry)i.next(); 262 put(e.getKey(), e.getValue()); 263 } 264 } 265 266 281 public Object remove(Object key) { 282 if (key == null) 283 return null; 284 if (!(key instanceof Long )) 285 return null; 286 long k = ((Long )key).longValue(); 287 return map.remove(k); 288 } 289 290 297 public int size() 298 { return map.size(); } 299 300 307 public Collection values() { 308 return map.values(); 309 } 310 311 class EntrySet extends AbstractSet { 312 313 public Iterator iterator() { 314 return new Iterator() { 315 LongKeyMapIterator i = map.entries(); 316 317 public boolean hasNext() 318 { return i.hasNext(); } 319 320 public Object next() { 321 i.next(); 322 return new Entry(i.getKey(), i.getValue()); 323 } 324 325 public void remove() 326 { i.remove(); } 327 }; 328 } 329 330 public boolean add(Object obj) { 331 Map.Entry e = (Map.Entry)obj; 332 if (contains(e)) 333 return false; 334 put(e.getKey(), e.getValue()); 335 return true; 336 } 337 338 public int size() 339 { return map.size(); } 340 } 341 342 class Entry implements Map.Entry { 343 Long key; 344 Object value; 345 346 Entry(long key, Object value) { 347 this.key = new Long (key); 348 this.value = value; 349 } 350 351 public Object getKey() 352 { return key; } 353 354 public Object getValue() 355 { return value; } 356 357 public Object setValue(Object value) 358 { return put(key, value); } 359 360 public int hashCode() 361 { return DefaultLongHashFunction.INSTANCE.hash(key.longValue()) ^ value.hashCode(); } 362 363 public boolean equals(Object obj) { 364 if (!(obj instanceof Map.Entry)) 365 return false; 366 Map.Entry e = (Map.Entry)obj; 367 return key.equals(e.getKey()) && value.equals(e.getValue()); 368 } 369 } 370 371 372 } | Popular Tags |