1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 |
> |
* CA 95054 USA or visit www.sun.com if you need additional information or |
23 |
> |
* have any questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
47 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
48 |
|
* specification. |
49 |
|
* |
50 |
< |
* <p>The documentation for each non-abstract methods in this class describes its |
50 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
51 |
|
* implementation in detail. Each of these methods may be overridden if the |
52 |
|
* map being implemented admits a more efficient implementation. |
53 |
|
* |
54 |
|
* <p>This class is a member of the |
55 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
55 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
56 |
|
* Java Collections Framework</a>. |
57 |
|
* |
58 |
|
* @param <K> the type of keys maintained by this map |
270 |
|
* @throws IllegalArgumentException {@inheritDoc} |
271 |
|
*/ |
272 |
|
public void putAll(Map<? extends K, ? extends V> m) { |
273 |
< |
Iterator<? extends Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); |
274 |
< |
while (i.hasNext()) { |
257 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
258 |
< |
put(e.getKey(), e.getValue()); |
259 |
< |
} |
273 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
274 |
> |
put(e.getKey(), e.getValue()); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
402 |
|
// Comparison and hashing |
403 |
|
|
404 |
|
/** |
405 |
< |
* {@inheritDoc} |
405 |
> |
* Compares the specified object with this map for equality. Returns |
406 |
> |
* <tt>true</tt> if the given object is also a map and the two maps |
407 |
> |
* represent the same mappings. More formally, two maps <tt>m1</tt> and |
408 |
> |
* <tt>m2</tt> represent the same mappings if |
409 |
> |
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the |
410 |
> |
* <tt>equals</tt> method works properly across different implementations |
411 |
> |
* of the <tt>Map</tt> interface. |
412 |
|
* |
413 |
|
* <p>This implementation first checks if the specified object is this map; |
414 |
|
* if so it returns <tt>true</tt>. Then, it checks if the specified |
418 |
|
* contains each mapping that this map contains. If the specified map |
419 |
|
* fails to contain such a mapping, <tt>false</tt> is returned. If the |
420 |
|
* iteration completes, <tt>true</tt> is returned. |
421 |
+ |
* |
422 |
+ |
* @param o object to be compared for equality with this map |
423 |
+ |
* @return <tt>true</tt> if the specified object is equal to this map |
424 |
|
*/ |
425 |
|
public boolean equals(Object o) { |
426 |
|
if (o == this) |
428 |
|
|
429 |
|
if (!(o instanceof Map)) |
430 |
|
return false; |
431 |
< |
Map<K,V> t = (Map<K,V>) o; |
432 |
< |
if (t.size() != size()) |
431 |
> |
Map<K,V> m = (Map<K,V>) o; |
432 |
> |
if (m.size() != size()) |
433 |
|
return false; |
434 |
|
|
435 |
|
try { |
439 |
|
K key = e.getKey(); |
440 |
|
V value = e.getValue(); |
441 |
|
if (value == null) { |
442 |
< |
if (!(t.get(key)==null && t.containsKey(key))) |
442 |
> |
if (!(m.get(key)==null && m.containsKey(key))) |
443 |
|
return false; |
444 |
|
} else { |
445 |
< |
if (!value.equals(t.get(key))) |
445 |
> |
if (!value.equals(m.get(key))) |
446 |
|
return false; |
447 |
|
} |
448 |
|
} |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* {@inheritDoc} |
459 |
> |
* Returns the hash code value for this map. The hash code of a map is |
460 |
> |
* defined to be the sum of the hash codes of each entry in the map's |
461 |
> |
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt> |
462 |
> |
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps |
463 |
> |
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of |
464 |
> |
* {@link Object#hashCode}. |
465 |
|
* |
466 |
|
* <p>This implementation iterates over <tt>entrySet()</tt>, calling |
467 |
< |
* <tt>hashCode()</tt> on each element (entry) in the set, and |
468 |
< |
* adding up the results. |
467 |
> |
* {@link Map.Entry#hashCode hashCode()} on each element (entry) in the |
468 |
> |
* set, and adding up the results. |
469 |
|
* |
470 |
+ |
* @return the hash code value for this map |
471 |
|
* @see Map.Entry#hashCode() |
442 |
– |
* @see Object#hashCode() |
472 |
|
* @see Object#equals(Object) |
473 |
|
* @see Set#equals(Object) |
474 |
|
*/ |
488 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
489 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
490 |
|
* associated value. Keys and values are converted to strings as by |
491 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
491 |
> |
* {@link String#valueOf(Object)}. |
492 |
|
* |
493 |
< |
* 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. |
470 |
< |
* |
471 |
< |
* @return a String representation of this map |
493 |
> |
* @return a string representation of this map |
494 |
|
*/ |
495 |
|
public String toString() { |
474 |
– |
StringBuilder sb = new StringBuilder(); |
475 |
– |
sb.append("{"); |
476 |
– |
|
496 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
497 |
< |
boolean hasNext = i.hasNext(); |
498 |
< |
while (hasNext) { |
497 |
> |
if (! i.hasNext()) |
498 |
> |
return "{}"; |
499 |
> |
|
500 |
> |
StringBuilder sb = new StringBuilder(); |
501 |
> |
sb.append('{'); |
502 |
> |
for (;;) { |
503 |
|
Entry<K,V> e = i.next(); |
504 |
|
K key = e.getKey(); |
505 |
< |
V value = e.getValue(); |
506 |
< |
if (key == this) |
507 |
< |
sb.append("(this Map)"); |
508 |
< |
else |
509 |
< |
sb.append(key); |
510 |
< |
sb.append("="); |
511 |
< |
if (value == this) |
512 |
< |
sb.append("(this Map)"); |
490 |
< |
else |
491 |
< |
sb.append(value); |
492 |
< |
hasNext = i.hasNext(); |
493 |
< |
if (hasNext) |
494 |
< |
sb.append(", "); |
495 |
< |
} |
496 |
< |
|
497 |
< |
sb.append("}"); |
498 |
< |
return sb.toString(); |
505 |
> |
V value = e.getValue(); |
506 |
> |
sb.append(key == this ? "(this Map)" : key); |
507 |
> |
sb.append('='); |
508 |
> |
sb.append(value == this ? "(this Map)" : value); |
509 |
> |
if (! i.hasNext()) |
510 |
> |
return sb.append('}').toString(); |
511 |
> |
sb.append(", "); |
512 |
> |
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
530 |
|
* Test for equality, checking for nulls. |
531 |
|
*/ |
532 |
|
private static boolean eq(Object o1, Object o2) { |
533 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
533 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
534 |
|
} |
535 |
|
|
536 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
547 |
|
* facilitates the process of building custom map |
548 |
|
* implementations. For example, it may be convenient to return |
549 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
550 |
< |
* <tt>Map.entrySet().toArray</tt> |
550 |
> |
* <tt>Map.entrySet().toArray</tt>. |
551 |
> |
* |
552 |
> |
* @since 1.6 |
553 |
|
*/ |
554 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
554 |
> |
public static class SimpleEntry<K,V> |
555 |
> |
implements Entry<K,V>, java.io.Serializable |
556 |
> |
{ |
557 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
558 |
> |
|
559 |
|
private final K key; |
560 |
|
private V value; |
561 |
|
|
613 |
|
return oldValue; |
614 |
|
} |
615 |
|
|
616 |
+ |
/** |
617 |
+ |
* Compares the specified object with this entry for equality. |
618 |
+ |
* Returns {@code true} if the given object is also a map entry and |
619 |
+ |
* the two entries represent the same mapping. More formally, two |
620 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
621 |
+ |
* if<pre> |
622 |
+ |
* (e1.getKey()==null ? |
623 |
+ |
* e2.getKey()==null : |
624 |
+ |
* e1.getKey().equals(e2.getKey())) |
625 |
+ |
* && |
626 |
+ |
* (e1.getValue()==null ? |
627 |
+ |
* e2.getValue()==null : |
628 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
629 |
+ |
* This ensures that the {@code equals} method works properly across |
630 |
+ |
* different implementations of the {@code Map.Entry} interface. |
631 |
+ |
* |
632 |
+ |
* @param o object to be compared for equality with this map entry |
633 |
+ |
* @return {@code true} if the specified object is equal to this map |
634 |
+ |
* entry |
635 |
+ |
* @see #hashCode |
636 |
+ |
*/ |
637 |
|
public boolean equals(Object o) { |
638 |
|
if (!(o instanceof Map.Entry)) |
639 |
|
return false; |
641 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
642 |
|
} |
643 |
|
|
644 |
+ |
/** |
645 |
+ |
* Returns the hash code value for this map entry. The hash code |
646 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
647 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
648 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
649 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
650 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
651 |
+ |
* {@code e1} and {@code e2}, as required by the general |
652 |
+ |
* contract of {@link Object#hashCode}. |
653 |
+ |
* |
654 |
+ |
* @return the hash code value for this map entry |
655 |
+ |
* @see #equals |
656 |
+ |
*/ |
657 |
|
public int hashCode() { |
658 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
659 |
< |
((value == null) ? 0 : value.hashCode()); |
658 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
659 |
> |
(value == null ? 0 : value.hashCode()); |
660 |
|
} |
661 |
|
|
662 |
|
/** |
674 |
|
} |
675 |
|
|
676 |
|
/** |
677 |
< |
* An Entry maintaining an immutable key and value, This class |
677 |
> |
* An Entry maintaining an immutable key and value. This class |
678 |
|
* does not support method <tt>setValue</tt>. This class may be |
679 |
|
* convenient in methods that return thread-safe snapshots of |
680 |
|
* key-value mappings. |
681 |
+ |
* |
682 |
+ |
* @since 1.6 |
683 |
|
*/ |
684 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
684 |
> |
public static class SimpleImmutableEntry<K,V> |
685 |
> |
implements Entry<K,V>, java.io.Serializable |
686 |
> |
{ |
687 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
688 |
> |
|
689 |
|
private final K key; |
690 |
|
private final V value; |
691 |
|
|
744 |
|
throw new UnsupportedOperationException(); |
745 |
|
} |
746 |
|
|
747 |
+ |
/** |
748 |
+ |
* Compares the specified object with this entry for equality. |
749 |
+ |
* Returns {@code true} if the given object is also a map entry and |
750 |
+ |
* the two entries represent the same mapping. More formally, two |
751 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
752 |
+ |
* if<pre> |
753 |
+ |
* (e1.getKey()==null ? |
754 |
+ |
* e2.getKey()==null : |
755 |
+ |
* e1.getKey().equals(e2.getKey())) |
756 |
+ |
* && |
757 |
+ |
* (e1.getValue()==null ? |
758 |
+ |
* e2.getValue()==null : |
759 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
760 |
+ |
* This ensures that the {@code equals} method works properly across |
761 |
+ |
* different implementations of the {@code Map.Entry} interface. |
762 |
+ |
* |
763 |
+ |
* @param o object to be compared for equality with this map entry |
764 |
+ |
* @return {@code true} if the specified object is equal to this map |
765 |
+ |
* entry |
766 |
+ |
* @see #hashCode |
767 |
+ |
*/ |
768 |
|
public boolean equals(Object o) { |
769 |
|
if (!(o instanceof Map.Entry)) |
770 |
|
return false; |
772 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
773 |
|
} |
774 |
|
|
775 |
+ |
/** |
776 |
+ |
* Returns the hash code value for this map entry. The hash code |
777 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
778 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
779 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
780 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
781 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
782 |
+ |
* {@code e1} and {@code e2}, as required by the general |
783 |
+ |
* contract of {@link Object#hashCode}. |
784 |
+ |
* |
785 |
+ |
* @return the hash code value for this map entry |
786 |
+ |
* @see #equals |
787 |
+ |
*/ |
788 |
|
public int hashCode() { |
789 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
790 |
< |
((value == null) ? 0 : value.hashCode()); |
789 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
790 |
> |
(value == null ? 0 : value.hashCode()); |
791 |
|
} |
792 |
|
|
793 |
|
/** |