src/share/classes/java/util/AbstractMap.java

Print this page
rev 4788 : Fix bunch of generics warnings


 426      * of the <tt>Map</tt> interface.
 427      *
 428      * <p>This implementation first checks if the specified object is this map;
 429      * if so it returns <tt>true</tt>.  Then, it checks if the specified
 430      * object is a map whose size is identical to the size of this map; if
 431      * not, it returns <tt>false</tt>.  If so, it iterates over this map's
 432      * <tt>entrySet</tt> collection, and checks that the specified map
 433      * contains each mapping that this map contains.  If the specified map
 434      * fails to contain such a mapping, <tt>false</tt> is returned.  If the
 435      * iteration completes, <tt>true</tt> is returned.
 436      *
 437      * @param o object to be compared for equality with this map
 438      * @return <tt>true</tt> if the specified object is equal to this map
 439      */
 440     public boolean equals(Object o) {
 441         if (o == this)
 442             return true;
 443 
 444         if (!(o instanceof Map))
 445             return false;
 446         Map<K,V> m = (Map<K,V>) o;
 447         if (m.size() != size())
 448             return false;
 449 
 450         try {
 451             Iterator<Entry<K,V>> i = entrySet().iterator();
 452             while (i.hasNext()) {
 453                 Entry<K,V> e = i.next();
 454                 K key = e.getKey();
 455                 V value = e.getValue();
 456                 if (value == null) {
 457                     if (!(m.get(key)==null && m.containsKey(key)))
 458                         return false;
 459                 } else {
 460                     if (!value.equals(m.get(key)))
 461                         return false;
 462                 }
 463             }
 464         } catch (ClassCastException unused) {
 465             return false;
 466         } catch (NullPointerException unused) {


 517         for (;;) {
 518             Entry<K,V> e = i.next();
 519             K key = e.getKey();
 520             V value = e.getValue();
 521             sb.append(key   == this ? "(this Map)" : key);
 522             sb.append('=');
 523             sb.append(value == this ? "(this Map)" : value);
 524             if (! i.hasNext())
 525                 return sb.append('}').toString();
 526             sb.append(',').append(' ');
 527         }
 528     }
 529 
 530     /**
 531      * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
 532      * and values themselves are not cloned.
 533      *
 534      * @return a shallow copy of this map
 535      */
 536     protected Object clone() throws CloneNotSupportedException {
 537         AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
 538         result.keySet = null;
 539         result.values = null;
 540         return result;
 541     }
 542 
 543     /**
 544      * Utility method for SimpleEntry and SimpleImmutableEntry.
 545      * Test for equality, checking for nulls.
 546      */
 547     private static boolean eq(Object o1, Object o2) {
 548         return o1 == null ? o2 == null : o1.equals(o2);
 549     }
 550 
 551     // Implementation Note: SimpleEntry and SimpleImmutableEntry
 552     // are distinct unrelated classes, even though they share
 553     // some code. Since you can't add or subtract final-ness
 554     // of a field in a subclass, they can't share representations,
 555     // and the amount of duplicated code is too small to warrant
 556     // exposing a common abstract class.
 557 


 635          * entries {@code e1} and {@code e2} represent the same mapping
 636          * if<pre>
 637          *   (e1.getKey()==null ?
 638          *    e2.getKey()==null :
 639          *    e1.getKey().equals(e2.getKey()))
 640          *   &amp;&amp;
 641          *   (e1.getValue()==null ?
 642          *    e2.getValue()==null :
 643          *    e1.getValue().equals(e2.getValue()))</pre>
 644          * This ensures that the {@code equals} method works properly across
 645          * different implementations of the {@code Map.Entry} interface.
 646          *
 647          * @param o object to be compared for equality with this map entry
 648          * @return {@code true} if the specified object is equal to this map
 649          *         entry
 650          * @see    #hashCode
 651          */
 652         public boolean equals(Object o) {
 653             if (!(o instanceof Map.Entry))
 654                 return false;
 655             Map.Entry e = (Map.Entry)o;
 656             return eq(key, e.getKey()) && eq(value, e.getValue());
 657         }
 658 
 659         /**
 660          * Returns the hash code value for this map entry.  The hash code
 661          * of a map entry {@code e} is defined to be: <pre>
 662          *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 663          *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
 664          * This ensures that {@code e1.equals(e2)} implies that
 665          * {@code e1.hashCode()==e2.hashCode()} for any two Entries
 666          * {@code e1} and {@code e2}, as required by the general
 667          * contract of {@link Object#hashCode}.
 668          *
 669          * @return the hash code value for this map entry
 670          * @see    #equals
 671          */
 672         public int hashCode() {
 673             return (key   == null ? 0 :   key.hashCode()) ^
 674                    (value == null ? 0 : value.hashCode());
 675         }


 766          * entries {@code e1} and {@code e2} represent the same mapping
 767          * if<pre>
 768          *   (e1.getKey()==null ?
 769          *    e2.getKey()==null :
 770          *    e1.getKey().equals(e2.getKey()))
 771          *   &amp;&amp;
 772          *   (e1.getValue()==null ?
 773          *    e2.getValue()==null :
 774          *    e1.getValue().equals(e2.getValue()))</pre>
 775          * This ensures that the {@code equals} method works properly across
 776          * different implementations of the {@code Map.Entry} interface.
 777          *
 778          * @param o object to be compared for equality with this map entry
 779          * @return {@code true} if the specified object is equal to this map
 780          *         entry
 781          * @see    #hashCode
 782          */
 783         public boolean equals(Object o) {
 784             if (!(o instanceof Map.Entry))
 785                 return false;
 786             Map.Entry e = (Map.Entry)o;
 787             return eq(key, e.getKey()) && eq(value, e.getValue());
 788         }
 789 
 790         /**
 791          * Returns the hash code value for this map entry.  The hash code
 792          * of a map entry {@code e} is defined to be: <pre>
 793          *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 794          *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
 795          * This ensures that {@code e1.equals(e2)} implies that
 796          * {@code e1.hashCode()==e2.hashCode()} for any two Entries
 797          * {@code e1} and {@code e2}, as required by the general
 798          * contract of {@link Object#hashCode}.
 799          *
 800          * @return the hash code value for this map entry
 801          * @see    #equals
 802          */
 803         public int hashCode() {
 804             return (key   == null ? 0 :   key.hashCode()) ^
 805                    (value == null ? 0 : value.hashCode());
 806         }


 426      * of the <tt>Map</tt> interface.
 427      *
 428      * <p>This implementation first checks if the specified object is this map;
 429      * if so it returns <tt>true</tt>.  Then, it checks if the specified
 430      * object is a map whose size is identical to the size of this map; if
 431      * not, it returns <tt>false</tt>.  If so, it iterates over this map's
 432      * <tt>entrySet</tt> collection, and checks that the specified map
 433      * contains each mapping that this map contains.  If the specified map
 434      * fails to contain such a mapping, <tt>false</tt> is returned.  If the
 435      * iteration completes, <tt>true</tt> is returned.
 436      *
 437      * @param o object to be compared for equality with this map
 438      * @return <tt>true</tt> if the specified object is equal to this map
 439      */
 440     public boolean equals(Object o) {
 441         if (o == this)
 442             return true;
 443 
 444         if (!(o instanceof Map))
 445             return false;
 446         Map<?,?> m = (Map<?,?>) o;
 447         if (m.size() != size())
 448             return false;
 449 
 450         try {
 451             Iterator<Entry<K,V>> i = entrySet().iterator();
 452             while (i.hasNext()) {
 453                 Entry<K,V> e = i.next();
 454                 K key = e.getKey();
 455                 V value = e.getValue();
 456                 if (value == null) {
 457                     if (!(m.get(key)==null && m.containsKey(key)))
 458                         return false;
 459                 } else {
 460                     if (!value.equals(m.get(key)))
 461                         return false;
 462                 }
 463             }
 464         } catch (ClassCastException unused) {
 465             return false;
 466         } catch (NullPointerException unused) {


 517         for (;;) {
 518             Entry<K,V> e = i.next();
 519             K key = e.getKey();
 520             V value = e.getValue();
 521             sb.append(key   == this ? "(this Map)" : key);
 522             sb.append('=');
 523             sb.append(value == this ? "(this Map)" : value);
 524             if (! i.hasNext())
 525                 return sb.append('}').toString();
 526             sb.append(',').append(' ');
 527         }
 528     }
 529 
 530     /**
 531      * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
 532      * and values themselves are not cloned.
 533      *
 534      * @return a shallow copy of this map
 535      */
 536     protected Object clone() throws CloneNotSupportedException {
 537         AbstractMap<?,?> result = (AbstractMap<?,?>)super.clone();
 538         result.keySet = null;
 539         result.values = null;
 540         return result;
 541     }
 542 
 543     /**
 544      * Utility method for SimpleEntry and SimpleImmutableEntry.
 545      * Test for equality, checking for nulls.
 546      */
 547     private static boolean eq(Object o1, Object o2) {
 548         return o1 == null ? o2 == null : o1.equals(o2);
 549     }
 550 
 551     // Implementation Note: SimpleEntry and SimpleImmutableEntry
 552     // are distinct unrelated classes, even though they share
 553     // some code. Since you can't add or subtract final-ness
 554     // of a field in a subclass, they can't share representations,
 555     // and the amount of duplicated code is too small to warrant
 556     // exposing a common abstract class.
 557 


 635          * entries {@code e1} and {@code e2} represent the same mapping
 636          * if<pre>
 637          *   (e1.getKey()==null ?
 638          *    e2.getKey()==null :
 639          *    e1.getKey().equals(e2.getKey()))
 640          *   &amp;&amp;
 641          *   (e1.getValue()==null ?
 642          *    e2.getValue()==null :
 643          *    e1.getValue().equals(e2.getValue()))</pre>
 644          * This ensures that the {@code equals} method works properly across
 645          * different implementations of the {@code Map.Entry} interface.
 646          *
 647          * @param o object to be compared for equality with this map entry
 648          * @return {@code true} if the specified object is equal to this map
 649          *         entry
 650          * @see    #hashCode
 651          */
 652         public boolean equals(Object o) {
 653             if (!(o instanceof Map.Entry))
 654                 return false;
 655             Map.Entry<?,?> e = (Map.Entry<?,?>)o;
 656             return eq(key, e.getKey()) && eq(value, e.getValue());
 657         }
 658 
 659         /**
 660          * Returns the hash code value for this map entry.  The hash code
 661          * of a map entry {@code e} is defined to be: <pre>
 662          *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 663          *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
 664          * This ensures that {@code e1.equals(e2)} implies that
 665          * {@code e1.hashCode()==e2.hashCode()} for any two Entries
 666          * {@code e1} and {@code e2}, as required by the general
 667          * contract of {@link Object#hashCode}.
 668          *
 669          * @return the hash code value for this map entry
 670          * @see    #equals
 671          */
 672         public int hashCode() {
 673             return (key   == null ? 0 :   key.hashCode()) ^
 674                    (value == null ? 0 : value.hashCode());
 675         }


 766          * entries {@code e1} and {@code e2} represent the same mapping
 767          * if<pre>
 768          *   (e1.getKey()==null ?
 769          *    e2.getKey()==null :
 770          *    e1.getKey().equals(e2.getKey()))
 771          *   &amp;&amp;
 772          *   (e1.getValue()==null ?
 773          *    e2.getValue()==null :
 774          *    e1.getValue().equals(e2.getValue()))</pre>
 775          * This ensures that the {@code equals} method works properly across
 776          * different implementations of the {@code Map.Entry} interface.
 777          *
 778          * @param o object to be compared for equality with this map entry
 779          * @return {@code true} if the specified object is equal to this map
 780          *         entry
 781          * @see    #hashCode
 782          */
 783         public boolean equals(Object o) {
 784             if (!(o instanceof Map.Entry))
 785                 return false;
 786             Map.Entry<?,?> e = (Map.Entry<?,?>)o;
 787             return eq(key, e.getKey()) && eq(value, e.getValue());
 788         }
 789 
 790         /**
 791          * Returns the hash code value for this map entry.  The hash code
 792          * of a map entry {@code e} is defined to be: <pre>
 793          *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 794          *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
 795          * This ensures that {@code e1.equals(e2)} implies that
 796          * {@code e1.hashCode()==e2.hashCode()} for any two Entries
 797          * {@code e1} and {@code e2}, as required by the general
 798          * contract of {@link Object#hashCode}.
 799          *
 800          * @return the hash code value for this map entry
 801          * @see    #equals
 802          */
 803         public int hashCode() {
 804             return (key   == null ? 0 :   key.hashCode()) ^
 805                    (value == null ? 0 : value.hashCode());
 806         }