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.2 by dl, Sun Mar 6 12:06:17 2005 UTC vs.
Revision 1.23 by jsr166, Sun Jan 7 07:38:27 2007 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
# Line 10 | Line 10 | import java.util.Map.Entry;
10  
11   /**
12   * This class provides a skeletal implementation of the <tt>Map</tt>
13 < * interface, to minimize the effort required to implement this interface. <p>
13 > * interface, to minimize the effort required to implement this interface.
14   *
15 < * To implement an unmodifiable map, the programmer needs only to extend this
15 > * <p>To implement an unmodifiable map, the programmer needs only to extend this
16   * class and provide an implementation for the <tt>entrySet</tt> method, which
17   * returns a set-view of the map's mappings.  Typically, the returned set
18   * will, in turn, be implemented atop <tt>AbstractSet</tt>.  This set should
19   * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
20 < * should not support the <tt>remove</tt> method.<p>
20 > * should not support the <tt>remove</tt> method.
21   *
22 < * To implement a modifiable map, the programmer must additionally override
22 > * <p>To implement a modifiable map, the programmer must additionally override
23   * this class's <tt>put</tt> method (which otherwise throws an
24   * <tt>UnsupportedOperationException</tt>), and the iterator returned by
25   * <tt>entrySet().iterator()</tt> must additionally implement its
26 < * <tt>remove</tt> method.<p>
26 > * <tt>remove</tt> method.
27   *
28 < * The programmer should generally provide a void (no argument) and map
28 > * <p>The programmer should generally provide a void (no argument) and map
29   * constructor, as per the recommendation in the <tt>Map</tt> interface
30 < * specification.<p>
30 > * specification.
31   *
32 < * The documentation for each non-abstract methods in this class describes its
32 > * <p>The documentation for each non-abstract method in this class describes its
33   * implementation in detail.  Each of these methods may be overridden if the
34 < * map being implemented admits a more efficient implementation.<p>
34 > * map being implemented admits a more efficient implementation.
35   *
36 < * This class is a member of the
37 < * <a href="{@docRoot}/../guide/collections/index.html">
36 > * <p>This class is a member of the
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
41 + * @param <V> the type of mapped values
42 + *
43   * @author  Josh Bloch
44   * @author  Neal Gafter
45   * @version %I%, %G%
# Line 56 | Line 59 | public abstract class AbstractMap<K,V> i
59      // Query Operations
60  
61      /**
62 <     * 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>.
62 >     * {@inheritDoc}
63       *
64 <     * @return the number of key-value mappings in this map.
64 >     * <p>This implementation returns <tt>entrySet().size()</tt>.
65       */
66      public int size() {
67          return entrySet().size();
68      }
69  
70      /**
71 <     * Returns <tt>true</tt> if this map contains no key-value mappings. <p>
73 <     *
74 <     * This implementation returns <tt>size() == 0</tt>.
71 >     * {@inheritDoc}
72       *
73 <     * @return <tt>true</tt> if this map contains no key-value mappings.
73 >     * <p>This implementation returns <tt>size() == 0</tt>.
74       */
75      public boolean isEmpty() {
76          return size() == 0;
77      }
78  
79      /**
80 <     * Returns <tt>true</tt> if this map maps one or more keys to this value.
81 <     * More formally, returns <tt>true</tt> if and only if this map contains
82 <     * at least one mapping to a value <tt>v</tt> such that <tt>(value==null ?
83 <     * v==null : value.equals(v))</tt>.  This operation will probably require
84 <     * time linear in the map size for most implementations of map.<p>
85 <     *
86 <     * This implementation iterates over entrySet() searching for an entry
87 <     * with the specified value.  If such an entry is found, <tt>true</tt> is
88 <     * returned.  If the iteration terminates without finding such an entry,
89 <     * <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.
80 >     * {@inheritDoc}
81 >     *
82 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
83 >     * for an entry with the specified value.  If such an entry is found,
84 >     * <tt>true</tt> is returned.  If the iteration terminates without
85 >     * finding such an entry, <tt>false</tt> is returned.  Note that this
86 >     * implementation requires linear time in the size of the map.
87 >     *
88 >     * @throws ClassCastException   {@inheritDoc}
89 >     * @throws NullPointerException {@inheritDoc}
90       */
91      public boolean containsValue(Object value) {
92          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 115 | Line 107 | public abstract class AbstractMap<K,V> i
107      }
108  
109      /**
110 <     * Returns <tt>true</tt> if this map contains a mapping for the specified
119 <     * key. <p>
110 >     * {@inheritDoc}
111       *
112 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
113 <     * entry with the specified key.  If such an entry is found, <tt>true</tt>
114 <     * is returned.  If the iteration terminates without finding such an
115 <     * entry, <tt>false</tt> is returned.  Note that this implementation
116 <     * requires linear time in the size of the map; many implementations will
117 <     * override this method.
118 <     *
119 <     * @param key key whose presence in this map is to be tested.
120 <     * @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.
112 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
113 >     * for an entry with the specified key.  If such an entry is found,
114 >     * <tt>true</tt> is returned.  If the iteration terminates without
115 >     * finding such an entry, <tt>false</tt> is returned.  Note that this
116 >     * implementation requires linear time in the size of the map; many
117 >     * implementations will override this method.
118 >     *
119 >     * @throws ClassCastException   {@inheritDoc}
120 >     * @throws NullPointerException {@inheritDoc}
121       */
122      public boolean containsKey(Object key) {
123          Iterator<Map.Entry<K,V>> i = entrySet().iterator();
# Line 151 | Line 138 | public abstract class AbstractMap<K,V> i
138      }
139  
140      /**
141 <     * Returns the value to which this map maps the specified key.  Returns
142 <     * <tt>null</tt> if the map contains no mapping for this key.  A return
143 <     * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
144 <     * map contains no mapping for the key; it's also possible that the map
145 <     * explicitly maps the key to <tt>null</tt>.  The containsKey operation
146 <     * may be used to distinguish these two cases. <p>
147 <     *
148 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
149 <     * entry with the specified key.  If such an entry is found, the entry's
150 <     * value is returned.  If the iteration terminates without finding such an
151 <     * 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)
141 >     * {@inheritDoc}
142 >     *
143 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching
144 >     * for an entry with the specified key.  If such an entry is found,
145 >     * the entry's value is returned.  If the iteration terminates without
146 >     * finding such an entry, <tt>null</tt> is returned.  Note that this
147 >     * implementation requires linear time in the size of the map; many
148 >     * implementations will override this method.
149 >     *
150 >     * @throws ClassCastException            {@inheritDoc}
151 >     * @throws NullPointerException          {@inheritDoc}
152       */
153      public V get(Object key) {
154          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 195 | Line 172 | public abstract class AbstractMap<K,V> i
172      // Modification Operations
173  
174      /**
175 <     * 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>
175 >     * {@inheritDoc}
176       *
177 <     * This implementation always throws an
177 >     * <p>This implementation always throws an
178       * <tt>UnsupportedOperationException</tt>.
179       *
180 <     * @param key key with which the specified value is to be associated.
181 <     * @param value value to be associated with the specified key.
182 <     *
183 <     * @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>.
180 >     * @throws UnsupportedOperationException {@inheritDoc}
181 >     * @throws ClassCastException            {@inheritDoc}
182 >     * @throws NullPointerException          {@inheritDoc}
183 >     * @throws IllegalArgumentException      {@inheritDoc}
184       */
185      public V put(K key, V value) {
186          throw new UnsupportedOperationException();
187      }
188  
189      /**
190 <     * Removes the mapping for this key from this map if present (optional
233 <     * operation). <p>
190 >     * {@inheritDoc}
191       *
192 <     * This implementation iterates over <tt>entrySet()</tt> searching for an
192 >     * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
193       * entry with the specified key.  If such an entry is found, its value is
194       * obtained with its <tt>getValue</tt> operation, the entry is removed
195 <     * from the Collection (and the backing map) with the iterator's
195 >     * from the collection (and the backing map) with the iterator's
196       * <tt>remove</tt> operation, and the saved value is returned.  If the
197       * iteration terminates without finding such an entry, <tt>null</tt> is
198       * returned.  Note that this implementation requires linear time in the
199 <     * size of the map; many implementations will override this method.<p>
199 >     * size of the map; many implementations will override this method.
200 >     *
201 >     * <p>Note that this implementation throws an
202 >     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
203 >     * iterator does not support the <tt>remove</tt> method and this map
204 >     * contains a mapping for the specified key.
205       *
206 <     * Note that this implementation throws an
207 <     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt> iterator
208 <     * does not support the <tt>remove</tt> method and this map contains a
247 <     * mapping for the specified key.
248 <     *
249 <     * @param key key whose mapping is to be removed from the map.
250 <     * @return previous value associated with specified key, or <tt>null</tt>
251 <     *         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.
206 >     * @throws UnsupportedOperationException {@inheritDoc}
207 >     * @throws ClassCastException            {@inheritDoc}
208 >     * @throws NullPointerException          {@inheritDoc}
209       */
210      public V remove(Object key) {
211          Iterator<Entry<K,V>> i = entrySet().iterator();
# Line 284 | Line 236 | public abstract class AbstractMap<K,V> i
236      // Bulk Operations
237  
238      /**
239 <     * 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>
239 >     * {@inheritDoc}
240       *
241 <     * This implementation iterates over the specified map's
241 >     * <p>This implementation iterates over the specified map's
242       * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
243 <     * operation once for each entry returned by the iteration.<p>
243 >     * operation once for each entry returned by the iteration.
244       *
245 <     * Note that this implementation throws an
245 >     * <p>Note that this implementation throws an
246       * <tt>UnsupportedOperationException</tt> if this map does not support
247       * the <tt>put</tt> operation and the specified map is nonempty.
248       *
249 <     * @param t mappings to be stored in this map.
250 <     *
251 <     * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
252 <     *            is not supported by this map.
253 <     *
254 <     * @throws ClassCastException if the class of a key or value in the
255 <     *            specified map prevents it from being stored in this map.
256 <     *
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 <        }
249 >     * @throws UnsupportedOperationException {@inheritDoc}
250 >     * @throws ClassCastException            {@inheritDoc}
251 >     * @throws NullPointerException          {@inheritDoc}
252 >     * @throws IllegalArgumentException      {@inheritDoc}
253 >     */
254 >    public void putAll(Map<? extends K, ? extends V> m) {
255 >        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
256 >            put(e.getKey(), e.getValue());
257      }
258  
259      /**
260 <     * Removes all mappings from this map (optional operation). <p>
260 >     * {@inheritDoc}
261       *
262 <     * This implementation calls <tt>entrySet().clear()</tt>.
262 >     * <p>This implementation calls <tt>entrySet().clear()</tt>.
263       *
264 <     * Note that this implementation throws an
264 >     * <p>Note that this implementation throws an
265       * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
266       * does not support the <tt>clear</tt> operation.
267       *
268 <     * @throws    UnsupportedOperationException clear is not supported
331 <     *            by this map.
268 >     * @throws UnsupportedOperationException {@inheritDoc}
269       */
270      public void clear() {
271          entrySet().clear();
# Line 346 | Line 283 | public abstract class AbstractMap<K,V> i
283      transient volatile Collection<V> values = null;
284  
285      /**
286 <     * Returns a Set view of the keys contained in this map.  The Set is
287 <     * backed by the map, so changes to the map are reflected in the Set,
288 <     * and vice-versa.  (If the map is modified while an iteration over
289 <     * the Set is in progress, the results of the iteration are undefined.)
290 <     * The Set supports element removal, which removes the corresponding entry
291 <     * from the map, via the Iterator.remove, Set.remove,  removeAll
292 <     * retainAll, and clear operations.  It does not support the add or
293 <     * 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>
286 >     * {@inheritDoc}
287 >     *
288 >     * <p>This implementation returns a set that subclasses {@link AbstractSet}.
289 >     * The subclass's iterator method returns a "wrapper object" over this
290 >     * map's <tt>entrySet()</tt> iterator.  The <tt>size</tt> method
291 >     * delegates to this map's <tt>size</tt> method and the
292 >     * <tt>contains</tt> method delegates to this map's
293 >     * <tt>containsKey</tt> method.
294       *
295 <     * The Set is created the first time this method is called,
295 >     * <p>The set is created the first time this method is called,
296       * and returned in response to all subsequent calls.  No synchronization
297       * is performed, so there is a slight chance that multiple calls to this
298 <     * method will not all return the same Set.
368 <     *
369 <     * @return a Set view of the keys contained in this map.
298 >     * method will not all return the same set.
299       */
300      public Set<K> keySet() {
301          if (keySet == null) {
# Line 402 | Line 331 | public abstract class AbstractMap<K,V> i
331      }
332  
333      /**
334 <     * Returns a collection view of the values contained in this map.  The
335 <     * collection is backed by the map, so changes to the map are reflected in
336 <     * the collection, and vice-versa.  (If the map is modified while an
337 <     * iteration over the collection is in progress, the results of the
338 <     * iteration are undefined.)  The collection supports element removal,
339 <     * which removes the corresponding entry from the map, via the
340 <     * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
341 <     * <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>
334 >     * {@inheritDoc}
335 >     *
336 >     * <p>This implementation returns a collection that subclasses {@link
337 >     * AbstractCollection}.  The subclass's iterator method returns a
338 >     * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
339 >     * The <tt>size</tt> method delegates to this map's <tt>size</tt>
340 >     * method and the <tt>contains</tt> method delegates to this map's
341 >     * <tt>containsValue</tt> method.
342       *
343 <     * The collection is created the first time this method is called, and
343 >     * <p>The collection is created the first time this method is called, and
344       * returned in response to all subsequent calls.  No synchronization is
345       * performed, so there is a slight chance that multiple calls to this
346 <     * method will not all return the same Collection.
425 <     *
426 <     * @return a collection view of the values contained in this map.
346 >     * method will not all return the same collection.
347       */
348      public Collection<V> values() {
349          if (values == null) {
# Line 458 | Line 378 | public abstract class AbstractMap<K,V> i
378          return values;
379      }
380  
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     */
381      public abstract Set<Entry<K,V>> entrySet();
382  
383  
# Line 479 | Line 386 | public abstract class AbstractMap<K,V> i
386      /**
387       * Compares the specified object with this map for equality.  Returns
388       * <tt>true</tt> if the given object is also a map and the two maps
389 <     * represent the same mappings.  More formally, two maps <tt>t1</tt> and
390 <     * <tt>t2</tt> represent the same mappings if
391 <     * <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
389 >     * represent the same mappings.  More formally, two maps <tt>m1</tt> and
390 >     * <tt>m2</tt> represent the same mappings if
391 >     * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
392       * <tt>equals</tt> method works properly across different implementations
393 <     * of the map interface.<p>
393 >     * of the <tt>Map</tt> interface.
394       *
395 <     * This implementation first checks if the specified object is this map;
395 >     * <p>This implementation first checks if the specified object is this map;
396       * if so it returns <tt>true</tt>.  Then, it checks if the specified
397 <     * object is a map whose size is identical to the size of this set; if
397 >     * object is a map whose size is identical to the size of this map; if
398       * not, it returns <tt>false</tt>.  If so, it iterates over this map's
399       * <tt>entrySet</tt> collection, and checks that the specified map
400       * contains each mapping that this map contains.  If the specified map
401       * fails to contain such a mapping, <tt>false</tt> is returned.  If the
402       * iteration completes, <tt>true</tt> is returned.
403       *
404 <     * @param o object to be compared for equality with this map.
405 <     * @return <tt>true</tt> if the specified object is equal to this map.
404 >     * @param o object to be compared for equality with this map
405 >     * @return <tt>true</tt> if the specified object is equal to this map
406       */
407      public boolean equals(Object o) {
408          if (o == this)
# Line 505 | Line 410 | public abstract class AbstractMap<K,V> i
410  
411          if (!(o instanceof Map))
412              return false;
413 <        Map<K,V> t = (Map<K,V>) o;
414 <        if (t.size() != size())
413 >        Map<K,V> m = (Map<K,V>) o;
414 >        if (m.size() != size())
415              return false;
416  
417          try {
# Line 516 | Line 421 | public abstract class AbstractMap<K,V> i
421                  K key = e.getKey();
422                  V value = e.getValue();
423                  if (value == null) {
424 <                    if (!(t.get(key)==null && t.containsKey(key)))
424 >                    if (!(m.get(key)==null && m.containsKey(key)))
425                          return false;
426                  } else {
427 <                    if (!value.equals(t.get(key)))
427 >                    if (!value.equals(m.get(key)))
428                          return false;
429                  }
430              }
431 <        } catch(ClassCastException unused) {
431 >        } catch (ClassCastException unused) {
432              return false;
433 <        } catch(NullPointerException unused) {
433 >        } catch (NullPointerException unused) {
434              return false;
435          }
436  
# Line 535 | Line 440 | public abstract class AbstractMap<K,V> i
440      /**
441       * Returns the hash code value for this map.  The hash code of a map is
442       * defined to be the sum of the hash codes of each entry in the map's
443 <     * <tt>entrySet()</tt> view.  This ensures that <tt>t1.equals(t2)</tt>
444 <     * implies that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
445 <     * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of
446 <     * Object.hashCode.<p>
447 <     *
448 <     * This implementation iterates over <tt>entrySet()</tt>, calling
449 <     * <tt>hashCode</tt> on each element (entry) in the Collection, and adding
450 <     * up the results.
443 >     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
444 >     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
445 >     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
446 >     * {@link Object#hashCode}.
447 >     *
448 >     * <p>This implementation iterates over <tt>entrySet()</tt>, calling
449 >     * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
450 >     * set, and adding up the results.
451       *
452 <     * @return the hash code value for this map.
452 >     * @return the hash code value for this map
453       * @see Map.Entry#hashCode()
549     * @see Object#hashCode()
454       * @see Object#equals(Object)
455       * @see Set#equals(Object)
456       */
# Line 566 | 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>
473 >     * {@link String#valueOf(Object)}.
474       *
475 <     * 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.
577 <     *
578 <     * @return a String representation of this map.
475 >     * @return a string representation of this map
476       */
477      public String toString() {
581        StringBuffer buf = new StringBuffer();
582        buf.append("{");
583
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 <                buf.append("(this Map)");
490 <            else
491 <                buf.append(key);
492 <            buf.append("=");
493 <            if (value == this)
494 <                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();
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 <    
496 >
497      /**
498       * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
499       * and values themselves are not cloned.
500       *
501 <     * @return a shallow copy of this map.
501 >     * @return a shallow copy of this map
502       */
503      protected Object clone() throws CloneNotSupportedException {
504          AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
# Line 623 | 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
519      // are distinct unrelated classes, even though they share
520      // some code. Since you can't add or subtract final-ness
521 <    // of a field in a subclass, they can't share represenations,
521 >    // of a field in a subclass, they can't share representations,
522      // and the amount of duplicated code is too small to warrant
523      // exposing a common abstract class.
524  
# Line 640 | 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 662 | Line 557 | public abstract class AbstractMap<K,V> i
557           * Creates an entry representing the same mapping as the
558           * specified entry.
559           *
560 <         * @param entry the entry to copy.
560 >         * @param entry the entry to copy
561           */
562 <        public SimpleEntry(Entry<K,V> entry) {
562 >        public SimpleEntry(Entry<? extends K, ? extends V> entry) {
563              this.key   = entry.getKey();
564              this.value = entry.getValue();
565          }
# Line 672 | Line 567 | public abstract class AbstractMap<K,V> i
567          /**
568           * Returns the key corresponding to this entry.
569           *
570 <         * @return the key corresponding to this entry.
570 >         * @return the key corresponding to this entry
571           */
572          public K getKey() {
573              return key;
# Line 681 | Line 576 | public abstract class AbstractMap<K,V> i
576          /**
577           * Returns the value corresponding to this entry.
578           *
579 <         * @return the value corresponding to this entry.
579 >         * @return the value corresponding to this entry
580           */
581          public V getValue() {
582              return value;
# Line 691 | Line 586 | public abstract class AbstractMap<K,V> i
586           * Replaces the value corresponding to this entry with the specified
587           * value.
588           *
589 <         * @param value new value to be stored in this entry.
590 <         * @return old value corresponding to the entry.
589 >         * @param value new value to be stored in this entry
590 >         * @return the old value corresponding to the entry
591           */
592          public V setValue(V value) {
593              V oldValue = this.value;
# Line 700 | 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 707 | 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 718 | Line 647 | public abstract class AbstractMap<K,V> i
647           * entry's key followed by the equals character ("<tt>=</tt>")
648           * followed by the string representation of this entry's value.
649           *
650 <         * @return a String representation of this map entry.
650 >         * @return a String representation of this map entry
651           */
652          public String toString() {
653              return key + "=" + value;
# Line 727 | 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 752 | Line 687 | public abstract class AbstractMap<K,V> i
687           * Creates an entry representing the same mapping as the
688           * specified entry.
689           *
690 <         * @param entry the entry to copy.
690 >         * @param entry the entry to copy
691           */
692 <        public SimpleImmutableEntry(Entry<K,V> entry) {
692 >        public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
693              this.key   = entry.getKey();
694              this.value = entry.getValue();
695          }
# Line 762 | Line 697 | public abstract class AbstractMap<K,V> i
697          /**
698           * Returns the key corresponding to this entry.
699           *
700 <         * @return the key corresponding to this entry.
700 >         * @return the key corresponding to this entry
701           */
702          public K getKey() {
703              return key;
# Line 771 | Line 706 | public abstract class AbstractMap<K,V> i
706          /**
707           * Returns the value corresponding to this entry.
708           *
709 <         * @return the value corresponding to this entry.
709 >         * @return the value corresponding to this entry
710           */
711          public V getValue() {
712              return value;
# Line 783 | Line 718 | public abstract class AbstractMap<K,V> i
718           * <tt>UnsupportedOperationException</tt>, as this class implements
719           * an <i>immutable</i> map entry.
720           *
721 <         * @param value new value to be stored in this entry.
721 >         * @param value new value to be stored in this entry
722           * @return (Does not return)
723           * @throws UnsupportedOperationException always
724           */
# Line 791 | 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 798 | 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          /**
# Line 809 | Line 778 | public abstract class AbstractMap<K,V> i
778           * entry's key followed by the equals character ("<tt>=</tt>")
779           * followed by the string representation of this entry's value.
780           *
781 <         * @return a String representation of this map entry.
781 >         * @return a String representation of this map entry
782           */
783          public String toString() {
784              return key + "=" + value;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines