1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
29 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
30 |
|
* specification. |
31 |
|
* |
32 |
< |
* <p>The documentation for each non-abstract methods in this class describes its |
32 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
33 |
|
* implementation in detail. Each of these methods may be overridden if the |
34 |
|
* map being implemented admits a more efficient implementation. |
35 |
|
* |
36 |
|
* <p>This class is a member of the |
37 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
37 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
38 |
|
* Java Collections Framework</a>. |
39 |
|
* |
40 |
|
* @param <K> the type of keys maintained by this map |
252 |
|
* @throws IllegalArgumentException {@inheritDoc} |
253 |
|
*/ |
254 |
|
public void putAll(Map<? extends K, ? extends V> m) { |
255 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); |
256 |
< |
while (i.hasNext()) { |
257 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
258 |
< |
put(e.getKey(), e.getValue()); |
259 |
< |
} |
255 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
256 |
> |
put(e.getKey(), e.getValue()); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
384 |
|
// Comparison and hashing |
385 |
|
|
386 |
|
/** |
387 |
< |
* {@inheritDoc} |
387 |
> |
* Compares the specified object with this map for equality. Returns |
388 |
> |
* <tt>true</tt> if the given object is also a map and the two maps |
389 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
390 |
> |
* <tt>m2</tt> represent the same mappings if |
391 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
392 |
> |
* <tt>equals</tt> method works properly across different implementations |
393 |
> |
* of the <tt>Map</tt> interface. |
394 |
|
* |
395 |
|
* <p>This implementation first checks if the specified object is this map; |
396 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
400 |
|
* contains each mapping that this map contains. If the specified map |
401 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
402 |
|
* iteration completes, <tt>true</tt> is returned. |
403 |
+ |
* |
404 |
+ |
* @param o object to be compared for equality with this map |
405 |
+ |
* @return <tt>true</tt> if the specified object is equal to this map |
406 |
|
*/ |
407 |
|
public boolean equals(Object o) { |
408 |
|
if (o == this) |
410 |
|
|
411 |
|
if (!(o instanceof Map)) |
412 |
|
return false; |
413 |
< |
Map<K,V> t = (Map<K,V>) o; |
414 |
< |
if (t.size() != size()) |
413 |
> |
Map<K,V> m = (Map<K,V>) o; |
414 |
> |
if (m.size() != size()) |
415 |
|
return false; |
416 |
|
|
417 |
|
try { |
421 |
|
K key = e.getKey(); |
422 |
|
V value = e.getValue(); |
423 |
|
if (value == null) { |
424 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
424 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
425 |
|
return false; |
426 |
|
} else { |
427 |
< |
if (!value.equals(t.get(key))) |
427 |
> |
if (!value.equals(m.get(key))) |
428 |
|
return false; |
429 |
|
} |
430 |
|
} |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
< |
* {@inheritDoc} |
441 |
> |
* Returns the hash code value for this map. The hash code of a map is |
442 |
> |
* defined to be the sum of the hash codes of each entry in the map's |
443 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
444 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
445 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
446 |
> |
* {@link Object#hashCode}. |
447 |
|
* |
448 |
|
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
449 |
< |
* <tt>hashCode()</tt> on each element (entry) in the set, and |
450 |
< |
* adding up the results. |
449 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
450 |
> |
* set, and adding up the results. |
451 |
|
* |
452 |
+ |
* @return the hash code value for this map |
453 |
|
* @see Map.Entry#hashCode() |
442 |
– |
* @see Object#hashCode() |
454 |
|
* @see Object#equals(Object) |
455 |
|
* @see Set#equals(Object) |
456 |
|
*/ |
470 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
471 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
472 |
|
* associated value. Keys and values are converted to strings as by |
473 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
463 |
< |
* |
464 |
< |
* This implementation creates an empty string buffer, appends a left |
465 |
< |
* brace, and iterates over the map's <tt>entrySet</tt> view, appending |
466 |
< |
* the string representation of each <tt>map.entry</tt> in turn. After |
467 |
< |
* appending each entry except the last, the string <tt>", "</tt> is |
468 |
< |
* appended. Finally a right brace is appended. A string is obtained |
469 |
< |
* from the stringbuffer, and returned. |
473 |
> |
* {@link String#valueOf(Object)}. |
474 |
|
* |
475 |
< |
* @return a String representation of this map |
475 |
> |
* @return a string representation of this map |
476 |
|
*/ |
477 |
|
public String toString() { |
474 |
– |
StringBuffer buf = new StringBuffer(); |
475 |
– |
buf.append("{"); |
476 |
– |
|
478 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
479 |
< |
boolean hasNext = i.hasNext(); |
480 |
< |
while (hasNext) { |
479 |
> |
if (! i.hasNext()) |
480 |
> |
return "{}"; |
481 |
> |
|
482 |
> |
StringBuilder sb = new StringBuilder(); |
483 |
> |
sb.append('{'); |
484 |
> |
for (;;) { |
485 |
|
Entry<K,V> e = i.next(); |
486 |
|
K key = e.getKey(); |
487 |
< |
V value = e.getValue(); |
488 |
< |
if (key == this) |
489 |
< |
buf.append("(this Map)"); |
490 |
< |
else |
491 |
< |
buf.append(key); |
492 |
< |
buf.append("="); |
493 |
< |
if (value == this) |
494 |
< |
buf.append("(this Map)"); |
490 |
< |
else |
491 |
< |
buf.append(value); |
492 |
< |
hasNext = i.hasNext(); |
493 |
< |
if (hasNext) |
494 |
< |
buf.append(", "); |
495 |
< |
} |
496 |
< |
|
497 |
< |
buf.append("}"); |
498 |
< |
return buf.toString(); |
487 |
> |
V value = e.getValue(); |
488 |
> |
sb.append(key == this ? "(this Map)" : key); |
489 |
> |
sb.append('='); |
490 |
> |
sb.append(value == this ? "(this Map)" : value); |
491 |
> |
if (! i.hasNext()) |
492 |
> |
return sb.append('}').toString(); |
493 |
> |
sb.append(", "); |
494 |
> |
} |
495 |
|
} |
496 |
|
|
497 |
|
/** |
512 |
|
* Test for equality, checking for nulls. |
513 |
|
*/ |
514 |
|
private static boolean eq(Object o1, Object o2) { |
515 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
515 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
516 |
|
} |
517 |
|
|
518 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
529 |
|
* facilitates the process of building custom map |
530 |
|
* implementations. For example, it may be convenient to return |
531 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
532 |
< |
* <tt>Map.entrySet().toArray</tt> |
532 |
> |
* <tt>Map.entrySet().toArray</tt>. |
533 |
> |
* |
534 |
> |
* @since 1.6 |
535 |
|
*/ |
536 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
536 |
> |
public static class SimpleEntry<K,V> |
537 |
> |
implements Entry<K,V>, java.io.Serializable |
538 |
> |
{ |
539 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
540 |
> |
|
541 |
|
private final K key; |
542 |
|
private V value; |
543 |
|
|
595 |
|
return oldValue; |
596 |
|
} |
597 |
|
|
598 |
+ |
/** |
599 |
+ |
* Compares the specified object with this entry for equality. |
600 |
+ |
* Returns {@code true} if the given object is also a map entry and |
601 |
+ |
* the two entries represent the same mapping. More formally, two |
602 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
603 |
+ |
* if<pre> |
604 |
+ |
* (e1.getKey()==null ? |
605 |
+ |
* e2.getKey()==null : |
606 |
+ |
* e1.getKey().equals(e2.getKey())) |
607 |
+ |
* && |
608 |
+ |
* (e1.getValue()==null ? |
609 |
+ |
* e2.getValue()==null : |
610 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
611 |
+ |
* This ensures that the {@code equals} method works properly across |
612 |
+ |
* different implementations of the {@code Map.Entry} interface. |
613 |
+ |
* |
614 |
+ |
* @param o object to be compared for equality with this map entry |
615 |
+ |
* @return {@code true} if the specified object is equal to this map |
616 |
+ |
* entry |
617 |
+ |
* @see #hashCode |
618 |
+ |
*/ |
619 |
|
public boolean equals(Object o) { |
620 |
|
if (!(o instanceof Map.Entry)) |
621 |
|
return false; |
623 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
624 |
|
} |
625 |
|
|
626 |
+ |
/** |
627 |
+ |
* Returns the hash code value for this map entry. The hash code |
628 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
629 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
630 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
631 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
632 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
633 |
+ |
* {@code e1} and {@code e2}, as required by the general |
634 |
+ |
* contract of {@link Object#hashCode}. |
635 |
+ |
* |
636 |
+ |
* @return the hash code value for this map entry |
637 |
+ |
* @see #equals |
638 |
+ |
*/ |
639 |
|
public int hashCode() { |
640 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
641 |
< |
((value == null) ? 0 : value.hashCode()); |
640 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
641 |
> |
(value == null ? 0 : value.hashCode()); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* An Entry maintaining an immutable key and value, This class |
659 |
> |
* An Entry maintaining an immutable key and value. This class |
660 |
|
* does not support method <tt>setValue</tt>. This class may be |
661 |
|
* convenient in methods that return thread-safe snapshots of |
662 |
|
* key-value mappings. |
663 |
+ |
* |
664 |
+ |
* @since 1.6 |
665 |
|
*/ |
666 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
666 |
> |
public static class SimpleImmutableEntry<K,V> |
667 |
> |
implements Entry<K,V>, java.io.Serializable |
668 |
> |
{ |
669 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
670 |
> |
|
671 |
|
private final K key; |
672 |
|
private final V value; |
673 |
|
|
726 |
|
throw new UnsupportedOperationException(); |
727 |
|
} |
728 |
|
|
729 |
+ |
/** |
730 |
+ |
* Compares the specified object with this entry for equality. |
731 |
+ |
* Returns {@code true} if the given object is also a map entry and |
732 |
+ |
* the two entries represent the same mapping. More formally, two |
733 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
734 |
+ |
* if<pre> |
735 |
+ |
* (e1.getKey()==null ? |
736 |
+ |
* e2.getKey()==null : |
737 |
+ |
* e1.getKey().equals(e2.getKey())) |
738 |
+ |
* && |
739 |
+ |
* (e1.getValue()==null ? |
740 |
+ |
* e2.getValue()==null : |
741 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
742 |
+ |
* This ensures that the {@code equals} method works properly across |
743 |
+ |
* different implementations of the {@code Map.Entry} interface. |
744 |
+ |
* |
745 |
+ |
* @param o object to be compared for equality with this map entry |
746 |
+ |
* @return {@code true} if the specified object is equal to this map |
747 |
+ |
* entry |
748 |
+ |
* @see #hashCode |
749 |
+ |
*/ |
750 |
|
public boolean equals(Object o) { |
751 |
|
if (!(o instanceof Map.Entry)) |
752 |
|
return false; |
754 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
755 |
|
} |
756 |
|
|
757 |
+ |
/** |
758 |
+ |
* Returns the hash code value for this map entry. The hash code |
759 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
760 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
761 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
762 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
763 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
764 |
+ |
* {@code e1} and {@code e2}, as required by the general |
765 |
+ |
* contract of {@link Object#hashCode}. |
766 |
+ |
* |
767 |
+ |
* @return the hash code value for this map entry |
768 |
+ |
* @see #equals |
769 |
+ |
*/ |
770 |
|
public int hashCode() { |
771 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
772 |
< |
((value == null) ? 0 : value.hashCode()); |
771 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
772 |
> |
(value == null ? 0 : value.hashCode()); |
773 |
|
} |
774 |
|
|
775 |
|
/** |