1 7 8 package org.objectweb.jac.util; 9 10 import java.util.AbstractSet ; 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 import java.util.Map.Entry; 14 import java.util.Map ; 15 import java.util.Set ; 16 import java.util.AbstractCollection ; 17 18 49 50 public abstract class AbstractMap implements Map { 51 55 protected AbstractMap() { 56 } 57 58 60 69 public int size() { 70 return entrySet().size(); 71 } 72 73 80 public boolean isEmpty() { 81 return size() == 0; 82 } 83 84 101 public boolean containsValue(Object value) { 102 Iterator i = entrySet().iterator(); 103 if (value==null) { 104 while (i.hasNext()) { 105 Entry e = (Entry) i.next(); 106 if (e.getValue()==null) 107 return true; 108 } 109 } else { 110 while (i.hasNext()) { 111 Entry e = (Entry) i.next(); 112 if (value.equals(e.getValue())) 113 return true; 114 } 115 } 116 return false; 117 } 118 119 137 public boolean containsKey(Object key) { 138 Iterator i = entrySet().iterator(); 139 if (key==null) { 140 while (i.hasNext()) { 141 Entry e = (Entry) i.next(); 142 if (e.getKey()==null) 143 return true; 144 } 145 } else { 146 while (i.hasNext()) { 147 Entry e = (Entry) i.next(); 148 if (key.equals(e.getKey())) 149 return true; 150 } 151 } 152 return false; 153 } 154 155 178 public Object get(Object key) { 179 Iterator i = entrySet().iterator(); 180 if (key==null) { 181 while (i.hasNext()) { 182 Entry e = (Entry) i.next(); 183 if (e.getKey()==null) 184 return e.getValue(); 185 } 186 } else { 187 while (i.hasNext()) { 188 Entry e = (Entry) i.next(); 189 if (key.equals(e.getKey())) 190 return e.getValue(); 191 } 192 } 193 return null; 194 } 195 196 197 199 229 public Object put(Object key, Object value) { 230 throw new UnsupportedOperationException (); 231 } 232 233 260 public Object remove(Object key) { 261 Iterator i = entrySet().iterator(); 262 Entry correctEntry = null; 263 if (key==null) { 264 while (correctEntry==null && i.hasNext()) { 265 Entry e = (Entry) i.next(); 266 if (e.getKey()==null) 267 correctEntry = e; 268 } 269 } else { 270 while (correctEntry==null && i.hasNext()) { 271 Entry e = (Entry) i.next(); 272 if (key.equals(e.getKey())) 273 correctEntry = e; 274 } 275 } 276 277 Object oldValue = null; 278 if (correctEntry !=null) { 279 oldValue = correctEntry.getValue(); 280 i.remove(); 281 } 282 return oldValue; 283 } 284 285 286 288 315 public void putAll(Map t) { 316 Iterator i = t.entrySet().iterator(); 317 while (i.hasNext()) { 318 Entry e = (Entry) i.next(); 319 put(e.getKey(), e.getValue()); 320 } 321 } 322 323 335 public void clear() { 336 entrySet().clear(); 337 } 338 339 340 342 347 transient volatile Set keySet = null; 348 transient volatile Collection values = null; 349 350 373 public Set keySet() { 374 if (keySet == null) { 375 keySet = new AbstractSet () { 376 public Iterator iterator() { 377 return new Iterator () { 378 private Iterator i = entrySet().iterator(); 379 380 public boolean hasNext() { 381 return i.hasNext(); 382 } 383 384 public Object next() { 385 return ((Entry)i.next()).getKey(); 386 } 387 388 public void remove() { 389 i.remove(); 390 } 391 }; 392 } 393 394 public int size() { 395 return AbstractMap.this.size(); 396 } 397 398 public boolean contains(Object k) { 399 return AbstractMap.this.containsKey(k); 400 } 401 }; 402 } 403 return keySet; 404 } 405 406 430 public Collection values() { 431 if (values == null) { 432 values = new AbstractCollection () { 433 public Iterator iterator() { 434 return new Iterator () { 435 private Iterator i = entrySet().iterator(); 436 437 public boolean hasNext() { 438 return i.hasNext(); 439 } 440 441 public Object next() { 442 return ((Entry)i.next()).getValue(); 443 } 444 445 public void remove() { 446 i.remove(); 447 } 448 }; 449 } 450 451 public int size() { 452 return AbstractMap.this.size(); 453 } 454 455 public boolean contains(Object v) { 456 return AbstractMap.this.containsValue(v); 457 } 458 }; 459 } 460 return values; 461 } 462 463 476 public abstract Set entrySet(); 477 478 479 481 504 public boolean equals(Object o) { 505 if (o == this) 506 return true; 507 508 if (!(o instanceof Map )) 509 return false; 510 Map t = (Map ) o; 511 if (t.size() != size()) 512 return false; 513 514 try { 515 Iterator i = entrySet().iterator(); 516 while (i.hasNext()) { 517 Entry e = (Entry) i.next(); 518 Object key = e.getKey(); 519 Object value = e.getValue(); 520 if (value == null) { 521 if (!(t.get(key)==null && t.containsKey(key))) 522 return false; 523 } else { 524 if (!value.equals(t.get(key))) 525 return false; 526 } 527 } 528 } catch(ClassCastException unused) { 529 return false; 530 } catch(NullPointerException unused) { 531 return false; 532 } 533 534 return true; 535 } 536 537 555 public int hashCode() { 556 int h = 0; 557 Iterator i = entrySet().iterator(); 558 while (i.hasNext()) 559 h += i.next().hashCode(); 560 return h; 561 } 562 563 582 public String toString() { 583 StringBuffer buf = new StringBuffer (); 584 buf.append("{"); 585 586 Iterator i = entrySet().iterator(); 587 boolean hasNext = i.hasNext(); 588 while (hasNext) { 589 Entry e = (Entry) (i.next()); 590 Object key = e.getKey(); 591 Object value = e.getValue(); 592 buf.append((key == this ? "(this Map)" : key) + "=" + 593 (value == this ? "(this Map)": value)); 594 595 hasNext = i.hasNext(); 596 if (hasNext) 597 buf.append(", "); 598 } 599 600 buf.append("}"); 601 return buf.toString(); 602 } 603 604 610 protected Object clone() throws CloneNotSupportedException { 611 AbstractMap result = (AbstractMap)super.clone(); 612 result.keySet = null; 613 result.values = null; 614 return result; 615 } 616 617 621 static class SimpleEntry implements Entry { 622 Object key; 623 Object value; 624 625 public SimpleEntry(Object key, Object value) { 626 this.key = key; 627 this.value = value; 628 } 629 630 public SimpleEntry(Map.Entry e) { 631 this.key = e.getKey(); 632 this.value = e.getValue(); 633 } 634 635 public Object getKey() { 636 return key; 637 } 638 639 public Object getValue() { 640 return value; 641 } 642 643 public Object setValue(Object value) { 644 Object oldValue = this.value; 645 this.value = value; 646 return oldValue; 647 } 648 649 public boolean equals(Object o) { 650 if (!(o instanceof Map.Entry )) 651 return false; 652 Map.Entry e = (Map.Entry )o; 653 return eq(key, e.getKey()) && eq(value, e.getValue()); 654 } 655 656 public int hashCode() { 657 Object v; 658 return ((key == null) ? 0 : key.hashCode()) ^ 659 ((value == null) ? 0 : value.hashCode()); 660 } 661 662 public String toString() { 663 return key + "=" + value; 664 } 665 666 private static boolean eq(Object o1, Object o2) { 667 return (o1 == null ? o2 == null : o1.equals(o2)); 668 } 669 } 670 } 671 | Popular Tags |