1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright 1997-2007 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; |
9 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
27 |
|
import java.util.Map.Entry; |
28 |
|
|
29 |
|
/** |
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()) { |
258 |
< |
Entry<? extends K, ? extends V> e = i.next(); |
259 |
< |
put(e.getKey(), e.getValue()); |
260 |
< |
} |
273 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
274 |
> |
put(e.getKey(), e.getValue()); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
340 |
|
return AbstractMap.this.size(); |
341 |
|
} |
342 |
|
|
343 |
+ |
public boolean isEmpty() { |
344 |
+ |
return AbstractMap.this.isEmpty(); |
345 |
+ |
} |
346 |
+ |
|
347 |
+ |
public void clear() { |
348 |
+ |
AbstractMap.this.clear(); |
349 |
+ |
} |
350 |
+ |
|
351 |
|
public boolean contains(Object k) { |
352 |
|
return AbstractMap.this.containsKey(k); |
353 |
|
} |
396 |
|
return AbstractMap.this.size(); |
397 |
|
} |
398 |
|
|
399 |
+ |
public boolean isEmpty() { |
400 |
+ |
return AbstractMap.this.isEmpty(); |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
public void clear() { |
404 |
+ |
AbstractMap.this.clear(); |
405 |
+ |
} |
406 |
+ |
|
407 |
|
public boolean contains(Object v) { |
408 |
|
return AbstractMap.this.containsValue(v); |
409 |
|
} |
504 |
|
* <tt>", "</tt> (comma and space). Each key-value mapping is rendered as |
505 |
|
* the key followed by an equals sign (<tt>"="</tt>) followed by the |
506 |
|
* associated value. Keys and values are converted to strings as by |
507 |
< |
* <tt>String.valueOf(Object)</tt>.<p> |
507 |
> |
* {@link String#valueOf(Object)}. |
508 |
|
* |
509 |
< |
* This implementation creates an empty string buffer, appends a left |
480 |
< |
* brace, and iterates over the map's <tt>entrySet</tt> view, appending |
481 |
< |
* the string representation of each <tt>map.entry</tt> in turn. After |
482 |
< |
* appending each entry except the last, the string <tt>", "</tt> is |
483 |
< |
* appended. Finally a right brace is appended. A string is obtained |
484 |
< |
* from the stringbuffer, and returned. |
485 |
< |
* |
486 |
< |
* @return a String representation of this map |
509 |
> |
* @return a string representation of this map |
510 |
|
*/ |
511 |
|
public String toString() { |
489 |
– |
StringBuilder sb = new StringBuilder(); |
490 |
– |
sb.append("{"); |
491 |
– |
|
512 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
513 |
< |
boolean hasNext = i.hasNext(); |
514 |
< |
while (hasNext) { |
513 |
> |
if (! i.hasNext()) |
514 |
> |
return "{}"; |
515 |
> |
|
516 |
> |
StringBuilder sb = new StringBuilder(); |
517 |
> |
sb.append('{'); |
518 |
> |
for (;;) { |
519 |
|
Entry<K,V> e = i.next(); |
520 |
|
K key = e.getKey(); |
521 |
< |
V value = e.getValue(); |
522 |
< |
if (key == this) |
523 |
< |
sb.append("(this Map)"); |
524 |
< |
else |
525 |
< |
sb.append(key); |
526 |
< |
sb.append("="); |
527 |
< |
if (value == this) |
528 |
< |
sb.append("(this Map)"); |
505 |
< |
else |
506 |
< |
sb.append(value); |
507 |
< |
hasNext = i.hasNext(); |
508 |
< |
if (hasNext) |
509 |
< |
sb.append(", "); |
510 |
< |
} |
511 |
< |
|
512 |
< |
sb.append("}"); |
513 |
< |
return sb.toString(); |
521 |
> |
V value = e.getValue(); |
522 |
> |
sb.append(key == this ? "(this Map)" : key); |
523 |
> |
sb.append('='); |
524 |
> |
sb.append(value == this ? "(this Map)" : value); |
525 |
> |
if (! i.hasNext()) |
526 |
> |
return sb.append('}').toString(); |
527 |
> |
sb.append(", "); |
528 |
> |
} |
529 |
|
} |
530 |
|
|
531 |
|
/** |
546 |
|
* Test for equality, checking for nulls. |
547 |
|
*/ |
548 |
|
private static boolean eq(Object o1, Object o2) { |
549 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
549 |
> |
return o1 == null ? o2 == null : o1.equals(o2); |
550 |
|
} |
551 |
|
|
552 |
|
// Implementation Note: SimpleEntry and SimpleImmutableEntry |
563 |
|
* facilitates the process of building custom map |
564 |
|
* implementations. For example, it may be convenient to return |
565 |
|
* arrays of <tt>SimpleEntry</tt> instances in method |
566 |
< |
* <tt>Map.entrySet().toArray</tt> |
566 |
> |
* <tt>Map.entrySet().toArray</tt>. |
567 |
> |
* |
568 |
> |
* @since 1.6 |
569 |
|
*/ |
570 |
< |
public static class SimpleEntry<K,V> implements Entry<K,V> { |
570 |
> |
public static class SimpleEntry<K,V> |
571 |
> |
implements Entry<K,V>, java.io.Serializable |
572 |
> |
{ |
573 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
574 |
> |
|
575 |
|
private final K key; |
576 |
|
private V value; |
577 |
|
|
629 |
|
return oldValue; |
630 |
|
} |
631 |
|
|
632 |
+ |
/** |
633 |
+ |
* Compares the specified object with this entry for equality. |
634 |
+ |
* Returns {@code true} if the given object is also a map entry and |
635 |
+ |
* the two entries represent the same mapping. More formally, two |
636 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
637 |
+ |
* if<pre> |
638 |
+ |
* (e1.getKey()==null ? |
639 |
+ |
* e2.getKey()==null : |
640 |
+ |
* e1.getKey().equals(e2.getKey())) |
641 |
+ |
* && |
642 |
+ |
* (e1.getValue()==null ? |
643 |
+ |
* e2.getValue()==null : |
644 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
645 |
+ |
* This ensures that the {@code equals} method works properly across |
646 |
+ |
* different implementations of the {@code Map.Entry} interface. |
647 |
+ |
* |
648 |
+ |
* @param o object to be compared for equality with this map entry |
649 |
+ |
* @return {@code true} if the specified object is equal to this map |
650 |
+ |
* entry |
651 |
+ |
* @see #hashCode |
652 |
+ |
*/ |
653 |
|
public boolean equals(Object o) { |
654 |
|
if (!(o instanceof Map.Entry)) |
655 |
|
return false; |
657 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
658 |
|
} |
659 |
|
|
660 |
+ |
/** |
661 |
+ |
* Returns the hash code value for this map entry. The hash code |
662 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
663 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
664 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
665 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
666 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
667 |
+ |
* {@code e1} and {@code e2}, as required by the general |
668 |
+ |
* contract of {@link Object#hashCode}. |
669 |
+ |
* |
670 |
+ |
* @return the hash code value for this map entry |
671 |
+ |
* @see #equals |
672 |
+ |
*/ |
673 |
|
public int hashCode() { |
674 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
675 |
< |
((value == null) ? 0 : value.hashCode()); |
674 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
675 |
> |
(value == null ? 0 : value.hashCode()); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
690 |
|
} |
691 |
|
|
692 |
|
/** |
693 |
< |
* An Entry maintaining an immutable key and value, This class |
693 |
> |
* An Entry maintaining an immutable key and value. This class |
694 |
|
* does not support method <tt>setValue</tt>. This class may be |
695 |
|
* convenient in methods that return thread-safe snapshots of |
696 |
|
* key-value mappings. |
697 |
+ |
* |
698 |
+ |
* @since 1.6 |
699 |
|
*/ |
700 |
< |
public static class SimpleImmutableEntry<K,V> implements Entry<K,V> { |
700 |
> |
public static class SimpleImmutableEntry<K,V> |
701 |
> |
implements Entry<K,V>, java.io.Serializable |
702 |
> |
{ |
703 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
704 |
> |
|
705 |
|
private final K key; |
706 |
|
private final V value; |
707 |
|
|
760 |
|
throw new UnsupportedOperationException(); |
761 |
|
} |
762 |
|
|
763 |
+ |
/** |
764 |
+ |
* Compares the specified object with this entry for equality. |
765 |
+ |
* Returns {@code true} if the given object is also a map entry and |
766 |
+ |
* the two entries represent the same mapping. More formally, two |
767 |
+ |
* entries {@code e1} and {@code e2} represent the same mapping |
768 |
+ |
* if<pre> |
769 |
+ |
* (e1.getKey()==null ? |
770 |
+ |
* e2.getKey()==null : |
771 |
+ |
* e1.getKey().equals(e2.getKey())) |
772 |
+ |
* && |
773 |
+ |
* (e1.getValue()==null ? |
774 |
+ |
* e2.getValue()==null : |
775 |
+ |
* e1.getValue().equals(e2.getValue()))</pre> |
776 |
+ |
* This ensures that the {@code equals} method works properly across |
777 |
+ |
* different implementations of the {@code Map.Entry} interface. |
778 |
+ |
* |
779 |
+ |
* @param o object to be compared for equality with this map entry |
780 |
+ |
* @return {@code true} if the specified object is equal to this map |
781 |
+ |
* entry |
782 |
+ |
* @see #hashCode |
783 |
+ |
*/ |
784 |
|
public boolean equals(Object o) { |
785 |
|
if (!(o instanceof Map.Entry)) |
786 |
|
return false; |
788 |
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
789 |
|
} |
790 |
|
|
791 |
+ |
/** |
792 |
+ |
* Returns the hash code value for this map entry. The hash code |
793 |
+ |
* of a map entry {@code e} is defined to be: <pre> |
794 |
+ |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
795 |
+ |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
796 |
+ |
* This ensures that {@code e1.equals(e2)} implies that |
797 |
+ |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
798 |
+ |
* {@code e1} and {@code e2}, as required by the general |
799 |
+ |
* contract of {@link Object#hashCode}. |
800 |
+ |
* |
801 |
+ |
* @return the hash code value for this map entry |
802 |
+ |
* @see #equals |
803 |
+ |
*/ |
804 |
|
public int hashCode() { |
805 |
< |
return ((key == null) ? 0 : key.hashCode()) ^ |
806 |
< |
((value == null) ? 0 : value.hashCode()); |
805 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
806 |
> |
(value == null ? 0 : value.hashCode()); |
807 |
|
} |
808 |
|
|
809 |
|
/** |