1 19 package bak.pcj.adapter; 20 21 import bak.pcj.ByteCollection; 22 import bak.pcj.map.LongKeyByteMap; 23 import bak.pcj.map.LongKeyByteMapIterator; 24 import bak.pcj.map.MapDefaults; 25 import bak.pcj.hash.DefaultLongHashFunction; 26 import bak.pcj.hash.DefaultByteHashFunction; 27 import bak.pcj.util.Exceptions; 28 29 import java.util.Iterator ; 30 import java.util.Collection ; 31 import java.util.AbstractSet ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 50 public class LongKeyByteMapToMapAdapter implements Map { 51 52 53 protected LongKeyByteMap map; 54 55 65 public LongKeyByteMapToMapAdapter(LongKeyByteMap map) throws NullPointerException { 66 if (map == null) 67 Exceptions.nullArgument("map"); 68 this.map = map; 69 } 70 71 78 public void clear() 79 { map.clear(); } 80 81 99 public boolean containsKey(Object key) throws NullPointerException , ClassCastException 100 { return map.containsKey(((Long )key).longValue()); } 101 102 117 public boolean containsValue(Object value) { 118 if (value == null) 119 return false; 120 return map.containsValue(((Byte )value).byteValue()); 121 } 122 123 131 public Set entrySet() { 132 return new EntrySet(); 133 } 134 135 145 public boolean equals(Object obj) { 146 if (!(obj instanceof Map)) 147 return false; 148 Map m = (Map)obj; 149 if (m.size() != map.size()) 150 return false; 151 Iterator i = m.entrySet().iterator(); 152 while (i.hasNext()) { 153 Map.Entry e = (Map.Entry)i.next(); 154 if (e.getKey() == null) 155 return false; 156 if (e.getValue() == null) 157 return false; 158 if ( !get(e.getKey()).equals(e.getValue()) ) 159 return false; 160 } 161 return true; 162 } 163 164 181 public Object get(Object key) throws NullPointerException , ClassCastException { 182 long k = ((Long )key).longValue(); 183 byte v = map.get(k); 184 if (v == MapDefaults.defaultByte()) 185 if (!map.containsKey(k)) 186 return null; 187 return new Byte (v); 188 } 189 190 196 public int hashCode() 197 { return map.hashCode(); } 198 199 205 public boolean isEmpty() 206 { return map.isEmpty(); } 207 208 216 public Set keySet() { 217 return new LongSetToSetAdapter(map.keySet()); 218 } 219 220 247 public Object put(Object key, Object value) throws NullPointerException , ClassCastException { 248 Object result = get(key); 249 long k = ((Long )key).longValue(); 250 map.put(k, ((Byte )value).byteValue()); 251 return result; 252 } 253 254 277 public void putAll(Map map) throws NullPointerException , ClassCastException { 278 Iterator i = map.entrySet().iterator(); 279 while (i.hasNext()) { 280 Map.Entry e = (Map.Entry)i.next(); 281 put(e.getKey(), e.getValue()); 282 } 283 } 284 285 300 public Object remove(Object key) { 301 if (key == null) 302 return null; 303 if (!(key instanceof Long )) 304 return null; 305 Object result = get(key); 306 long k = ((Long )key).longValue(); 307 map.remove(k); 308 return result; 309 } 310 311 318 public int size() 319 { return map.size(); } 320 321 329 public Collection values() { 330 return new ByteCollectionToCollectionAdapter(map.values()); 331 } 332 333 class EntrySet extends AbstractSet { 334 335 public Iterator iterator() { 336 return new Iterator() { 337 LongKeyByteMapIterator i = map.entries(); 338 339 public boolean hasNext() 340 { return i.hasNext(); } 341 342 public Object next() { 343 i.next(); 344 return new Entry(i.getKey(), i.getValue()); 345 } 346 347 public void remove() 348 { i.remove(); } 349 }; 350 } 351 352 public boolean add(Object obj) { 353 Map.Entry e = (Map.Entry)obj; 354 if (contains(e)) 355 return false; 356 put(e.getKey(), e.getValue()); 357 return true; 358 } 359 360 public int size() 361 { return map.size(); } 362 } 363 364 class Entry implements Map.Entry { 365 Long key; 366 Byte value; 367 368 Entry(long key, byte value) { 369 this.key = new Long (key); 370 this.value = new Byte (value); 371 } 372 373 public Object getKey() 374 { return key; } 375 376 public Object getValue() 377 { return value; } 378 379 public Object setValue(Object value) 380 { return put(key, value); } 381 382 public int hashCode() 383 { return DefaultLongHashFunction.INSTANCE.hash(key.longValue()) ^ DefaultByteHashFunction.INSTANCE.hash(value.byteValue()); } 384 385 public boolean equals(Object obj) { 386 if (!(obj instanceof Map.Entry)) 387 return false; 388 Map.Entry e = (Map.Entry)obj; 389 return key.equals(e.getKey()) && value.equals(e.getValue()); 390 } 391 } 392 393 394 } | Popular Tags |