1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. |
3 |
> |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
|
* |
5 |
< |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
6 |
< |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
5 |
> |
* This code is free software; you can redistribute it and/or modify it |
6 |
> |
* under the terms of the GNU General Public License version 2 only, as |
7 |
> |
* published by the Free Software Foundation. Sun designates this |
8 |
> |
* particular file as subject to the "Classpath" exception as provided |
9 |
> |
* by Sun in the LICENSE file that accompanied this code. |
10 |
> |
* |
11 |
> |
* This code is distributed in the hope that it will be useful, but WITHOUT |
12 |
> |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
> |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
> |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
> |
* accompanied this code). |
16 |
> |
* |
17 |
> |
* You should have received a copy of the GNU General Public License version |
18 |
> |
* 2 along with this work; if not, write to the Free Software Foundation, |
19 |
> |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
> |
* |
21 |
> |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
> |
* or visit www.oracle.com if you need additional information or have any |
23 |
> |
* questions. |
24 |
|
*/ |
25 |
|
|
26 |
|
package java.util; |
9 |
– |
import java.util.*; // for javadoc (till 6280605 is fixed) |
27 |
|
import java.util.Map.Entry; |
28 |
|
|
29 |
|
/** |
47 |
|
* constructor, as per the recommendation in the <tt>Map</tt> interface |
48 |
|
* specification. |
49 |
|
* |
50 |
< |
* <p>The documentation for each non-abstract methods in this class describes its |
50 |
> |
* <p>The documentation for each non-abstract method in this class describes its |
51 |
|
* implementation in detail. Each of these methods may be overridden if the |
52 |
|
* map being implemented admits a more efficient implementation. |
53 |
|
* |
54 |
|
* <p>This class is a member of the |
55 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
55 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
56 |
|
* Java Collections Framework</a>. |
57 |
|
* |
58 |
|
* @param <K> the type of keys maintained by this map |
60 |
|
* |
61 |
|
* @author Josh Bloch |
62 |
|
* @author Neal Gafter |
46 |
– |
* @version %I%, %G% |
63 |
|
* @see Map |
64 |
|
* @see Collection |
65 |
|
* @since 1.2 |
81 |
|
* <p>This implementation returns <tt>entrySet().size()</tt>. |
82 |
|
*/ |
83 |
|
public int size() { |
84 |
< |
return entrySet().size(); |
84 |
> |
return entrySet().size(); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
90 |
|
* <p>This implementation returns <tt>size() == 0</tt>. |
91 |
|
*/ |
92 |
|
public boolean isEmpty() { |
93 |
< |
return size() == 0; |
93 |
> |
return size() == 0; |
94 |
|
} |
95 |
|
|
96 |
|
/** |
106 |
|
* @throws NullPointerException {@inheritDoc} |
107 |
|
*/ |
108 |
|
public boolean containsValue(Object value) { |
109 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
110 |
< |
if (value==null) { |
111 |
< |
while (i.hasNext()) { |
112 |
< |
Entry<K,V> e = i.next(); |
113 |
< |
if (e.getValue()==null) |
114 |
< |
return true; |
115 |
< |
} |
116 |
< |
} else { |
117 |
< |
while (i.hasNext()) { |
118 |
< |
Entry<K,V> e = i.next(); |
119 |
< |
if (value.equals(e.getValue())) |
120 |
< |
return true; |
121 |
< |
} |
122 |
< |
} |
123 |
< |
return false; |
109 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
110 |
> |
if (value==null) { |
111 |
> |
while (i.hasNext()) { |
112 |
> |
Entry<K,V> e = i.next(); |
113 |
> |
if (e.getValue()==null) |
114 |
> |
return true; |
115 |
> |
} |
116 |
> |
} else { |
117 |
> |
while (i.hasNext()) { |
118 |
> |
Entry<K,V> e = i.next(); |
119 |
> |
if (value.equals(e.getValue())) |
120 |
> |
return true; |
121 |
> |
} |
122 |
> |
} |
123 |
> |
return false; |
124 |
|
} |
125 |
|
|
126 |
|
/** |
137 |
|
* @throws NullPointerException {@inheritDoc} |
138 |
|
*/ |
139 |
|
public boolean containsKey(Object key) { |
140 |
< |
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
141 |
< |
if (key==null) { |
142 |
< |
while (i.hasNext()) { |
143 |
< |
Entry<K,V> e = i.next(); |
144 |
< |
if (e.getKey()==null) |
145 |
< |
return true; |
146 |
< |
} |
147 |
< |
} else { |
148 |
< |
while (i.hasNext()) { |
149 |
< |
Entry<K,V> e = i.next(); |
150 |
< |
if (key.equals(e.getKey())) |
151 |
< |
return true; |
152 |
< |
} |
153 |
< |
} |
154 |
< |
return false; |
140 |
> |
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
141 |
> |
if (key==null) { |
142 |
> |
while (i.hasNext()) { |
143 |
> |
Entry<K,V> e = i.next(); |
144 |
> |
if (e.getKey()==null) |
145 |
> |
return true; |
146 |
> |
} |
147 |
> |
} else { |
148 |
> |
while (i.hasNext()) { |
149 |
> |
Entry<K,V> e = i.next(); |
150 |
> |
if (key.equals(e.getKey())) |
151 |
> |
return true; |
152 |
> |
} |
153 |
> |
} |
154 |
> |
return false; |
155 |
|
} |
156 |
|
|
157 |
|
/** |
168 |
|
* @throws NullPointerException {@inheritDoc} |
169 |
|
*/ |
170 |
|
public V get(Object key) { |
171 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
172 |
< |
if (key==null) { |
173 |
< |
while (i.hasNext()) { |
174 |
< |
Entry<K,V> e = i.next(); |
175 |
< |
if (e.getKey()==null) |
176 |
< |
return e.getValue(); |
177 |
< |
} |
178 |
< |
} else { |
179 |
< |
while (i.hasNext()) { |
180 |
< |
Entry<K,V> e = i.next(); |
181 |
< |
if (key.equals(e.getKey())) |
182 |
< |
return e.getValue(); |
183 |
< |
} |
184 |
< |
} |
185 |
< |
return null; |
171 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
172 |
> |
if (key==null) { |
173 |
> |
while (i.hasNext()) { |
174 |
> |
Entry<K,V> e = i.next(); |
175 |
> |
if (e.getKey()==null) |
176 |
> |
return e.getValue(); |
177 |
> |
} |
178 |
> |
} else { |
179 |
> |
while (i.hasNext()) { |
180 |
> |
Entry<K,V> e = i.next(); |
181 |
> |
if (key.equals(e.getKey())) |
182 |
> |
return e.getValue(); |
183 |
> |
} |
184 |
> |
} |
185 |
> |
return null; |
186 |
|
} |
187 |
|
|
188 |
|
|
200 |
|
* @throws IllegalArgumentException {@inheritDoc} |
201 |
|
*/ |
202 |
|
public V put(K key, V value) { |
203 |
< |
throw new UnsupportedOperationException(); |
203 |
> |
throw new UnsupportedOperationException(); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
225 |
|
* @throws NullPointerException {@inheritDoc} |
226 |
|
*/ |
227 |
|
public V remove(Object key) { |
228 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
229 |
< |
Entry<K,V> correctEntry = null; |
230 |
< |
if (key==null) { |
231 |
< |
while (correctEntry==null && i.hasNext()) { |
232 |
< |
Entry<K,V> e = i.next(); |
233 |
< |
if (e.getKey()==null) |
234 |
< |
correctEntry = e; |
235 |
< |
} |
236 |
< |
} else { |
237 |
< |
while (correctEntry==null && i.hasNext()) { |
238 |
< |
Entry<K,V> e = i.next(); |
239 |
< |
if (key.equals(e.getKey())) |
240 |
< |
correctEntry = e; |
241 |
< |
} |
242 |
< |
} |
243 |
< |
|
244 |
< |
V oldValue = null; |
245 |
< |
if (correctEntry !=null) { |
246 |
< |
oldValue = correctEntry.getValue(); |
247 |
< |
i.remove(); |
248 |
< |
} |
249 |
< |
return oldValue; |
228 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
229 |
> |
Entry<K,V> correctEntry = null; |
230 |
> |
if (key==null) { |
231 |
> |
while (correctEntry==null && i.hasNext()) { |
232 |
> |
Entry<K,V> e = i.next(); |
233 |
> |
if (e.getKey()==null) |
234 |
> |
correctEntry = e; |
235 |
> |
} |
236 |
> |
} else { |
237 |
> |
while (correctEntry==null && i.hasNext()) { |
238 |
> |
Entry<K,V> e = i.next(); |
239 |
> |
if (key.equals(e.getKey())) |
240 |
> |
correctEntry = e; |
241 |
> |
} |
242 |
> |
} |
243 |
> |
|
244 |
> |
V oldValue = null; |
245 |
> |
if (correctEntry !=null) { |
246 |
> |
oldValue = correctEntry.getValue(); |
247 |
> |
i.remove(); |
248 |
> |
} |
249 |
> |
return oldValue; |
250 |
|
} |
251 |
|
|
252 |
|
|
285 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
286 |
|
*/ |
287 |
|
public void clear() { |
288 |
< |
entrySet().clear(); |
288 |
> |
entrySet().clear(); |
289 |
|
} |
290 |
|
|
291 |
|
|
315 |
|
* method will not all return the same set. |
316 |
|
*/ |
317 |
|
public Set<K> keySet() { |
318 |
< |
if (keySet == null) { |
319 |
< |
keySet = new AbstractSet<K>() { |
320 |
< |
public Iterator<K> iterator() { |
321 |
< |
return new Iterator<K>() { |
322 |
< |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
323 |
< |
|
324 |
< |
public boolean hasNext() { |
325 |
< |
return i.hasNext(); |
326 |
< |
} |
327 |
< |
|
328 |
< |
public K next() { |
329 |
< |
return i.next().getKey(); |
330 |
< |
} |
331 |
< |
|
332 |
< |
public void remove() { |
333 |
< |
i.remove(); |
334 |
< |
} |
318 |
> |
if (keySet == null) { |
319 |
> |
keySet = new AbstractSet<K>() { |
320 |
> |
public Iterator<K> iterator() { |
321 |
> |
return new Iterator<K>() { |
322 |
> |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
323 |
> |
|
324 |
> |
public boolean hasNext() { |
325 |
> |
return i.hasNext(); |
326 |
> |
} |
327 |
> |
|
328 |
> |
public K next() { |
329 |
> |
return i.next().getKey(); |
330 |
> |
} |
331 |
> |
|
332 |
> |
public void remove() { |
333 |
> |
i.remove(); |
334 |
> |
} |
335 |
|
}; |
336 |
< |
} |
336 |
> |
} |
337 |
|
|
338 |
< |
public int size() { |
339 |
< |
return AbstractMap.this.size(); |
340 |
< |
} |
341 |
< |
|
342 |
< |
public boolean contains(Object k) { |
343 |
< |
return AbstractMap.this.containsKey(k); |
344 |
< |
} |
345 |
< |
}; |
346 |
< |
} |
347 |
< |
return keySet; |
338 |
> |
public int size() { |
339 |
> |
return AbstractMap.this.size(); |
340 |
> |
} |
341 |
> |
|
342 |
> |
public boolean isEmpty() { |
343 |
> |
return AbstractMap.this.isEmpty(); |
344 |
> |
} |
345 |
> |
|
346 |
> |
public void clear() { |
347 |
> |
AbstractMap.this.clear(); |
348 |
> |
} |
349 |
> |
|
350 |
> |
public boolean contains(Object k) { |
351 |
> |
return AbstractMap.this.containsKey(k); |
352 |
> |
} |
353 |
> |
}; |
354 |
> |
} |
355 |
> |
return keySet; |
356 |
|
} |
357 |
|
|
358 |
|
/** |
371 |
|
* method will not all return the same collection. |
372 |
|
*/ |
373 |
|
public Collection<V> values() { |
374 |
< |
if (values == null) { |
375 |
< |
values = new AbstractCollection<V>() { |
376 |
< |
public Iterator<V> iterator() { |
377 |
< |
return new Iterator<V>() { |
378 |
< |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
379 |
< |
|
380 |
< |
public boolean hasNext() { |
381 |
< |
return i.hasNext(); |
382 |
< |
} |
383 |
< |
|
384 |
< |
public V next() { |
385 |
< |
return i.next().getValue(); |
386 |
< |
} |
387 |
< |
|
388 |
< |
public void remove() { |
389 |
< |
i.remove(); |
390 |
< |
} |
374 |
> |
if (values == null) { |
375 |
> |
values = new AbstractCollection<V>() { |
376 |
> |
public Iterator<V> iterator() { |
377 |
> |
return new Iterator<V>() { |
378 |
> |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
379 |
> |
|
380 |
> |
public boolean hasNext() { |
381 |
> |
return i.hasNext(); |
382 |
> |
} |
383 |
> |
|
384 |
> |
public V next() { |
385 |
> |
return i.next().getValue(); |
386 |
> |
} |
387 |
> |
|
388 |
> |
public void remove() { |
389 |
> |
i.remove(); |
390 |
> |
} |
391 |
|
}; |
392 |
|
} |
393 |
|
|
394 |
< |
public int size() { |
395 |
< |
return AbstractMap.this.size(); |
396 |
< |
} |
397 |
< |
|
398 |
< |
public boolean contains(Object v) { |
399 |
< |
return AbstractMap.this.containsValue(v); |
400 |
< |
} |
401 |
< |
}; |
402 |
< |
} |
403 |
< |
return values; |
394 |
> |
public int size() { |
395 |
> |
return AbstractMap.this.size(); |
396 |
> |
} |
397 |
> |
|
398 |
> |
public boolean isEmpty() { |
399 |
> |
return AbstractMap.this.isEmpty(); |
400 |
> |
} |
401 |
> |
|
402 |
> |
public void clear() { |
403 |
> |
AbstractMap.this.clear(); |
404 |
> |
} |
405 |
> |
|
406 |
> |
public boolean contains(Object v) { |
407 |
> |
return AbstractMap.this.containsValue(v); |
408 |
> |
} |
409 |
> |
}; |
410 |
> |
} |
411 |
> |
return values; |
412 |
|
} |
413 |
|
|
414 |
|
public abstract Set<Entry<K,V>> entrySet(); |
438 |
|
* @return <tt>true</tt> if the specified object is equal to this map |
439 |
|
*/ |
440 |
|
public boolean equals(Object o) { |
441 |
< |
if (o == this) |
442 |
< |
return true; |
441 |
> |
if (o == this) |
442 |
> |
return true; |
443 |
|
|
444 |
< |
if (!(o instanceof Map)) |
445 |
< |
return false; |
446 |
< |
Map<K,V> m = (Map<K,V>) o; |
447 |
< |
if (m.size() != size()) |
448 |
< |
return false; |
444 |
> |
if (!(o instanceof Map)) |
445 |
> |
return false; |
446 |
> |
Map<K,V> m = (Map<K,V>) o; |
447 |
> |
if (m.size() != size()) |
448 |
> |
return false; |
449 |
|
|
450 |
|
try { |
451 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
452 |
|
while (i.hasNext()) { |
453 |
|
Entry<K,V> e = i.next(); |
454 |
< |
K key = e.getKey(); |
454 |
> |
K key = e.getKey(); |
455 |
|
V value = e.getValue(); |
456 |
|
if (value == null) { |
457 |
|
if (!(m.get(key)==null && m.containsKey(key))) |
467 |
|
return false; |
468 |
|
} |
469 |
|
|
470 |
< |
return true; |
470 |
> |
return true; |
471 |
|
} |
472 |
|
|
473 |
|
/** |
488 |
|
* @see Set#equals(Object) |
489 |
|
*/ |
490 |
|
public int hashCode() { |
491 |
< |
int h = 0; |
492 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
493 |
< |
while (i.hasNext()) |
494 |
< |
h += i.next().hashCode(); |
495 |
< |
return h; |
491 |
> |
int h = 0; |
492 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
493 |
> |
while (i.hasNext()) |
494 |
> |
h += i.next().hashCode(); |
495 |
> |
return h; |
496 |
|
} |
497 |
|
|
498 |
|
/** |
508 |
|
* @return a string representation of this map |
509 |
|
*/ |
510 |
|
public String toString() { |
511 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
512 |
< |
if (! i.hasNext()) |
513 |
< |
return "{}"; |
514 |
< |
|
515 |
< |
StringBuilder sb = new StringBuilder(); |
516 |
< |
sb.append('{'); |
517 |
< |
for (;;) { |
518 |
< |
Entry<K,V> e = i.next(); |
519 |
< |
K key = e.getKey(); |
520 |
< |
V value = e.getValue(); |
521 |
< |
sb.append(key == this ? "(this Map)" : key); |
522 |
< |
sb.append('='); |
523 |
< |
sb.append(value == this ? "(this Map)" : value); |
524 |
< |
if (! i.hasNext()) |
525 |
< |
return sb.append('}').toString(); |
526 |
< |
sb.append(", "); |
527 |
< |
} |
511 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
512 |
> |
if (! i.hasNext()) |
513 |
> |
return "{}"; |
514 |
> |
|
515 |
> |
StringBuilder sb = new StringBuilder(); |
516 |
> |
sb.append('{'); |
517 |
> |
for (;;) { |
518 |
> |
Entry<K,V> e = i.next(); |
519 |
> |
K key = e.getKey(); |
520 |
> |
V value = e.getValue(); |
521 |
> |
sb.append(key == this ? "(this Map)" : key); |
522 |
> |
sb.append('='); |
523 |
> |
sb.append(value == this ? "(this Map)" : value); |
524 |
> |
if (! i.hasNext()) |
525 |
> |
return sb.append('}').toString(); |
526 |
> |
sb.append(',').append(' '); |
527 |
> |
} |
528 |
|
} |
529 |
|
|
530 |
|
/** |
567 |
|
* @since 1.6 |
568 |
|
*/ |
569 |
|
public static class SimpleEntry<K,V> |
570 |
< |
implements Entry<K,V>, java.io.Serializable |
570 |
> |
implements Entry<K,V>, java.io.Serializable |
571 |
|
{ |
572 |
< |
private static final long serialVersionUID = -8499721149061103585L; |
572 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
573 |
|
|
574 |
< |
private final K key; |
575 |
< |
private V value; |
574 |
> |
private final K key; |
575 |
> |
private V value; |
576 |
|
|
577 |
|
/** |
578 |
|
* Creates an entry representing a mapping from the specified |
581 |
|
* @param key the key represented by this entry |
582 |
|
* @param value the value represented by this entry |
583 |
|
*/ |
584 |
< |
public SimpleEntry(K key, V value) { |
585 |
< |
this.key = key; |
584 |
> |
public SimpleEntry(K key, V value) { |
585 |
> |
this.key = key; |
586 |
|
this.value = value; |
587 |
< |
} |
587 |
> |
} |
588 |
|
|
589 |
|
/** |
590 |
|
* Creates an entry representing the same mapping as the |
592 |
|
* |
593 |
|
* @param entry the entry to copy |
594 |
|
*/ |
595 |
< |
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
596 |
< |
this.key = entry.getKey(); |
595 |
> |
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
596 |
> |
this.key = entry.getKey(); |
597 |
|
this.value = entry.getValue(); |
598 |
< |
} |
598 |
> |
} |
599 |
> |
|
600 |
> |
/** |
601 |
> |
* Returns the key corresponding to this entry. |
602 |
> |
* |
603 |
> |
* @return the key corresponding to this entry |
604 |
> |
*/ |
605 |
> |
public K getKey() { |
606 |
> |
return key; |
607 |
> |
} |
608 |
> |
|
609 |
> |
/** |
610 |
> |
* Returns the value corresponding to this entry. |
611 |
> |
* |
612 |
> |
* @return the value corresponding to this entry |
613 |
> |
*/ |
614 |
> |
public V getValue() { |
615 |
> |
return value; |
616 |
> |
} |
617 |
> |
|
618 |
> |
/** |
619 |
> |
* Replaces the value corresponding to this entry with the specified |
620 |
> |
* value. |
621 |
> |
* |
622 |
> |
* @param value new value to be stored in this entry |
623 |
> |
* @return the old value corresponding to the entry |
624 |
> |
*/ |
625 |
> |
public V setValue(V value) { |
626 |
> |
V oldValue = this.value; |
627 |
> |
this.value = value; |
628 |
> |
return oldValue; |
629 |
> |
} |
630 |
|
|
631 |
< |
/** |
632 |
< |
* Returns the key corresponding to this entry. |
633 |
< |
* |
634 |
< |
* @return the key corresponding to this entry |
635 |
< |
*/ |
636 |
< |
public K getKey() { |
637 |
< |
return key; |
638 |
< |
} |
639 |
< |
|
640 |
< |
/** |
641 |
< |
* Returns the value corresponding to this entry. |
642 |
< |
* |
643 |
< |
* @return the value corresponding to this entry |
644 |
< |
*/ |
645 |
< |
public V getValue() { |
646 |
< |
return value; |
647 |
< |
} |
648 |
< |
|
649 |
< |
/** |
650 |
< |
* Replaces the value corresponding to this entry with the specified |
651 |
< |
* value. |
652 |
< |
* |
653 |
< |
* @param value new value to be stored in this entry |
654 |
< |
* @return the old value corresponding to the entry |
655 |
< |
*/ |
656 |
< |
public V setValue(V value) { |
657 |
< |
V oldValue = this.value; |
658 |
< |
this.value = value; |
659 |
< |
return oldValue; |
660 |
< |
} |
661 |
< |
|
662 |
< |
/** |
663 |
< |
* Compares the specified object with this entry for equality. |
664 |
< |
* Returns {@code true} if the given object is also a map entry and |
665 |
< |
* the two entries represent the same mapping. More formally, two |
666 |
< |
* entries {@code e1} and {@code e2} represent the same mapping |
667 |
< |
* if<pre> |
668 |
< |
* (e1.getKey()==null ? |
669 |
< |
* e2.getKey()==null : |
670 |
< |
* e1.getKey().equals(e2.getKey())) |
671 |
< |
* && |
672 |
< |
* (e1.getValue()==null ? |
673 |
< |
* e2.getValue()==null : |
674 |
< |
* e1.getValue().equals(e2.getValue()))</pre> |
675 |
< |
* 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 |
< |
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 |
< |
/** |
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 |
< |
public int hashCode() { |
641 |
< |
return (key == null ? 0 : key.hashCode()) ^ |
642 |
< |
(value == null ? 0 : value.hashCode()); |
643 |
< |
} |
631 |
> |
/** |
632 |
> |
* Compares the specified object with this entry for equality. |
633 |
> |
* Returns {@code true} if the given object is also a map entry and |
634 |
> |
* the two entries represent the same mapping. More formally, two |
635 |
> |
* entries {@code e1} and {@code e2} represent the same mapping |
636 |
> |
* if<pre> |
637 |
> |
* (e1.getKey()==null ? |
638 |
> |
* e2.getKey()==null : |
639 |
> |
* e1.getKey().equals(e2.getKey())) |
640 |
> |
* && |
641 |
> |
* (e1.getValue()==null ? |
642 |
> |
* e2.getValue()==null : |
643 |
> |
* e1.getValue().equals(e2.getValue()))</pre> |
644 |
> |
* This ensures that the {@code equals} method works properly across |
645 |
> |
* different implementations of the {@code Map.Entry} interface. |
646 |
> |
* |
647 |
> |
* @param o object to be compared for equality with this map entry |
648 |
> |
* @return {@code true} if the specified object is equal to this map |
649 |
> |
* entry |
650 |
> |
* @see #hashCode |
651 |
> |
*/ |
652 |
> |
public boolean equals(Object o) { |
653 |
> |
if (!(o instanceof Map.Entry)) |
654 |
> |
return false; |
655 |
> |
Map.Entry e = (Map.Entry)o; |
656 |
> |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
657 |
> |
} |
658 |
> |
|
659 |
> |
/** |
660 |
> |
* Returns the hash code value for this map entry. The hash code |
661 |
> |
* of a map entry {@code e} is defined to be: <pre> |
662 |
> |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
663 |
> |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
664 |
> |
* This ensures that {@code e1.equals(e2)} implies that |
665 |
> |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
666 |
> |
* {@code e1} and {@code e2}, as required by the general |
667 |
> |
* contract of {@link Object#hashCode}. |
668 |
> |
* |
669 |
> |
* @return the hash code value for this map entry |
670 |
> |
* @see #equals |
671 |
> |
*/ |
672 |
> |
public int hashCode() { |
673 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
674 |
> |
(value == null ? 0 : value.hashCode()); |
675 |
> |
} |
676 |
|
|
677 |
|
/** |
678 |
|
* Returns a String representation of this map entry. This |
682 |
|
* |
683 |
|
* @return a String representation of this map entry |
684 |
|
*/ |
685 |
< |
public String toString() { |
686 |
< |
return key + "=" + value; |
687 |
< |
} |
685 |
> |
public String toString() { |
686 |
> |
return key + "=" + value; |
687 |
> |
} |
688 |
|
|
689 |
|
} |
690 |
|
|
697 |
|
* @since 1.6 |
698 |
|
*/ |
699 |
|
public static class SimpleImmutableEntry<K,V> |
700 |
< |
implements Entry<K,V>, java.io.Serializable |
700 |
> |
implements Entry<K,V>, java.io.Serializable |
701 |
|
{ |
702 |
< |
private static final long serialVersionUID = 7138329143949025153L; |
702 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
703 |
|
|
704 |
< |
private final K key; |
705 |
< |
private final V value; |
704 |
> |
private final K key; |
705 |
> |
private final V value; |
706 |
|
|
707 |
|
/** |
708 |
|
* Creates an entry representing a mapping from the specified |
711 |
|
* @param key the key represented by this entry |
712 |
|
* @param value the value represented by this entry |
713 |
|
*/ |
714 |
< |
public SimpleImmutableEntry(K key, V value) { |
715 |
< |
this.key = key; |
714 |
> |
public SimpleImmutableEntry(K key, V value) { |
715 |
> |
this.key = key; |
716 |
|
this.value = value; |
717 |
< |
} |
717 |
> |
} |
718 |
|
|
719 |
|
/** |
720 |
|
* Creates an entry representing the same mapping as the |
722 |
|
* |
723 |
|
* @param entry the entry to copy |
724 |
|
*/ |
725 |
< |
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
726 |
< |
this.key = entry.getKey(); |
725 |
> |
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
726 |
> |
this.key = entry.getKey(); |
727 |
|
this.value = entry.getValue(); |
728 |
< |
} |
728 |
> |
} |
729 |
> |
|
730 |
> |
/** |
731 |
> |
* Returns the key corresponding to this entry. |
732 |
> |
* |
733 |
> |
* @return the key corresponding to this entry |
734 |
> |
*/ |
735 |
> |
public K getKey() { |
736 |
> |
return key; |
737 |
> |
} |
738 |
> |
|
739 |
> |
/** |
740 |
> |
* Returns the value corresponding to this entry. |
741 |
> |
* |
742 |
> |
* @return the value corresponding to this entry |
743 |
> |
*/ |
744 |
> |
public V getValue() { |
745 |
> |
return value; |
746 |
> |
} |
747 |
|
|
748 |
< |
/** |
749 |
< |
* Returns the key corresponding to this entry. |
750 |
< |
* |
701 |
< |
* @return the key corresponding to this entry |
702 |
< |
*/ |
703 |
< |
public K getKey() { |
704 |
< |
return key; |
705 |
< |
} |
706 |
< |
|
707 |
< |
/** |
708 |
< |
* Returns the value corresponding to this entry. |
709 |
< |
* |
710 |
< |
* @return the value corresponding to this entry |
711 |
< |
*/ |
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 |
748 |
> |
/** |
749 |
> |
* Replaces the value corresponding to this entry with the specified |
750 |
> |
* value (optional operation). This implementation simply throws |
751 |
|
* <tt>UnsupportedOperationException</tt>, as this class implements |
752 |
|
* an <i>immutable</i> map entry. |
753 |
< |
* |
754 |
< |
* @param value new value to be stored in this entry |
755 |
< |
* @return (Does not return) |
756 |
< |
* @throws UnsupportedOperationException always |
753 |
> |
* |
754 |
> |
* @param value new value to be stored in this entry |
755 |
> |
* @return (Does not return) |
756 |
> |
* @throws UnsupportedOperationException always |
757 |
|
*/ |
758 |
< |
public V setValue(V value) { |
758 |
> |
public V setValue(V value) { |
759 |
|
throw new UnsupportedOperationException(); |
760 |
|
} |
761 |
|
|
762 |
< |
/** |
763 |
< |
* Compares the specified object with this entry for equality. |
764 |
< |
* Returns {@code true} if the given object is also a map entry and |
765 |
< |
* the two entries represent the same mapping. More formally, two |
766 |
< |
* entries {@code e1} and {@code e2} represent the same mapping |
767 |
< |
* if<pre> |
768 |
< |
* (e1.getKey()==null ? |
769 |
< |
* e2.getKey()==null : |
770 |
< |
* e1.getKey().equals(e2.getKey())) |
771 |
< |
* && |
772 |
< |
* (e1.getValue()==null ? |
773 |
< |
* e2.getValue()==null : |
774 |
< |
* e1.getValue().equals(e2.getValue()))</pre> |
775 |
< |
* This ensures that the {@code equals} method works properly across |
776 |
< |
* different implementations of the {@code Map.Entry} interface. |
777 |
< |
* |
778 |
< |
* @param o object to be compared for equality with this map entry |
779 |
< |
* @return {@code true} if the specified object is equal to this map |
780 |
< |
* entry |
781 |
< |
* @see #hashCode |
782 |
< |
*/ |
783 |
< |
public boolean equals(Object o) { |
784 |
< |
if (!(o instanceof Map.Entry)) |
785 |
< |
return false; |
786 |
< |
Map.Entry e = (Map.Entry)o; |
787 |
< |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
788 |
< |
} |
789 |
< |
|
790 |
< |
/** |
791 |
< |
* Returns the hash code value for this map entry. The hash code |
792 |
< |
* of a map entry {@code e} is defined to be: <pre> |
793 |
< |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
794 |
< |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
795 |
< |
* This ensures that {@code e1.equals(e2)} implies that |
796 |
< |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
797 |
< |
* {@code e1} and {@code e2}, as required by the general |
798 |
< |
* contract of {@link Object#hashCode}. |
799 |
< |
* |
800 |
< |
* @return the hash code value for this map entry |
801 |
< |
* @see #equals |
802 |
< |
*/ |
803 |
< |
public int hashCode() { |
804 |
< |
return (key == null ? 0 : key.hashCode()) ^ |
805 |
< |
(value == null ? 0 : value.hashCode()); |
806 |
< |
} |
762 |
> |
/** |
763 |
> |
* Compares the specified object with this entry for equality. |
764 |
> |
* Returns {@code true} if the given object is also a map entry and |
765 |
> |
* the two entries represent the same mapping. More formally, two |
766 |
> |
* entries {@code e1} and {@code e2} represent the same mapping |
767 |
> |
* if<pre> |
768 |
> |
* (e1.getKey()==null ? |
769 |
> |
* e2.getKey()==null : |
770 |
> |
* e1.getKey().equals(e2.getKey())) |
771 |
> |
* && |
772 |
> |
* (e1.getValue()==null ? |
773 |
> |
* e2.getValue()==null : |
774 |
> |
* e1.getValue().equals(e2.getValue()))</pre> |
775 |
> |
* This ensures that the {@code equals} method works properly across |
776 |
> |
* different implementations of the {@code Map.Entry} interface. |
777 |
> |
* |
778 |
> |
* @param o object to be compared for equality with this map entry |
779 |
> |
* @return {@code true} if the specified object is equal to this map |
780 |
> |
* entry |
781 |
> |
* @see #hashCode |
782 |
> |
*/ |
783 |
> |
public boolean equals(Object o) { |
784 |
> |
if (!(o instanceof Map.Entry)) |
785 |
> |
return false; |
786 |
> |
Map.Entry e = (Map.Entry)o; |
787 |
> |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
788 |
> |
} |
789 |
> |
|
790 |
> |
/** |
791 |
> |
* Returns the hash code value for this map entry. The hash code |
792 |
> |
* of a map entry {@code e} is defined to be: <pre> |
793 |
> |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
794 |
> |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
795 |
> |
* This ensures that {@code e1.equals(e2)} implies that |
796 |
> |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
797 |
> |
* {@code e1} and {@code e2}, as required by the general |
798 |
> |
* contract of {@link Object#hashCode}. |
799 |
> |
* |
800 |
> |
* @return the hash code value for this map entry |
801 |
> |
* @see #equals |
802 |
> |
*/ |
803 |
> |
public int hashCode() { |
804 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
805 |
> |
(value == null ? 0 : value.hashCode()); |
806 |
> |
} |
807 |
|
|
808 |
|
/** |
809 |
|
* Returns a String representation of this map entry. This |
813 |
|
* |
814 |
|
* @return a String representation of this map entry |
815 |
|
*/ |
816 |
< |
public String toString() { |
817 |
< |
return key + "=" + value; |
818 |
< |
} |
816 |
> |
public String toString() { |
817 |
> |
return key + "=" + value; |
818 |
> |
} |
819 |
|
|
820 |
|
} |
821 |
|
|