ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractMap.java
Revision: 1.19
Committed: Mon Dec 5 02:56:59 2005 UTC (18 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +1 -1 lines
Log Message:
copyright update for 2006

File Contents

# User Rev Content
1 dl 1.1 /*
2     * %W% %E%
3     *
4 jsr166 1.19 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5 dl 1.1 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6     */
7    
8     package java.util;
9 jsr166 1.13 import java.util.*; // for javadoc (till 6280605 is fixed)
10 dl 1.1 import java.util.Map.Entry;
11    
12     /**
13     * This class provides a skeletal implementation of the <tt>Map</tt>
14 jsr166 1.9 * interface, to minimize the effort required to implement this interface.
15 dl 1.1 *
16 jsr166 1.9 * <p>To implement an unmodifiable map, the programmer needs only to extend this
17 dl 1.1 * class and provide an implementation for the <tt>entrySet</tt> method, which
18     * returns a set-view of the map's mappings. Typically, the returned set
19     * will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should
20     * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
21 jsr166 1.9 * should not support the <tt>remove</tt> method.
22 dl 1.1 *
23 jsr166 1.9 * <p>To implement a modifiable map, the programmer must additionally override
24 dl 1.1 * this class's <tt>put</tt> method (which otherwise throws an
25     * <tt>UnsupportedOperationException</tt>), and the iterator returned by
26     * <tt>entrySet().iterator()</tt> must additionally implement its
27 jsr166 1.9 * <tt>remove</tt> method.
28 dl 1.1 *
29 jsr166 1.9 * <p>The programmer should generally provide a void (no argument) and map
30 dl 1.1 * constructor, as per the recommendation in the <tt>Map</tt> interface
31 jsr166 1.9 * specification.
32 dl 1.1 *
33 jsr166 1.9 * <p>The documentation for each non-abstract methods in this class describes its
34 dl 1.1 * implementation in detail. Each of these methods may be overridden if the
35 jsr166 1.9 * map being implemented admits a more efficient implementation.
36 dl 1.1 *
37 jsr166 1.9 * <p>This class is a member of the
38 dl 1.1 * <a href="{@docRoot}/../guide/collections/index.html">
39     * Java Collections Framework</a>.
40     *
41 jsr166 1.7 * @param <K> the type of keys maintained by this map
42     * @param <V> the type of mapped values
43     *
44 dl 1.1 * @author Josh Bloch
45     * @author Neal Gafter
46     * @version %I%, %G%
47     * @see Map
48     * @see Collection
49     * @since 1.2
50     */
51    
52     public abstract class AbstractMap<K,V> implements Map<K,V> {
53     /**
54     * Sole constructor. (For invocation by subclass constructors, typically
55     * implicit.)
56     */
57     protected AbstractMap() {
58     }
59    
60     // Query Operations
61    
62     /**
63 jsr166 1.9 * {@inheritDoc}
64 dl 1.1 *
65 jsr166 1.9 * <p>This implementation returns <tt>entrySet().size()</tt>.
66 dl 1.1 */
67     public int size() {
68     return entrySet().size();
69     }
70    
71     /**
72 jsr166 1.9 * {@inheritDoc}
73 dl 1.1 *
74 jsr166 1.9 * <p>This implementation returns <tt>size() == 0</tt>.
75 dl 1.1 */
76     public boolean isEmpty() {
77     return size() == 0;
78     }
79    
80     /**
81 jsr166 1.9 * {@inheritDoc}
82 dl 1.1 *
83 jsr166 1.9 * <p>This implementation iterates over <tt>entrySet()</tt> searching
84     * for an entry with the specified value. If such an entry is found,
85     * <tt>true</tt> is returned. If the iteration terminates without
86     * finding such an entry, <tt>false</tt> is returned. Note that this
87     * implementation requires linear time in the size of the map.
88 jsr166 1.7 *
89 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
90 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
91 dl 1.1 */
92     public boolean containsValue(Object value) {
93     Iterator<Entry<K,V>> i = entrySet().iterator();
94     if (value==null) {
95     while (i.hasNext()) {
96     Entry<K,V> e = i.next();
97     if (e.getValue()==null)
98     return true;
99     }
100     } else {
101     while (i.hasNext()) {
102     Entry<K,V> e = i.next();
103     if (value.equals(e.getValue()))
104     return true;
105     }
106     }
107     return false;
108     }
109    
110     /**
111 jsr166 1.9 * {@inheritDoc}
112     *
113     * <p>This implementation iterates over <tt>entrySet()</tt> searching
114     * for an entry with the specified key. If such an entry is found,
115     * <tt>true</tt> is returned. If the iteration terminates without
116     * finding such an entry, <tt>false</tt> is returned. Note that this
117     * implementation requires linear time in the size of the map; many
118     * implementations will override this method.
119 dl 1.1 *
120 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
121 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
122 dl 1.1 */
123     public boolean containsKey(Object key) {
124     Iterator<Map.Entry<K,V>> i = entrySet().iterator();
125     if (key==null) {
126     while (i.hasNext()) {
127     Entry<K,V> e = i.next();
128     if (e.getKey()==null)
129     return true;
130     }
131     } else {
132     while (i.hasNext()) {
133     Entry<K,V> e = i.next();
134     if (key.equals(e.getKey()))
135     return true;
136     }
137     }
138     return false;
139     }
140    
141     /**
142 jsr166 1.9 * {@inheritDoc}
143 dl 1.1 *
144 jsr166 1.9 * <p>This implementation iterates over <tt>entrySet()</tt> searching
145     * for an entry with the specified key. If such an entry is found,
146     * the entry's value is returned. If the iteration terminates without
147     * finding such an entry, <tt>null</tt> is returned. Note that this
148     * implementation requires linear time in the size of the map; many
149     * implementations will override this method.
150 jsr166 1.7 *
151 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
152 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
153 dl 1.1 */
154     public V get(Object key) {
155     Iterator<Entry<K,V>> i = entrySet().iterator();
156     if (key==null) {
157     while (i.hasNext()) {
158     Entry<K,V> e = i.next();
159     if (e.getKey()==null)
160     return e.getValue();
161     }
162     } else {
163     while (i.hasNext()) {
164     Entry<K,V> e = i.next();
165     if (key.equals(e.getKey()))
166     return e.getValue();
167     }
168     }
169     return null;
170     }
171    
172    
173     // Modification Operations
174    
175     /**
176 jsr166 1.9 * {@inheritDoc}
177 dl 1.1 *
178 jsr166 1.9 * <p>This implementation always throws an
179 dl 1.1 * <tt>UnsupportedOperationException</tt>.
180     *
181 jsr166 1.9 * @throws UnsupportedOperationException {@inheritDoc}
182 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
183 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
184     * @throws IllegalArgumentException {@inheritDoc}
185 dl 1.1 */
186     public V put(K key, V value) {
187     throw new UnsupportedOperationException();
188     }
189    
190     /**
191 jsr166 1.9 * {@inheritDoc}
192 dl 1.1 *
193 jsr166 1.9 * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
194 dl 1.1 * entry with the specified key. If such an entry is found, its value is
195     * obtained with its <tt>getValue</tt> operation, the entry is removed
196 jsr166 1.7 * from the collection (and the backing map) with the iterator's
197 dl 1.1 * <tt>remove</tt> operation, and the saved value is returned. If the
198     * iteration terminates without finding such an entry, <tt>null</tt> is
199     * returned. Note that this implementation requires linear time in the
200 jsr166 1.9 * size of the map; many implementations will override this method.
201     *
202     * <p>Note that this implementation throws an
203     * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
204     * iterator does not support the <tt>remove</tt> method and this map
205     * contains a mapping for the specified key.
206 dl 1.1 *
207 jsr166 1.9 * @throws UnsupportedOperationException {@inheritDoc}
208 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
209 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
210 dl 1.1 */
211     public V remove(Object key) {
212     Iterator<Entry<K,V>> i = entrySet().iterator();
213     Entry<K,V> correctEntry = null;
214     if (key==null) {
215     while (correctEntry==null && i.hasNext()) {
216     Entry<K,V> e = i.next();
217     if (e.getKey()==null)
218     correctEntry = e;
219     }
220     } else {
221     while (correctEntry==null && i.hasNext()) {
222     Entry<K,V> e = i.next();
223     if (key.equals(e.getKey()))
224     correctEntry = e;
225     }
226     }
227    
228     V oldValue = null;
229     if (correctEntry !=null) {
230     oldValue = correctEntry.getValue();
231     i.remove();
232     }
233     return oldValue;
234     }
235    
236    
237     // Bulk Operations
238    
239     /**
240 jsr166 1.9 * {@inheritDoc}
241 dl 1.1 *
242 jsr166 1.9 * <p>This implementation iterates over the specified map's
243 dl 1.1 * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
244 jsr166 1.9 * operation once for each entry returned by the iteration.
245 dl 1.1 *
246 jsr166 1.9 * <p>Note that this implementation throws an
247 dl 1.1 * <tt>UnsupportedOperationException</tt> if this map does not support
248     * the <tt>put</tt> operation and the specified map is nonempty.
249     *
250 jsr166 1.9 * @throws UnsupportedOperationException {@inheritDoc}
251 jsr166 1.10 * @throws ClassCastException {@inheritDoc}
252 jsr166 1.9 * @throws NullPointerException {@inheritDoc}
253     * @throws IllegalArgumentException {@inheritDoc}
254 dl 1.1 */
255 jsr166 1.9 public void putAll(Map<? extends K, ? extends V> m) {
256 jsr166 1.17 for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
257     put(e.getKey(), e.getValue());
258 dl 1.1 }
259    
260     /**
261 jsr166 1.9 * {@inheritDoc}
262 dl 1.1 *
263 jsr166 1.9 * <p>This implementation calls <tt>entrySet().clear()</tt>.
264 dl 1.1 *
265 jsr166 1.9 * <p>Note that this implementation throws an
266 dl 1.1 * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
267     * does not support the <tt>clear</tt> operation.
268     *
269 jsr166 1.9 * @throws UnsupportedOperationException {@inheritDoc}
270 dl 1.1 */
271     public void clear() {
272     entrySet().clear();
273     }
274    
275    
276     // Views
277    
278     /**
279     * Each of these fields are initialized to contain an instance of the
280     * appropriate view the first time this view is requested. The views are
281     * stateless, so there's no reason to create more than one of each.
282     */
283     transient volatile Set<K> keySet = null;
284     transient volatile Collection<V> values = null;
285    
286     /**
287 jsr166 1.9 * {@inheritDoc}
288 jsr166 1.7 *
289     * <p>This implementation returns a set that subclasses {@link AbstractSet}.
290     * The subclass's iterator method returns a "wrapper object" over this
291     * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method
292     * delegates to this map's <tt>size</tt> method and the
293     * <tt>contains</tt> method delegates to this map's
294     * <tt>containsKey</tt> method.
295 dl 1.1 *
296 jsr166 1.7 * <p>The set is created the first time this method is called,
297 dl 1.1 * and returned in response to all subsequent calls. No synchronization
298     * is performed, so there is a slight chance that multiple calls to this
299 jsr166 1.7 * method will not all return the same set.
300 dl 1.1 */
301     public Set<K> keySet() {
302     if (keySet == null) {
303     keySet = new AbstractSet<K>() {
304     public Iterator<K> iterator() {
305     return new Iterator<K>() {
306     private Iterator<Entry<K,V>> i = entrySet().iterator();
307    
308     public boolean hasNext() {
309     return i.hasNext();
310     }
311    
312     public K next() {
313     return i.next().getKey();
314     }
315    
316     public void remove() {
317     i.remove();
318     }
319     };
320     }
321    
322     public int size() {
323     return AbstractMap.this.size();
324     }
325    
326     public boolean contains(Object k) {
327     return AbstractMap.this.containsKey(k);
328     }
329     };
330     }
331     return keySet;
332     }
333    
334     /**
335 jsr166 1.9 * {@inheritDoc}
336 jsr166 1.7 *
337     * <p>This implementation returns a collection that subclasses {@link
338     * AbstractCollection}. The subclass's iterator method returns a
339     * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
340     * The <tt>size</tt> method delegates to this map's <tt>size</tt>
341     * method and the <tt>contains</tt> method delegates to this map's
342     * <tt>containsValue</tt> method.
343 dl 1.1 *
344 jsr166 1.7 * <p>The collection is created the first time this method is called, and
345 dl 1.1 * returned in response to all subsequent calls. No synchronization is
346     * performed, so there is a slight chance that multiple calls to this
347 jsr166 1.7 * method will not all return the same collection.
348 dl 1.1 */
349     public Collection<V> values() {
350     if (values == null) {
351     values = new AbstractCollection<V>() {
352     public Iterator<V> iterator() {
353     return new Iterator<V>() {
354     private Iterator<Entry<K,V>> i = entrySet().iterator();
355    
356     public boolean hasNext() {
357     return i.hasNext();
358     }
359    
360     public V next() {
361     return i.next().getValue();
362     }
363    
364     public void remove() {
365     i.remove();
366     }
367     };
368     }
369    
370     public int size() {
371     return AbstractMap.this.size();
372     }
373    
374     public boolean contains(Object v) {
375     return AbstractMap.this.containsValue(v);
376     }
377     };
378     }
379     return values;
380     }
381    
382     public abstract Set<Entry<K,V>> entrySet();
383    
384    
385     // Comparison and hashing
386    
387     /**
388 jsr166 1.14 * Compares the specified object with this map for equality. Returns
389     * <tt>true</tt> if the given object is also a map and the two maps
390     * represent the same mappings. More formally, two maps <tt>m1</tt> and
391     * <tt>m2</tt> represent the same mappings if
392     * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
393     * <tt>equals</tt> method works properly across different implementations
394     * of the <tt>Map</tt> interface.
395 dl 1.1 *
396 jsr166 1.9 * <p>This implementation first checks if the specified object is this map;
397 dl 1.1 * if so it returns <tt>true</tt>. Then, it checks if the specified
398 jsr166 1.8 * object is a map whose size is identical to the size of this map; if
399 dl 1.1 * not, it returns <tt>false</tt>. If so, it iterates over this map's
400     * <tt>entrySet</tt> collection, and checks that the specified map
401     * contains each mapping that this map contains. If the specified map
402     * fails to contain such a mapping, <tt>false</tt> is returned. If the
403     * iteration completes, <tt>true</tt> is returned.
404 jsr166 1.14 *
405     * @param o object to be compared for equality with this map
406     * @return <tt>true</tt> if the specified object is equal to this map
407 dl 1.1 */
408     public boolean equals(Object o) {
409     if (o == this)
410     return true;
411    
412     if (!(o instanceof Map))
413     return false;
414 jsr166 1.13 Map<K,V> m = (Map<K,V>) o;
415     if (m.size() != size())
416 dl 1.1 return false;
417    
418     try {
419     Iterator<Entry<K,V>> i = entrySet().iterator();
420     while (i.hasNext()) {
421     Entry<K,V> e = i.next();
422     K key = e.getKey();
423     V value = e.getValue();
424     if (value == null) {
425 jsr166 1.13 if (!(m.get(key)==null && m.containsKey(key)))
426 dl 1.1 return false;
427     } else {
428 jsr166 1.13 if (!value.equals(m.get(key)))
429 dl 1.1 return false;
430     }
431     }
432 jsr166 1.7 } catch (ClassCastException unused) {
433 dl 1.1 return false;
434 jsr166 1.7 } catch (NullPointerException unused) {
435 dl 1.1 return false;
436     }
437    
438     return true;
439     }
440    
441     /**
442 jsr166 1.14 * Returns the hash code value for this map. The hash code of a map is
443     * defined to be the sum of the hash codes of each entry in the map's
444     * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
445     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
446     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
447     * {@link Object#hashCode}.
448 jsr166 1.9 *
449     * <p>This implementation iterates over <tt>entrySet()</tt>, calling
450 jsr166 1.14 * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
451     * set, and adding up the results.
452 dl 1.1 *
453 jsr166 1.14 * @return the hash code value for this map
454 dl 1.1 * @see Map.Entry#hashCode()
455     * @see Object#equals(Object)
456     * @see Set#equals(Object)
457     */
458     public int hashCode() {
459     int h = 0;
460     Iterator<Entry<K,V>> i = entrySet().iterator();
461     while (i.hasNext())
462     h += i.next().hashCode();
463     return h;
464     }
465    
466     /**
467     * Returns a string representation of this map. The string representation
468     * consists of a list of key-value mappings in the order returned by the
469     * map's <tt>entrySet</tt> view's iterator, enclosed in braces
470     * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
471     * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
472     * the key followed by an equals sign (<tt>"="</tt>) followed by the
473     * associated value. Keys and values are converted to strings as by
474 jsr166 1.17 * {@link String#valueOf(Object)}.
475 dl 1.1 *
476 jsr166 1.17 * @return a string representation of this map
477 dl 1.1 */
478     public String toString() {
479 jsr166 1.17 Iterator<Entry<K,V>> i = entrySet().iterator();
480     if (! i.hasNext())
481     return "{}";
482    
483 jsr166 1.12 StringBuilder sb = new StringBuilder();
484 jsr166 1.17 sb.append('{');
485     for (;;) {
486 dl 1.1 Entry<K,V> e = i.next();
487     K key = e.getKey();
488 jsr166 1.17 V value = e.getValue();
489     sb.append(key == this ? "(this Map)" : key);
490     sb.append('=');
491     sb.append(value == this ? "(this Map)" : value);
492     if (! i.hasNext())
493     return sb.append('}').toString();
494     sb.append(", ");
495     }
496 dl 1.1 }
497 jsr166 1.7
498 dl 1.1 /**
499     * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
500     * and values themselves are not cloned.
501     *
502 jsr166 1.9 * @return a shallow copy of this map
503 dl 1.1 */
504     protected Object clone() throws CloneNotSupportedException {
505     AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
506     result.keySet = null;
507     result.values = null;
508     return result;
509     }
510    
511     /**
512     * Utility method for SimpleEntry and SimpleImmutableEntry.
513     * Test for equality, checking for nulls.
514     */
515     private static boolean eq(Object o1, Object o2) {
516 jsr166 1.16 return o1 == null ? o2 == null : o1.equals(o2);
517 dl 1.1 }
518    
519     // Implementation Note: SimpleEntry and SimpleImmutableEntry
520     // are distinct unrelated classes, even though they share
521     // some code. Since you can't add or subtract final-ness
522 dl 1.3 // of a field in a subclass, they can't share representations,
523 dl 1.1 // and the amount of duplicated code is too small to warrant
524     // exposing a common abstract class.
525    
526    
527     /**
528     * An Entry maintaining a key and a value. The value may be
529     * changed using the <tt>setValue</tt> method. This class
530     * facilitates the process of building custom map
531     * implementations. For example, it may be convenient to return
532     * arrays of <tt>SimpleEntry</tt> instances in method
533 jsr166 1.16 * <tt>Map.entrySet().toArray</tt>.
534 jsr166 1.15 *
535     * @since 1.6
536 dl 1.1 */
537 jsr166 1.16 public static class SimpleEntry<K,V>
538     implements Entry<K,V>, java.io.Serializable
539     {
540     private static final long serialVersionUID = -8499721149061103585L;
541    
542 dl 1.2 private final K key;
543 dl 1.1 private V value;
544    
545     /**
546     * Creates an entry representing a mapping from the specified
547     * key to the specified value.
548     *
549     * @param key the key represented by this entry
550     * @param value the value represented by this entry
551     */
552     public SimpleEntry(K key, V value) {
553     this.key = key;
554     this.value = value;
555     }
556    
557     /**
558     * Creates an entry representing the same mapping as the
559     * specified entry.
560     *
561 jsr166 1.11 * @param entry the entry to copy
562 dl 1.1 */
563 dl 1.4 public SimpleEntry(Entry<? extends K, ? extends V> entry) {
564 dl 1.1 this.key = entry.getKey();
565     this.value = entry.getValue();
566     }
567    
568     /**
569     * Returns the key corresponding to this entry.
570     *
571 jsr166 1.9 * @return the key corresponding to this entry
572 dl 1.1 */
573     public K getKey() {
574     return key;
575     }
576    
577     /**
578     * Returns the value corresponding to this entry.
579     *
580 jsr166 1.9 * @return the value corresponding to this entry
581 dl 1.1 */
582     public V getValue() {
583     return value;
584     }
585    
586     /**
587     * Replaces the value corresponding to this entry with the specified
588     * value.
589     *
590 jsr166 1.9 * @param value new value to be stored in this entry
591     * @return the old value corresponding to the entry
592 dl 1.1 */
593     public V setValue(V value) {
594     V oldValue = this.value;
595     this.value = value;
596     return oldValue;
597     }
598    
599 jsr166 1.18 /**
600     * Compares the specified object with this entry for equality.
601     * Returns {@code true} if the given object is also a map entry and
602     * the two entries represent the same mapping. More formally, two
603     * entries {@code e1} and {@code e2} represent the same mapping
604     * if<pre>
605     * (e1.getKey()==null ?
606     * e2.getKey()==null :
607     * e1.getKey().equals(e2.getKey()))
608     * &amp;&amp;
609     * (e1.getValue()==null ?
610     * e2.getValue()==null :
611     * e1.getValue().equals(e2.getValue()))</pre>
612     * This ensures that the {@code equals} method works properly across
613     * different implementations of the {@code Map.Entry} interface.
614     *
615     * @param o object to be compared for equality with this map entry
616     * @return {@code true} if the specified object is equal to this map
617     * entry
618     * @see #hashCode
619     */
620 dl 1.1 public boolean equals(Object o) {
621     if (!(o instanceof Map.Entry))
622     return false;
623     Map.Entry e = (Map.Entry)o;
624     return eq(key, e.getKey()) && eq(value, e.getValue());
625     }
626    
627 jsr166 1.18 /**
628     * Returns the hash code value for this map entry. The hash code
629     * of a map entry {@code e} is defined to be: <pre>
630     * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
631     * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
632     * This ensures that {@code e1.equals(e2)} implies that
633     * {@code e1.hashCode()==e2.hashCode()} for any two Entries
634     * {@code e1} and {@code e2}, as required by the general
635     * contract of {@link Object#hashCode}.
636     *
637     * @return the hash code value for this map entry
638     * @see #equals
639     */
640 dl 1.1 public int hashCode() {
641 jsr166 1.16 return (key == null ? 0 : key.hashCode()) ^
642     (value == null ? 0 : value.hashCode());
643 dl 1.1 }
644    
645     /**
646     * Returns a String representation of this map entry. This
647     * implementation returns the string representation of this
648     * entry's key followed by the equals character ("<tt>=</tt>")
649     * followed by the string representation of this entry's value.
650     *
651 jsr166 1.9 * @return a String representation of this map entry
652 dl 1.1 */
653     public String toString() {
654     return key + "=" + value;
655     }
656    
657     }
658    
659     /**
660 jsr166 1.16 * An Entry maintaining an immutable key and value. This class
661 dl 1.1 * does not support method <tt>setValue</tt>. This class may be
662     * convenient in methods that return thread-safe snapshots of
663     * key-value mappings.
664 jsr166 1.15 *
665     * @since 1.6
666 dl 1.1 */
667 jsr166 1.16 public static class SimpleImmutableEntry<K,V>
668     implements Entry<K,V>, java.io.Serializable
669     {
670     private static final long serialVersionUID = 7138329143949025153L;
671    
672 dl 1.1 private final K key;
673     private final V value;
674    
675     /**
676     * Creates an entry representing a mapping from the specified
677     * key to the specified value.
678     *
679     * @param key the key represented by this entry
680     * @param value the value represented by this entry
681     */
682     public SimpleImmutableEntry(K key, V value) {
683     this.key = key;
684     this.value = value;
685     }
686    
687     /**
688     * Creates an entry representing the same mapping as the
689     * specified entry.
690     *
691 jsr166 1.9 * @param entry the entry to copy
692 dl 1.1 */
693 dl 1.4 public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
694 dl 1.1 this.key = entry.getKey();
695     this.value = entry.getValue();
696     }
697    
698     /**
699     * Returns the key corresponding to this entry.
700     *
701 jsr166 1.9 * @return the key corresponding to this entry
702 dl 1.1 */
703     public K getKey() {
704     return key;
705     }
706    
707     /**
708     * Returns the value corresponding to this entry.
709     *
710 jsr166 1.9 * @return the value corresponding to this entry
711 dl 1.1 */
712     public V getValue() {
713     return value;
714     }
715    
716     /**
717     * Replaces the value corresponding to this entry with the specified
718     * value (optional operation). This implementation simply throws
719     * <tt>UnsupportedOperationException</tt>, as this class implements
720     * an <i>immutable</i> map entry.
721     *
722 jsr166 1.9 * @param value new value to be stored in this entry
723 dl 1.1 * @return (Does not return)
724     * @throws UnsupportedOperationException always
725     */
726     public V setValue(V value) {
727     throw new UnsupportedOperationException();
728     }
729    
730 jsr166 1.18 /**
731     * Compares the specified object with this entry for equality.
732     * Returns {@code true} if the given object is also a map entry and
733     * the two entries represent the same mapping. More formally, two
734     * entries {@code e1} and {@code e2} represent the same mapping
735     * if<pre>
736     * (e1.getKey()==null ?
737     * e2.getKey()==null :
738     * e1.getKey().equals(e2.getKey()))
739     * &amp;&amp;
740     * (e1.getValue()==null ?
741     * e2.getValue()==null :
742     * e1.getValue().equals(e2.getValue()))</pre>
743     * This ensures that the {@code equals} method works properly across
744     * different implementations of the {@code Map.Entry} interface.
745     *
746     * @param o object to be compared for equality with this map entry
747     * @return {@code true} if the specified object is equal to this map
748     * entry
749     * @see #hashCode
750     */
751 dl 1.1 public boolean equals(Object o) {
752     if (!(o instanceof Map.Entry))
753     return false;
754     Map.Entry e = (Map.Entry)o;
755     return eq(key, e.getKey()) && eq(value, e.getValue());
756     }
757    
758 jsr166 1.18 /**
759     * Returns the hash code value for this map entry. The hash code
760     * of a map entry {@code e} is defined to be: <pre>
761     * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
762     * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
763     * This ensures that {@code e1.equals(e2)} implies that
764     * {@code e1.hashCode()==e2.hashCode()} for any two Entries
765     * {@code e1} and {@code e2}, as required by the general
766     * contract of {@link Object#hashCode}.
767     *
768     * @return the hash code value for this map entry
769     * @see #equals
770     */
771 dl 1.1 public int hashCode() {
772 jsr166 1.16 return (key == null ? 0 : key.hashCode()) ^
773     (value == null ? 0 : value.hashCode());
774 dl 1.1 }
775    
776     /**
777     * Returns a String representation of this map entry. This
778     * implementation returns the string representation of this
779     * entry's key followed by the equals character ("<tt>=</tt>")
780     * followed by the string representation of this entry's value.
781     *
782 jsr166 1.9 * @return a String representation of this map entry
783 dl 1.1 */
784     public String toString() {
785     return key + "=" + value;
786     }
787    
788     }
789    
790     }