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