1 19 package bak.pcj.adapter; 20 21 import bak.pcj.CharCollection; 22 import bak.pcj.map.ByteKeyCharMap; 23 import bak.pcj.map.ByteKeyCharMapIterator; 24 import bak.pcj.map.MapDefaults; 25 import bak.pcj.hash.DefaultByteHashFunction; 26 import bak.pcj.hash.DefaultCharHashFunction; 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 ByteKeyCharMapToMapAdapter implements Map { 51 52 53 protected ByteKeyCharMap map; 54 55 65 public ByteKeyCharMapToMapAdapter(ByteKeyCharMap 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(((Byte )key).byteValue()); } 101 102 117 public boolean containsValue(Object value) { 118 if (value == null) 119 return false; 120 return map.containsValue(((Character )value).charValue()); 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 byte k = ((Byte )key).byteValue(); 183 char v = map.get(k); 184 if (v == MapDefaults.defaultChar()) 185 if (!map.containsKey(k)) 186 return null; 187 return new Character (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 ByteSetToSetAdapter(map.keySet()); 218 } 219 220 247 public Object put(Object key, Object value) throws NullPointerException , ClassCastException { 248 Object result = get(key); 249 byte k = ((Byte )key).byteValue(); 250 map.put(k, ((Character )value).charValue()); 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 Byte )) 304 return null; 305 Object result = get(key); 306 byte k = ((Byte )key).byteValue(); 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 CharCollectionToCollectionAdapter(map.values()); 331 } 332 333 class EntrySet extends AbstractSet { 334 335 public Iterator iterator() { 336 return new Iterator() { 337 ByteKeyCharMapIterator 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 Byte key; 366 Character value; 367 368 Entry(byte key, char value) { 369 this.key = new Byte (key); 370 this.value = new Character (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 DefaultByteHashFunction.INSTANCE.hash(key.byteValue()) ^ DefaultCharHashFunction.INSTANCE.hash(value.charValue()); } 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 |