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.4 by dl, Tue Mar 8 12:27:06 2005 UTC vs.
Revision 1.24 by jsr166, Sun May 20 07:54:01 2007 UTC

# Line 1 | Line 1
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 2004 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;
# Line 10 | Line 28 | import java.util.Map.Entry;
28  
29   /**
30   * This class provides a skeletal implementation of the <tt>Map</tt>
31 < * interface, to minimize the effort required to implement this interface. <p>
31 > * interface, to minimize the effort required to implement this interface.
32   *
33 < * To implement an unmodifiable map, the programmer needs only to extend this
33 > * <p>To implement an unmodifiable map, the programmer needs only to extend this
34   * class and provide an implementation for the <tt>entrySet</tt> method, which
35   * returns a set-view of the map's mappings.  Typically, the returned set
36   * will, in turn, be implemented atop <tt>AbstractSet</tt>.  This set should
37   * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
38 < * should not support the <tt>remove</tt> method.<p>
38 > * should not support the <tt>remove</tt> method.
39   *
40 < * To implement a modifiable map, the programmer must additionally override
40 > * <p>To implement a modifiable map, the programmer must additionally override
41   * this class's <tt>put</tt> method (which otherwise throws an
42   * <tt>UnsupportedOperationException</tt>), and the iterator returned by
43   * <tt>entrySet().iterator()</tt> must additionally implement its
44 < * <tt>remove</tt> method.<p>
44 > * <tt>remove</tt> method.
45   *
46 < * The programmer should generally provide a void (no argument) and map
46 > * <p>The programmer should generally provide a void (no argument) and map
47   * constructor, as per the recommendation in the <tt>Map</tt> interface
48 < * specification.<p>
48 > * specification.
49   *
50 < * 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.<p>
52 > * map being implemented admits a more efficient implementation.
53   *
54 < * This class is a member of the
55 < * <a href="{@docRoot}/../guide/collections/index.html">
54 > * <p>This class is a member of the
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
59 + * @param <V> the type of mapped values
60 + *
61   * @author  Josh Bloch
62   * @author  Neal Gafter
63   * @version %I%, %G%
# Line 56 | Line 77 | public abstract class AbstractMap<K,V> i
77      // Query Operations
78  
79      /**
80 <     * Returns the number of key-value mappings in this map.  If the map
60 <     * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
61 <     * <tt>Integer.MAX_VALUE</tt>.<p>
62 <     *
63 <     * This implementation returns <tt>entrySet().size()</tt>.
80 >     * {@inheritDoc}
81       *
82 <     * @return the number of key-value mappings in this map.
82 >     * <p>This implementation returns <tt>entrySet().size()</tt>.
83       */
84      public int size() {
85          return entrySet().size();
86      }
87  
88      /**
89 <     * Returns <tt>true</tt> if this map contains no key-value mappings. <p>
73 <     *
74 <     * This implementation returns <tt>size() == 0</tt>.
89 >     * {@inheritDoc}
90       *
91 <     * @return <tt>true</tt> if this map contains no key-value mappings.
91 >     * <p>This implementation returns <tt>size() == 0</tt>.
92       */
93      public boolean isEmpty() {
94          return size() == 0;
95      }
96  
97      /**
98 <     * Returns <tt>true</tt> if this map maps one or more keys to this value.
99 <     * More formally, returns <tt>true</tt> if and only if this map contains
100 <     * at least one mapping to a value <tt>v</tt> such that <tt>(value==null ?
101 <     * v==null : value.equals(v))</tt>.  This operation will probably require
102 <     * time linear in the map size for most implementations of map.<p>
103 <     *
104 <     * This implementation iterates over entrySet() searching for an entry
105 <     * with the specified value.  If such an entry is found, <tt>true</tt> is
106 <     * returned.  If the iteration terminates without finding such an entry,
107 <     * <tt>false</tt> is returned.  Note that this implementation requires
93 <     * linear time in the size of the map.
94 <     *
95 <     * @param value value whose presence in this map is to be tested.
96 <     *
97 <     * @return <tt>true</tt> if this map maps one or more keys to this value.
98 >     * {@inheritDoc}
99 >     *
100 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
101 >     * for an entry with the specified value.  If such an entry is found,
102 >     * <tt>true</tt> is returned.  If the iteration terminates without
103 >     * finding such an entry, <tt>false</tt> is returned.  Note that this
104 >     * implementation requires linear time in the size of the map.
105 >     *
106 >     * @throws ClassCastException   {@inheritDoc}
107 >     * @throws NullPointerException {@inheritDoc}
108       */
109      public boolean containsValue(Object value) {
110          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 115 | Line 125 | public abstract class AbstractMap<K,V> i
125      }
126  
127      /**
128 <     * Returns <tt>true</tt> if this map contains a mapping for the specified
129 <     * key. <p>
128 >     * {@inheritDoc}
129 >     *
130 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
131 >     * for an entry with the specified key.  If such an entry is found,
132 >     * <tt>true</tt> is returned.  If the iteration terminates without
133 >     * finding such an entry, <tt>false</tt> is returned.  Note that this
134 >     * implementation requires linear time in the size of the map; many
135 >     * implementations will override this method.
136       *
137 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
138 <     * entry with the specified key.  If such an entry is found, <tt>true</tt>
123 <     * is returned.  If the iteration terminates without finding such an
124 <     * entry, <tt>false</tt> is returned.  Note that this implementation
125 <     * requires linear time in the size of the map; many implementations will
126 <     * override this method.
127 <     *
128 <     * @param key key whose presence in this map is to be tested.
129 <     * @return <tt>true</tt> if this map contains a mapping for the specified
130 <     *            key.
131 <     *
132 <     * @throws NullPointerException if the key is <tt>null</tt> and this map
133 <     *            does not permit <tt>null</tt> keys.
137 >     * @throws ClassCastException   {@inheritDoc}
138 >     * @throws NullPointerException {@inheritDoc}
139       */
140      public boolean containsKey(Object key) {
141          Iterator<Map.Entry<K,V>> i = entrySet().iterator();
# Line 151 | Line 156 | public abstract class AbstractMap<K,V> i
156      }
157  
158      /**
159 <     * Returns the value to which this map maps the specified key.  Returns
160 <     * <tt>null</tt> if the map contains no mapping for this key.  A return
161 <     * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
162 <     * map contains no mapping for the key; it's also possible that the map
163 <     * explicitly maps the key to <tt>null</tt>.  The containsKey operation
164 <     * may be used to distinguish these two cases. <p>
165 <     *
166 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
167 <     * entry with the specified key.  If such an entry is found, the entry's
168 <     * value is returned.  If the iteration terminates without finding such an
169 <     * entry, <tt>null</tt> is returned.  Note that this implementation
165 <     * requires linear time in the size of the map; many implementations will
166 <     * override this method.
167 <     *
168 <     * @param key key whose associated value is to be returned.
169 <     * @return the value to which this map maps the specified key.
170 <     *
171 <     * @throws NullPointerException if the key is <tt>null</tt> and this map
172 <     *            does not permit <tt>null</tt> keys.
173 <     *
174 <     * @see #containsKey(Object)
159 >     * {@inheritDoc}
160 >     *
161 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
162 >     * for an entry with the specified key.  If such an entry is found,
163 >     * the entry's value is returned.  If the iteration terminates without
164 >     * finding such an entry, <tt>null</tt> is returned.  Note that this
165 >     * implementation requires linear time in the size of the map; many
166 >     * implementations will override this method.
167 >     *
168 >     * @throws ClassCastException            {@inheritDoc}
169 >     * @throws NullPointerException          {@inheritDoc}
170       */
171      public V get(Object key) {
172          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 195 | Line 190 | public abstract class AbstractMap<K,V> i
190      // Modification Operations
191  
192      /**
193 <     * Associates the specified value with the specified key in this map
199 <     * (optional operation).  If the map previously contained a mapping for
200 <     * this key, the old value is replaced.<p>
193 >     * {@inheritDoc}
194       *
195 <     * This implementation always throws an
195 >     * <p>This implementation always throws an
196       * <tt>UnsupportedOperationException</tt>.
197       *
198 <     * @param key key with which the specified value is to be associated.
199 <     * @param value value to be associated with the specified key.
200 <     *
201 <     * @return previous value associated with specified key, or <tt>null</tt>
209 <     *         if there was no mapping for key.  (A <tt>null</tt> return can
210 <     *         also indicate that the map previously associated <tt>null</tt>
211 <     *         with the specified key, if the implementation supports
212 <     *         <tt>null</tt> values.)
213 <     *
214 <     * @throws UnsupportedOperationException if the <tt>put</tt> operation is
215 <     *            not supported by this map.
216 <     *
217 <     * @throws ClassCastException if the class of the specified key or value
218 <     *            prevents it from being stored in this map.
219 <     *
220 <     * @throws IllegalArgumentException if some aspect of this key or value *
221 <     *            prevents it from being stored in this map.
222 <     *
223 <     * @throws NullPointerException if this map does not permit <tt>null</tt>
224 <     *            keys or values, and the specified key or value is
225 <     *            <tt>null</tt>.
198 >     * @throws UnsupportedOperationException {@inheritDoc}
199 >     * @throws ClassCastException            {@inheritDoc}
200 >     * @throws NullPointerException          {@inheritDoc}
201 >     * @throws IllegalArgumentException      {@inheritDoc}
202       */
203      public V put(K key, V value) {
204          throw new UnsupportedOperationException();
205      }
206  
207      /**
208 <     * Removes the mapping for this key from this map if present (optional
233 <     * operation). <p>
208 >     * {@inheritDoc}
209       *
210 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
210 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
211       * entry with the specified key.  If such an entry is found, its value is
212       * obtained with its <tt>getValue</tt> operation, the entry is removed
213 <     * from the Collection (and the backing map) with the iterator's
213 >     * from the collection (and the backing map) with the iterator's
214       * <tt>remove</tt> operation, and the saved value is returned.  If the
215       * iteration terminates without finding such an entry, <tt>null</tt> is
216       * returned.  Note that this implementation requires linear time in the
217 <     * size of the map; many implementations will override this method.<p>
217 >     * size of the map; many implementations will override this method.
218       *
219 <     * Note that this implementation throws an
220 <     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> iterator
221 <     * does not support the <tt>remove</tt> method and this map contains a
222 <     * mapping for the specified key.
223 <     *
224 <     * @param key key whose mapping is to be removed from the map.
225 <     * @return previous value associated with specified key, or <tt>null</tt>
226 <     *         if there was no entry for key.  (A <tt>null</tt> return can
252 <     *         also indicate that the map previously associated <tt>null</tt>
253 <     *         with the specified key, if the implementation supports
254 <     *         <tt>null</tt> values.)
255 <     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
256 <     *            is not supported by this map.
219 >     * <p>Note that this implementation throws an
220 >     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
221 >     * iterator does not support the <tt>remove</tt> method and this map
222 >     * contains a mapping for the specified key.
223 >     *
224 >     * @throws UnsupportedOperationException {@inheritDoc}
225 >     * @throws ClassCastException            {@inheritDoc}
226 >     * @throws NullPointerException          {@inheritDoc}
227       */
228      public V remove(Object key) {
229          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 284 | Line 254 | public abstract class AbstractMap<K,V> i
254      // Bulk Operations
255  
256      /**
257 <     * Copies all of the mappings from the specified map to this map
288 <     * (optional operation).  These mappings will replace any mappings that
289 <     * this map had for any of the keys currently in the specified map.<p>
257 >     * {@inheritDoc}
258       *
259 <     * This implementation iterates over the specified map's
259 >     * <p>This implementation iterates over the specified map's
260       * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
261 <     * operation once for each entry returned by the iteration.<p>
261 >     * operation once for each entry returned by the iteration.
262       *
263 <     * Note that this implementation throws an
263 >     * <p>Note that this implementation throws an
264       * <tt>UnsupportedOperationException</tt> if this map does not support
265       * the <tt>put</tt> operation and the specified map is nonempty.
266       *
267 <     * @param t mappings to be stored in this map.
268 <     *
269 <     * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
270 <     *            is not supported by this map.
271 <     *
272 <     * @throws ClassCastException if the class of a key or value in the
273 <     *            specified map prevents it from being stored in this map.
274 <     *
307 <     * @throws IllegalArgumentException if some aspect of a key or value in
308 <     *            the specified map prevents it from being stored in this map.
309 <     * @throws NullPointerException if the specified map is <tt>null</tt>, or if
310 <     *         this map does not permit <tt>null</tt> keys or values, and the
311 <     *         specified map contains <tt>null</tt> keys or values.
312 <     */
313 <    public void putAll(Map<? extends K, ? extends V> t) {
314 <        Iterator<? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator();
315 <        while (i.hasNext()) {
316 <            Entry<? extends K, ? extends V> e = i.next();
317 <            put(e.getKey(), e.getValue());
318 <        }
267 >     * @throws UnsupportedOperationException {@inheritDoc}
268 >     * @throws ClassCastException            {@inheritDoc}
269 >     * @throws NullPointerException          {@inheritDoc}
270 >     * @throws IllegalArgumentException      {@inheritDoc}
271 >     */
272 >    public void putAll(Map<? extends K, ? extends V> m) {
273 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
274 >            put(e.getKey(), e.getValue());
275      }
276  
277      /**
278 <     * Removes all mappings from this map (optional operation). <p>
278 >     * {@inheritDoc}
279       *
280 <     * This implementation calls <tt>entrySet().clear()</tt>.
280 >     * <p>This implementation calls <tt>entrySet().clear()</tt>.
281       *
282 <     * Note that this implementation throws an
282 >     * <p>Note that this implementation throws an
283       * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
284       * does not support the <tt>clear</tt> operation.
285       *
286 <     * @throws    UnsupportedOperationException clear is not supported
331 <     *            by this map.
286 >     * @throws UnsupportedOperationException {@inheritDoc}
287       */
288      public void clear() {
289          entrySet().clear();
# Line 346 | Line 301 | public abstract class AbstractMap<K,V> i
301      transient volatile Collection<V> values = null;
302  
303      /**
304 <     * Returns a Set view of the keys contained in this map.  The Set is
305 <     * backed by the map, so changes to the map are reflected in the Set,
306 <     * and vice-versa.  (If the map is modified while an iteration over
307 <     * the Set is in progress, the results of the iteration are undefined.)
308 <     * The Set supports element removal, which removes the corresponding entry
309 <     * from the map, via the Iterator.remove, Set.remove,  removeAll
310 <     * retainAll, and clear operations.  It does not support the add or
311 <     * addAll operations.<p>
357 <     *
358 <     * This implementation returns a Set that subclasses
359 <     * AbstractSet.  The subclass's iterator method returns a "wrapper
360 <     * object" over this map's entrySet() iterator.  The size method delegates
361 <     * to this map's size method and the contains method delegates to this
362 <     * map's containsKey method.<p>
304 >     * {@inheritDoc}
305 >     *
306 >     * <p>This implementation returns a set that subclasses {@link AbstractSet}.
307 >     * The subclass's iterator method returns a "wrapper object" over this
308 >     * map's <tt>entrySet()</tt> iterator.  The <tt>size</tt> method
309 >     * delegates to this map's <tt>size</tt> method and the
310 >     * <tt>contains</tt> method delegates to this map's
311 >     * <tt>containsKey</tt> method.
312       *
313 <     * The Set is created the first time this method is called,
313 >     * <p>The set is created the first time this method is called,
314       * and returned in response to all subsequent calls.  No synchronization
315       * is performed, so there is a slight chance that multiple calls to this
316 <     * method will not all return the same Set.
368 <     *
369 <     * @return a Set view of the keys contained in this map.
316 >     * method will not all return the same set.
317       */
318      public Set<K> keySet() {
319          if (keySet == null) {
# Line 402 | Line 349 | public abstract class AbstractMap<K,V> i
349      }
350  
351      /**
352 <     * Returns a collection view of the values contained in this map.  The
353 <     * collection is backed by the map, so changes to the map are reflected in
354 <     * the collection, and vice-versa.  (If the map is modified while an
355 <     * iteration over the collection is in progress, the results of the
356 <     * iteration are undefined.)  The collection supports element removal,
357 <     * which removes the corresponding entry from the map, via the
358 <     * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
359 <     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations.
413 <     * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.<p>
414 <     *
415 <     * This implementation returns a collection that subclasses abstract
416 <     * collection.  The subclass's iterator method returns a "wrapper object"
417 <     * over this map's <tt>entrySet()</tt> iterator.  The size method
418 <     * delegates to this map's size method and the contains method delegates
419 <     * to this map's containsValue method.<p>
352 >     * {@inheritDoc}
353 >     *
354 >     * <p>This implementation returns a collection that subclasses {@link
355 >     * AbstractCollection}.  The subclass's iterator method returns a
356 >     * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
357 >     * The <tt>size</tt> method delegates to this map's <tt>size</tt>
358 >     * method and the <tt>contains</tt> method delegates to this map's
359 >     * <tt>containsValue</tt> method.
360       *
361 <     * The collection is created the first time this method is called, and
361 >     * <p>The collection is created the first time this method is called, and
362       * returned in response to all subsequent calls.  No synchronization is
363       * performed, so there is a slight chance that multiple calls to this
364 <     * method will not all return the same Collection.
425 <     *
426 <     * @return a collection view of the values contained in this map.
364 >     * method will not all return the same collection.
365       */
366      public Collection<V> values() {
367          if (values == null) {
# Line 458 | Line 396 | public abstract class AbstractMap<K,V> i
396          return values;
397      }
398  
461    /**
462     * Returns a set view of the mappings contained in this map.  Each element
463     * in this set is a Map.Entry.  The set is backed by the map, so changes
464     * to the map are reflected in the set, and vice-versa.  (If the map is
465     * modified while an iteration over the set is in progress, the results of
466     * the iteration are undefined.)  The set supports element removal, which
467     * removes the corresponding entry from the map, via the
468     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
469     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not support
470     * the <tt>add</tt> or <tt>addAll</tt> operations.
471     *
472     * @return a set view of the mappings contained in this map.
473     */
399      public abstract Set<Entry<K,V>> entrySet();
400  
401  
# Line 479 | Line 404 | public abstract class AbstractMap<K,V> i
404      /**
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>t1</tt> and
408 <     * <tt>t2</tt> represent the same mappings if
409 <     * <tt>t1.keySet().equals(t2.keySet())</tt> and for every key <tt>k</tt>
485 <     * in <tt>t1.keySet()</tt>, <tt> (t1.get(k)==null ? t2.get(k)==null :
486 <     * t1.get(k).equals(t2.get(k))) </tt>.  This ensures that the
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 map interface.<p>
411 >     * of the <tt>Map</tt> interface.
412       *
413 <     * This implementation first checks if the specified object is this map;
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
415 <     * object is a map whose size is identical to the size of this set; if
415 >     * object is a map whose size is identical to the size of this map; if
416       * not, it returns <tt>false</tt>.  If so, it iterates over this map's
417       * <tt>entrySet</tt> collection, and checks that the specified map
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.
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)
# Line 505 | Line 428 | public abstract class AbstractMap<K,V> i
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 {
# Line 516 | Line 439 | public abstract class AbstractMap<K,V> i
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              }
449 <        } catch(ClassCastException unused) {
449 >        } catch (ClassCastException unused) {
450              return false;
451 <        } catch(NullPointerException unused) {
451 >        } catch (NullPointerException unused) {
452              return false;
453          }
454  
# Line 535 | Line 458 | public abstract class AbstractMap<K,V> i
458      /**
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>t1.equals(t2)</tt>
462 <     * implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
463 <     * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of
464 <     * Object.hashCode.<p>
465 <     *
466 <     * This implementation iterates over <tt>entrySet()</tt>, calling
467 <     * <tt>hashCode</tt> on each element (entry) in the Collection, and adding
468 <     * up the results.
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 >     * {@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.
470 >     * @return the hash code value for this map
471       * @see Map.Entry#hashCode()
549     * @see Object#hashCode()
472       * @see Object#equals(Object)
473       * @see Set#equals(Object)
474       */
# Line 566 | Line 488 | public abstract class AbstractMap<K,V> i
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>
570 <     *
571 <     * This implementation creates an empty string buffer, appends a left
572 <     * brace, and iterates over the map's <tt>entrySet</tt> view, appending
573 <     * the string representation of each <tt>map.entry</tt> in turn.  After
574 <     * appending each entry except the last, the string <tt>", "</tt> is
575 <     * appended.  Finally a right brace is appended.  A string is obtained
576 <     * from the stringbuffer, and returned.
491 >     * {@link String#valueOf(Object)}.
492       *
493 <     * @return a String representation of this map.
493 >     * @return a string representation of this map
494       */
495      public String toString() {
581        StringBuffer buf = new StringBuffer();
582        buf.append("{");
583
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 <                buf.append("(this Map)");
508 <            else
509 <                buf.append(key);
510 <            buf.append("=");
511 <            if (value == this)
512 <                buf.append("(this Map)");
597 <            else
598 <                buf.append(value);
599 <            hasNext = i.hasNext();
600 <            if (hasNext)
601 <                buf.append(", ");
602 <        }
603 <
604 <        buf.append("}");
605 <        return buf.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 <    
514 >
515      /**
516       * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
517       * and values themselves are not cloned.
518       *
519 <     * @return a shallow copy of this map.
519 >     * @return a shallow copy of this map
520       */
521      protected Object clone() throws CloneNotSupportedException {
522          AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
# Line 623 | Line 530 | public abstract class AbstractMap<K,V> i
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
# Line 640 | Line 547 | public abstract class AbstractMap<K,V> i
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  
# Line 662 | Line 575 | public abstract class AbstractMap<K,V> i
575           * Creates an entry representing the same mapping as the
576           * specified entry.
577           *
578 <         * @param entry the entry to copy.
578 >         * @param entry the entry to copy
579           */
580          public SimpleEntry(Entry<? extends K, ? extends V> entry) {
581              this.key   = entry.getKey();
# Line 672 | Line 585 | public abstract class AbstractMap<K,V> i
585          /**
586           * Returns the key corresponding to this entry.
587           *
588 <         * @return the key corresponding to this entry.
588 >         * @return the key corresponding to this entry
589           */
590          public K getKey() {
591              return key;
# Line 681 | Line 594 | public abstract class AbstractMap<K,V> i
594          /**
595           * Returns the value corresponding to this entry.
596           *
597 <         * @return the value corresponding to this entry.
597 >         * @return the value corresponding to this entry
598           */
599          public V getValue() {
600              return value;
# Line 691 | Line 604 | public abstract class AbstractMap<K,V> i
604           * Replaces the value corresponding to this entry with the specified
605           * value.
606           *
607 <         * @param value new value to be stored in this entry.
608 <         * @return old value corresponding to the entry.
607 >         * @param value new value to be stored in this entry
608 >         * @return the old value corresponding to the entry
609           */
610          public V setValue(V value) {
611              V oldValue = this.value;
# Line 700 | Line 613 | public abstract class AbstractMap<K,V> i
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 +         *   &amp;&amp;
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;
# Line 707 | Line 641 | public abstract class AbstractMap<K,V> i
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          /**
# Line 718 | Line 665 | public abstract class AbstractMap<K,V> i
665           * entry's key followed by the equals character ("<tt>=</tt>")
666           * followed by the string representation of this entry's value.
667           *
668 <         * @return a String representation of this map entry.
668 >         * @return a String representation of this map entry
669           */
670          public String toString() {
671              return key + "=" + value;
# Line 727 | Line 674 | public abstract class AbstractMap<K,V> i
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  
# Line 752 | Line 705 | public abstract class AbstractMap<K,V> i
705           * Creates an entry representing the same mapping as the
706           * specified entry.
707           *
708 <         * @param entry the entry to copy.
708 >         * @param entry the entry to copy
709           */
710          public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
711              this.key   = entry.getKey();
# Line 762 | Line 715 | public abstract class AbstractMap<K,V> i
715          /**
716           * Returns the key corresponding to this entry.
717           *
718 <         * @return the key corresponding to this entry.
718 >         * @return the key corresponding to this entry
719           */
720          public K getKey() {
721              return key;
# Line 771 | Line 724 | public abstract class AbstractMap<K,V> i
724          /**
725           * Returns the value corresponding to this entry.
726           *
727 <         * @return the value corresponding to this entry.
727 >         * @return the value corresponding to this entry
728           */
729          public V getValue() {
730              return value;
# Line 783 | Line 736 | public abstract class AbstractMap<K,V> i
736           * <tt>UnsupportedOperationException</tt>, as this class implements
737           * an <i>immutable</i> map entry.
738           *
739 <         * @param value new value to be stored in this entry.
739 >         * @param value new value to be stored in this entry
740           * @return (Does not return)
741           * @throws UnsupportedOperationException always
742           */
# Line 791 | Line 744 | public abstract class AbstractMap<K,V> i
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 +         *   &amp;&amp;
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;
# Line 798 | Line 772 | public abstract class AbstractMap<K,V> i
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          /**
# Line 809 | Line 796 | public abstract class AbstractMap<K,V> i
796           * entry's key followed by the equals character ("<tt>=</tt>")
797           * followed by the string representation of this entry's value.
798           *
799 <         * @return a String representation of this map entry.
799 >         * @return a String representation of this map entry
800           */
801          public String toString() {
802              return key + "=" + value;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines