1 7 8 package java.util; 9 import java.util.Map.Entry; 10 11 47 48 public abstract class AbstractMap<K,V> implements Map <K,V> { 49 53 protected AbstractMap() { 54 } 55 56 58 67 public int size() { 68 return entrySet().size(); 69 } 70 71 78 public boolean isEmpty() { 79 return size() == 0; 80 } 81 82 99 public boolean containsValue(Object value) { 100 Iterator <Entry<K,V>> i = entrySet().iterator(); 101 if (value==null) { 102 while (i.hasNext()) { 103 Entry<K,V> e = i.next(); 104 if (e.getValue()==null) 105 return true; 106 } 107 } else { 108 while (i.hasNext()) { 109 Entry<K,V> e = i.next(); 110 if (value.equals(e.getValue())) 111 return true; 112 } 113 } 114 return false; 115 } 116 117 135 public boolean containsKey(Object key) { 136 Iterator <Map.Entry <K,V>> i = entrySet().iterator(); 137 if (key==null) { 138 while (i.hasNext()) { 139 Entry<K,V> e = i.next(); 140 if (e.getKey()==null) 141 return true; 142 } 143 } else { 144 while (i.hasNext()) { 145 Entry<K,V> e = i.next(); 146 if (key.equals(e.getKey())) 147 return true; 148 } 149 } 150 return false; 151 } 152 153 176 public V get(Object key) { 177 Iterator <Entry<K,V>> i = entrySet().iterator(); 178 if (key==null) { 179 while (i.hasNext()) { 180 Entry<K,V> e = i.next(); 181 if (e.getKey()==null) 182 return e.getValue(); 183 } 184 } else { 185 while (i.hasNext()) { 186 Entry<K,V> e = i.next(); 187 if (key.equals(e.getKey())) 188 return e.getValue(); 189 } 190 } 191 return null; 192 } 193 194 195 197 227 public V put(K key, V value) { 228 throw new UnsupportedOperationException (); 229 } 230 231 258 public V remove(Object key) { 259 Iterator <Entry<K,V>> i = entrySet().iterator(); 260 Entry<K,V> correctEntry = null; 261 if (key==null) { 262 while (correctEntry==null && i.hasNext()) { 263 Entry<K,V> e = i.next(); 264 if (e.getKey()==null) 265 correctEntry = e; 266 } 267 } else { 268 while (correctEntry==null && i.hasNext()) { 269 Entry<K,V> e = i.next(); 270 if (key.equals(e.getKey())) 271 correctEntry = e; 272 } 273 } 274 275 V oldValue = null; 276 if (correctEntry !=null) { 277 oldValue = correctEntry.getValue(); 278 i.remove(); 279 } 280 return oldValue; 281 } 282 283 284 286 313 public void putAll(Map <? extends K, ? extends V> t) { 314 Iterator <? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator(); 315 while (i.hasNext()) { 316 Entry<? extends K, ? extends V> e = i.next(); 317 put(e.getKey(), e.getValue()); 318 } 319 } 320 321 333 public void clear() { 334 entrySet().clear(); 335 } 336 337 338 340 345 transient volatile Set <K> keySet = null; 346 transient volatile Collection <V> values = null; 347 348 371 public Set <K> keySet() { 372 if (keySet == null) { 373 keySet = new AbstractSet <K>() { 374 public Iterator <K> iterator() { 375 return new Iterator <K>() { 376 private Iterator <Entry<K,V>> i = entrySet().iterator(); 377 378 public boolean hasNext() { 379 return i.hasNext(); 380 } 381 382 public K next() { 383 return i.next().getKey(); 384 } 385 386 public void remove() { 387 i.remove(); 388 } 389 }; 390 } 391 392 public int size() { 393 return AbstractMap.this.size(); 394 } 395 396 public boolean contains(Object k) { 397 return AbstractMap.this.containsKey(k); 398 } 399 }; 400 } 401 return keySet; 402 } 403 404 428 public Collection <V> values() { 429 if (values == null) { 430 values = new AbstractCollection <V>() { 431 public Iterator <V> iterator() { 432 return new Iterator <V>() { 433 private Iterator <Entry<K,V>> i = entrySet().iterator(); 434 435 public boolean hasNext() { 436 return i.hasNext(); 437 } 438 439 public V next() { 440 return i.next().getValue(); 441 } 442 443 public void remove() { 444 i.remove(); 445 } 446 }; 447 } 448 449 public int size() { 450 return AbstractMap.this.size(); 451 } 452 453 public boolean contains(Object v) { 454 return AbstractMap.this.containsValue(v); 455 } 456 }; 457 } 458 return values; 459 } 460 461 474 public abstract Set <Entry<K,V>> entrySet(); 475 476 477 479 502 public boolean equals(Object o) { 503 if (o == this) 504 return true; 505 506 if (!(o instanceof Map )) 507 return false; 508 Map <K,V> t = (Map <K,V>) o; 509 if (t.size() != size()) 510 return false; 511 512 try { 513 Iterator <Entry<K,V>> i = entrySet().iterator(); 514 while (i.hasNext()) { 515 Entry<K,V> e = i.next(); 516 K key = e.getKey(); 517 V value = e.getValue(); 518 if (value == null) { 519 if (!(t.get(key)==null && t.containsKey(key))) 520 return false; 521 } else { 522 if (!value.equals(t.get(key))) 523 return false; 524 } 525 } 526 } catch(ClassCastException unused) { 527 return false; 528 } catch(NullPointerException unused) { 529 return false; 530 } 531 532 return true; 533 } 534 535 553 public int hashCode() { 554 int h = 0; 555 Iterator <Entry<K,V>> i = entrySet().iterator(); 556 while (i.hasNext()) 557 h += i.next().hashCode(); 558 return h; 559 } 560 561 580 public String toString() { 581 StringBuffer buf = new StringBuffer (); 582 buf.append("{"); 583 584 Iterator <Entry<K,V>> i = entrySet().iterator(); 585 boolean hasNext = i.hasNext(); 586 while (hasNext) { 587 Entry<K,V> e = i.next(); 588 K key = e.getKey(); 589 V value = e.getValue(); 590 if (key == this) 591 buf.append("(this Map)"); 592 else 593 buf.append(key); 594 buf.append("="); 595 if (value == this) 596 buf.append("(this Map)"); 597 else 598 buf.append(value); 599 hasNext = i.hasNext(); 600 if (hasNext) 601 buf.append(", "); 602 } 603 604 buf.append("}"); 605 return buf.toString(); 606 } 607 608 614 protected Object clone() throws CloneNotSupportedException { 615 AbstractMap <K,V> result = (AbstractMap <K,V>)super.clone(); 616 result.keySet = null; 617 result.values = null; 618 return result; 619 } 620 621 625 static class SimpleEntry<K,V> implements Entry<K,V> { 626 K key; 627 V value; 628 629 public SimpleEntry(K key, V value) { 630 this.key = key; 631 this.value = value; 632 } 633 634 public SimpleEntry(Entry<K,V> e) { 635 this.key = e.getKey(); 636 this.value = e.getValue(); 637 } 638 639 public K getKey() { 640 return key; 641 } 642 643 public V getValue() { 644 return value; 645 } 646 647 public V setValue(V value) { 648 V oldValue = this.value; 649 this.value = value; 650 return oldValue; 651 } 652 653 public boolean equals(Object o) { 654 if (!(o instanceof Map.Entry )) 655 return false; 656 Map.Entry e = (Map.Entry )o; 657 return eq(key, e.getKey()) && eq(value, e.getValue()); 658 } 659 660 public int hashCode() { 661 return ((key == null) ? 0 : key.hashCode()) ^ 662 ((value == null) ? 0 : value.hashCode()); 663 } 664 665 public String toString() { 666 return key + "=" + value; 667 } 668 669 private static boolean eq(Object o1, Object o2) { 670 return (o1 == null ? o2 == null : o1.equals(o2)); 671 } 672 } 673 } 674 | Popular Tags |