ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/AbstractMap.java (file contents):
Revision 1.14 by jsr166, Tue Jun 21 07:43:09 2005 UTC vs.
Revision 1.21 by jsr166, Sun May 28 23:36:29 2006 UTC

# Line 1 | Line 1
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  
8   package java.util;
9 import java.util.*; // for javadoc (till 6280605 is fixed)
9   import java.util.Map.Entry;
10  
11   /**
# Line 35 | Line 34 | import java.util.Map.Entry;
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
# Line 253 | Line 252 | public abstract class AbstractMap<K,V> i
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()) {
258 <            Entry<? extends K, ? extends V> e = i.next();
259 <            put(e.getKey(), e.getValue());
260 <        }
255 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
256 >            put(e.getKey(), e.getValue());
257      }
258  
259      /**
# Line 474 | Line 470 | public abstract class AbstractMap<K,V> i
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>
478 <     *
479 <     * 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.
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() {
489        StringBuilder sb = new StringBuilder();
490        sb.append("{");
491
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 <                sb.append("(this Map)");
490 <            else
491 <                sb.append(key);
492 <            sb.append("=");
493 <            if (value == this)
494 <                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();
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      /**
# Line 531 | Line 512 | public abstract class AbstractMap<K,V> i
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
# Line 548 | Line 529 | public abstract class AbstractMap<K,V> i
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  
# Line 608 | Line 595 | public abstract class AbstractMap<K,V> i
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 +         *   &amp;&amp;
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;
# Line 615 | Line 623 | public abstract class AbstractMap<K,V> i
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          /**
# Line 635 | Line 656 | public abstract class AbstractMap<K,V> i
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  
# Line 699 | Line 726 | public abstract class AbstractMap<K,V> i
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 +         *   &amp;&amp;
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;
# Line 706 | Line 754 | public abstract class AbstractMap<K,V> i
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          /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines