ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractMap.java
Revision: 1.14
Committed: Tue Jun 21 07:43:09 2005 UTC (18 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +19 -5 lines
Log Message:
@inheritDoc multiple inheritance bug

File Contents

# User Rev Content
1 dl 1.1 /*
2     * %W% %E%
3     *
4 jsr166 1.6 * Copyright 2005 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     Iterator<? extends Entry<? extends K, ? extends V>> i = m.entrySet().iterator();
257 dl 1.1 while (i.hasNext()) {
258     Entry<? extends K, ? extends V> e = i.next();
259     put(e.getKey(), e.getValue());
260     }
261     }
262    
263     /**
264 jsr166 1.9 * {@inheritDoc}
265 dl 1.1 *
266 jsr166 1.9 * <p>This implementation calls <tt>entrySet().clear()</tt>.
267 dl 1.1 *
268 jsr166 1.9 * <p>Note that this implementation throws an
269 dl 1.1 * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
270     * does not support the <tt>clear</tt> operation.
271     *
272 jsr166 1.9 * @throws UnsupportedOperationException {@inheritDoc}
273 dl 1.1 */
274     public void clear() {
275     entrySet().clear();
276     }
277    
278    
279     // Views
280    
281     /**
282     * Each of these fields are initialized to contain an instance of the
283     * appropriate view the first time this view is requested. The views are
284     * stateless, so there's no reason to create more than one of each.
285     */
286     transient volatile Set<K> keySet = null;
287     transient volatile Collection<V> values = null;
288    
289     /**
290 jsr166 1.9 * {@inheritDoc}
291 jsr166 1.7 *
292     * <p>This implementation returns a set that subclasses {@link AbstractSet}.
293     * The subclass's iterator method returns a "wrapper object" over this
294     * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method
295     * delegates to this map's <tt>size</tt> method and the
296     * <tt>contains</tt> method delegates to this map's
297     * <tt>containsKey</tt> method.
298 dl 1.1 *
299 jsr166 1.7 * <p>The set is created the first time this method is called,
300 dl 1.1 * and returned in response to all subsequent calls. No synchronization
301     * is performed, so there is a slight chance that multiple calls to this
302 jsr166 1.7 * method will not all return the same set.
303 dl 1.1 */
304     public Set<K> keySet() {
305     if (keySet == null) {
306     keySet = new AbstractSet<K>() {
307     public Iterator<K> iterator() {
308     return new Iterator<K>() {
309     private Iterator<Entry<K,V>> i = entrySet().iterator();
310    
311     public boolean hasNext() {
312     return i.hasNext();
313     }
314    
315     public K next() {
316     return i.next().getKey();
317     }
318    
319     public void remove() {
320     i.remove();
321     }
322     };
323     }
324    
325     public int size() {
326     return AbstractMap.this.size();
327     }
328    
329     public boolean contains(Object k) {
330     return AbstractMap.this.containsKey(k);
331     }
332     };
333     }
334     return keySet;
335     }
336    
337     /**
338 jsr166 1.9 * {@inheritDoc}
339 jsr166 1.7 *
340     * <p>This implementation returns a collection that subclasses {@link
341     * AbstractCollection}. The subclass's iterator method returns a
342     * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
343     * The <tt>size</tt> method delegates to this map's <tt>size</tt>
344     * method and the <tt>contains</tt> method delegates to this map's
345     * <tt>containsValue</tt> method.
346 dl 1.1 *
347 jsr166 1.7 * <p>The collection is created the first time this method is called, and
348 dl 1.1 * returned in response to all subsequent calls. No synchronization is
349     * performed, so there is a slight chance that multiple calls to this
350 jsr166 1.7 * method will not all return the same collection.
351 dl 1.1 */
352     public Collection<V> values() {
353     if (values == null) {
354     values = new AbstractCollection<V>() {
355     public Iterator<V> iterator() {
356     return new Iterator<V>() {
357     private Iterator<Entry<K,V>> i = entrySet().iterator();
358    
359     public boolean hasNext() {
360     return i.hasNext();
361     }
362    
363     public V next() {
364     return i.next().getValue();
365     }
366    
367     public void remove() {
368     i.remove();
369     }
370     };
371     }
372    
373     public int size() {
374     return AbstractMap.this.size();
375     }
376    
377     public boolean contains(Object v) {
378     return AbstractMap.this.containsValue(v);
379     }
380     };
381     }
382     return values;
383     }
384    
385     public abstract Set<Entry<K,V>> entrySet();
386    
387    
388     // Comparison and hashing
389    
390     /**
391 jsr166 1.14 * Compares the specified object with this map for equality. Returns
392     * <tt>true</tt> if the given object is also a map and the two maps
393     * represent the same mappings. More formally, two maps <tt>m1</tt> and
394     * <tt>m2</tt> represent the same mappings if
395     * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
396     * <tt>equals</tt> method works properly across different implementations
397     * of the <tt>Map</tt> interface.
398 dl 1.1 *
399 jsr166 1.9 * <p>This implementation first checks if the specified object is this map;
400 dl 1.1 * if so it returns <tt>true</tt>. Then, it checks if the specified
401 jsr166 1.8 * object is a map whose size is identical to the size of this map; if
402 dl 1.1 * not, it returns <tt>false</tt>. If so, it iterates over this map's
403     * <tt>entrySet</tt> collection, and checks that the specified map
404     * contains each mapping that this map contains. If the specified map
405     * fails to contain such a mapping, <tt>false</tt> is returned. If the
406     * iteration completes, <tt>true</tt> is returned.
407 jsr166 1.14 *
408     * @param o object to be compared for equality with this map
409     * @return <tt>true</tt> if the specified object is equal to this map
410 dl 1.1 */
411     public boolean equals(Object o) {
412     if (o == this)
413     return true;
414    
415     if (!(o instanceof Map))
416     return false;
417 jsr166 1.13 Map<K,V> m = (Map<K,V>) o;
418     if (m.size() != size())
419 dl 1.1 return false;
420    
421     try {
422     Iterator<Entry<K,V>> i = entrySet().iterator();
423     while (i.hasNext()) {
424     Entry<K,V> e = i.next();
425     K key = e.getKey();
426     V value = e.getValue();
427     if (value == null) {
428 jsr166 1.13 if (!(m.get(key)==null && m.containsKey(key)))
429 dl 1.1 return false;
430     } else {
431 jsr166 1.13 if (!value.equals(m.get(key)))
432 dl 1.1 return false;
433     }
434     }
435 jsr166 1.7 } catch (ClassCastException unused) {
436 dl 1.1 return false;
437 jsr166 1.7 } catch (NullPointerException unused) {
438 dl 1.1 return false;
439     }
440    
441     return true;
442     }
443    
444     /**
445 jsr166 1.14 * Returns the hash code value for this map. The hash code of a map is
446     * defined to be the sum of the hash codes of each entry in the map's
447     * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
448     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
449     * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
450     * {@link Object#hashCode}.
451 jsr166 1.9 *
452     * <p>This implementation iterates over <tt>entrySet()</tt>, calling
453 jsr166 1.14 * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
454     * set, and adding up the results.
455 dl 1.1 *
456 jsr166 1.14 * @return the hash code value for this map
457 dl 1.1 * @see Map.Entry#hashCode()
458     * @see Object#equals(Object)
459     * @see Set#equals(Object)
460     */
461     public int hashCode() {
462     int h = 0;
463     Iterator<Entry<K,V>> i = entrySet().iterator();
464     while (i.hasNext())
465     h += i.next().hashCode();
466     return h;
467     }
468    
469     /**
470     * Returns a string representation of this map. The string representation
471     * consists of a list of key-value mappings in the order returned by the
472     * map's <tt>entrySet</tt> view's iterator, enclosed in braces
473     * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters
474     * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as
475     * the key followed by an equals sign (<tt>"="</tt>) followed by the
476     * associated value. Keys and values are converted to strings as by
477     * <tt>String.valueOf(Object)</tt>.<p>
478     *
479     * This implementation creates an empty string buffer, appends a left
480     * brace, and iterates over the map's <tt>entrySet</tt> view, appending
481     * the string representation of each <tt>map.entry</tt> in turn. After
482     * appending each entry except the last, the string <tt>", "</tt> is
483     * appended. Finally a right brace is appended. A string is obtained
484     * from the stringbuffer, and returned.
485     *
486 jsr166 1.11 * @return a String representation of this map
487 dl 1.1 */
488     public String toString() {
489 jsr166 1.12 StringBuilder sb = new StringBuilder();
490     sb.append("{");
491 dl 1.1
492     Iterator<Entry<K,V>> i = entrySet().iterator();
493     boolean hasNext = i.hasNext();
494     while (hasNext) {
495     Entry<K,V> e = i.next();
496     K key = e.getKey();
497     V value = e.getValue();
498     if (key == this)
499 jsr166 1.12 sb.append("(this Map)");
500 dl 1.1 else
501 jsr166 1.12 sb.append(key);
502     sb.append("=");
503 dl 1.1 if (value == this)
504 jsr166 1.12 sb.append("(this Map)");
505 dl 1.1 else
506 jsr166 1.12 sb.append(value);
507 dl 1.1 hasNext = i.hasNext();
508     if (hasNext)
509 jsr166 1.12 sb.append(", ");
510 dl 1.1 }
511    
512 jsr166 1.12 sb.append("}");
513     return sb.toString();
514 dl 1.1 }
515 jsr166 1.7
516 dl 1.1 /**
517     * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
518     * and values themselves are not cloned.
519     *
520 jsr166 1.9 * @return a shallow copy of this map
521 dl 1.1 */
522     protected Object clone() throws CloneNotSupportedException {
523     AbstractMap<K,V> result = (AbstractMap<K,V>)super.clone();
524     result.keySet = null;
525     result.values = null;
526     return result;
527     }
528    
529     /**
530     * Utility method for SimpleEntry and SimpleImmutableEntry.
531     * Test for equality, checking for nulls.
532     */
533     private static boolean eq(Object o1, Object o2) {
534     return (o1 == null ? o2 == null : o1.equals(o2));
535     }
536    
537     // Implementation Note: SimpleEntry and SimpleImmutableEntry
538     // are distinct unrelated classes, even though they share
539     // some code. Since you can't add or subtract final-ness
540 dl 1.3 // of a field in a subclass, they can't share representations,
541 dl 1.1 // and the amount of duplicated code is too small to warrant
542     // exposing a common abstract class.
543    
544    
545     /**
546     * An Entry maintaining a key and a value. The value may be
547     * changed using the <tt>setValue</tt> method. This class
548     * facilitates the process of building custom map
549     * implementations. For example, it may be convenient to return
550     * arrays of <tt>SimpleEntry</tt> instances in method
551     * <tt>Map.entrySet().toArray</tt>
552     */
553     public static class SimpleEntry<K,V> implements Entry<K,V> {
554 dl 1.2 private final K key;
555 dl 1.1 private V value;
556    
557     /**
558     * Creates an entry representing a mapping from the specified
559     * key to the specified value.
560     *
561     * @param key the key represented by this entry
562     * @param value the value represented by this entry
563     */
564     public SimpleEntry(K key, V value) {
565     this.key = key;
566     this.value = value;
567     }
568    
569     /**
570     * Creates an entry representing the same mapping as the
571     * specified entry.
572     *
573 jsr166 1.11 * @param entry the entry to copy
574 dl 1.1 */
575 dl 1.4 public SimpleEntry(Entry<? extends K, ? extends V> entry) {
576 dl 1.1 this.key = entry.getKey();
577     this.value = entry.getValue();
578     }
579    
580     /**
581     * Returns the key corresponding to this entry.
582     *
583 jsr166 1.9 * @return the key corresponding to this entry
584 dl 1.1 */
585     public K getKey() {
586     return key;
587     }
588    
589     /**
590     * Returns the value corresponding to this entry.
591     *
592 jsr166 1.9 * @return the value corresponding to this entry
593 dl 1.1 */
594     public V getValue() {
595     return value;
596     }
597    
598     /**
599     * Replaces the value corresponding to this entry with the specified
600     * value.
601     *
602 jsr166 1.9 * @param value new value to be stored in this entry
603     * @return the old value corresponding to the entry
604 dl 1.1 */
605     public V setValue(V value) {
606     V oldValue = this.value;
607     this.value = value;
608     return oldValue;
609     }
610    
611     public boolean equals(Object o) {
612     if (!(o instanceof Map.Entry))
613     return false;
614     Map.Entry e = (Map.Entry)o;
615     return eq(key, e.getKey()) && eq(value, e.getValue());
616     }
617    
618     public int hashCode() {
619     return ((key == null) ? 0 : key.hashCode()) ^
620     ((value == null) ? 0 : value.hashCode());
621     }
622    
623     /**
624     * Returns a String representation of this map entry. This
625     * implementation returns the string representation of this
626     * entry's key followed by the equals character ("<tt>=</tt>")
627     * followed by the string representation of this entry's value.
628     *
629 jsr166 1.9 * @return a String representation of this map entry
630 dl 1.1 */
631     public String toString() {
632     return key + "=" + value;
633     }
634    
635     }
636    
637     /**
638     * An Entry maintaining an immutable key and value, This class
639     * does not support method <tt>setValue</tt>. This class may be
640     * convenient in methods that return thread-safe snapshots of
641     * key-value mappings.
642     */
643     public static class SimpleImmutableEntry<K,V> implements Entry<K,V> {
644     private final K key;
645     private final V value;
646    
647     /**
648     * Creates an entry representing a mapping from the specified
649     * key to the specified value.
650     *
651     * @param key the key represented by this entry
652     * @param value the value represented by this entry
653     */
654     public SimpleImmutableEntry(K key, V value) {
655     this.key = key;
656     this.value = value;
657     }
658    
659     /**
660     * Creates an entry representing the same mapping as the
661     * specified entry.
662     *
663 jsr166 1.9 * @param entry the entry to copy
664 dl 1.1 */
665 dl 1.4 public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
666 dl 1.1 this.key = entry.getKey();
667     this.value = entry.getValue();
668     }
669    
670     /**
671     * Returns the key corresponding to this entry.
672     *
673 jsr166 1.9 * @return the key corresponding to this entry
674 dl 1.1 */
675     public K getKey() {
676     return key;
677     }
678    
679     /**
680     * Returns the value corresponding to this entry.
681     *
682 jsr166 1.9 * @return the value corresponding to this entry
683 dl 1.1 */
684     public V getValue() {
685     return value;
686     }
687    
688     /**
689     * Replaces the value corresponding to this entry with the specified
690     * value (optional operation). This implementation simply throws
691     * <tt>UnsupportedOperationException</tt>, as this class implements
692     * an <i>immutable</i> map entry.
693     *
694 jsr166 1.9 * @param value new value to be stored in this entry
695 dl 1.1 * @return (Does not return)
696     * @throws UnsupportedOperationException always
697     */
698     public V setValue(V value) {
699     throw new UnsupportedOperationException();
700     }
701    
702     public boolean equals(Object o) {
703     if (!(o instanceof Map.Entry))
704     return false;
705     Map.Entry e = (Map.Entry)o;
706     return eq(key, e.getKey()) && eq(value, e.getValue());
707     }
708    
709     public int hashCode() {
710     return ((key == null) ? 0 : key.hashCode()) ^
711     ((value == null) ? 0 : value.hashCode());
712     }
713    
714     /**
715     * Returns a String representation of this map entry. This
716     * implementation returns the string representation of this
717     * entry's key followed by the equals character ("<tt>=</tt>")
718     * followed by the string representation of this entry's value.
719     *
720 jsr166 1.9 * @return a String representation of this map entry
721 dl 1.1 */
722     public String toString() {
723     return key + "=" + value;
724     }
725    
726     }
727    
728     }