1 19 package bak.pcj.adapter; 20 21 import bak.pcj.map.ObjectKeyByteMap; 22 import bak.pcj.map.ObjectKeyByteMapIterator; 23 import bak.pcj.map.MapDefaults; 24 import bak.pcj.hash.DefaultByteHashFunction; 25 import bak.pcj.util.Exceptions; 26 27 import java.util.Iterator ; 28 import java.util.Collection ; 29 import java.util.AbstractSet ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 48 public class ObjectKeyByteMapToMapAdapter implements Map { 49 50 51 protected ObjectKeyByteMap map; 52 53 63 public ObjectKeyByteMapToMapAdapter(ObjectKeyByteMap map) throws NullPointerException { 64 if (map == null) 65 Exceptions.nullArgument("map"); 66 this.map = map; 67 } 68 69 76 public void clear() 77 { map.clear(); } 78 79 91 public boolean containsKey(Object key) 92 { return map.containsKey(key); } 93 94 109 public boolean containsValue(Object value) { 110 if (value == null) 111 return false; 112 return map.containsValue(((Byte )value).byteValue()); 113 } 114 115 123 public Set entrySet() { 124 return new EntrySet(); 125 } 126 127 137 public boolean equals(Object obj) { 138 if (!(obj instanceof Map)) 139 return false; 140 Map m = (Map)obj; 141 if (m.size() != map.size()) 142 return false; 143 Iterator i = m.entrySet().iterator(); 144 while (i.hasNext()) { 145 Map.Entry e = (Map.Entry)i.next(); 146 if (e.getValue() == null) 147 return false; 148 if ( !get(e.getKey()).equals(e.getValue()) ) 149 return false; 150 } 151 return true; 152 } 153 154 165 public Object get(Object key) { 166 byte v = map.get(key); 167 if (v == MapDefaults.defaultByte()) 168 if (!map.containsKey(key)) 169 return null; 170 return new Byte (v); 171 } 172 173 179 public int hashCode() 180 { return map.hashCode(); } 181 182 188 public boolean isEmpty() 189 { return map.isEmpty(); } 190 191 198 public Set keySet() { 199 return map.keySet(); 200 } 201 202 227 public Object put(Object key, Object value) throws NullPointerException , ClassCastException { 228 Object result = get(key); 229 map.put(key, ((Byte )value).byteValue()); 230 return result; 231 } 232 233 254 public void putAll(Map map) throws NullPointerException , ClassCastException { 255 Iterator i = map.entrySet().iterator(); 256 while (i.hasNext()) { 257 Map.Entry e = (Map.Entry)i.next(); 258 put(e.getKey(), e.getValue()); 259 } 260 } 261 262 277 public Object remove(Object key) { 278 Object result = get(key); 279 map.remove(key); 280 return result; 281 } 282 283 290 public int size() 291 { return map.size(); } 292 293 301 public Collection values() { 302 return new ByteCollectionToCollectionAdapter(map.values()); 303 } 304 305 class EntrySet extends AbstractSet { 306 307 public Iterator iterator() { 308 return new Iterator() { 309 ObjectKeyByteMapIterator i = map.entries(); 310 311 public boolean hasNext() 312 { return i.hasNext(); } 313 314 public Object next() { 315 i.next(); 316 return new Entry(i.getKey(), i.getValue()); 317 } 318 319 public void remove() 320 { i.remove(); } 321 }; 322 } 323 324 public boolean add(Object obj) { 325 Map.Entry e = (Map.Entry)obj; 326 if (contains(e)) 327 return false; 328 put(e.getKey(), e.getValue()); 329 return true; 330 } 331 332 public int size() 333 { return map.size(); } 334 } 335 336 class Entry implements Map.Entry { 337 Object key; 338 Byte value; 339 340 Entry(Object key, byte value) { 341 this.key = key; 342 this.value = new Byte (value); 343 } 344 345 public Object getKey() 346 { return key; } 347 348 public Object getValue() 349 { return value; } 350 351 public Object setValue(Object value) 352 { return put(key, value); } 353 354 public int hashCode() 355 { return (key == null ? 0 : key.hashCode()) ^ DefaultByteHashFunction.INSTANCE.hash(value.byteValue()); } 356 357 public boolean equals(Object obj) { 358 if (!(obj instanceof Map.Entry)) 359 return false; 360 Map.Entry e = (Map.Entry)obj; 361 return (key==null?e.getKey()==null:key.equals(e.getKey())) && value.equals(e.getValue()); 362 } 363 } 364 365 } | Popular Tags |