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