author | zmajo |
Fri, 03 Jul 2015 07:23:45 +0200 | |
changeset 31671 | 362e0c0acece |
parent 25859 | 3317bb8137f4 |
child 32108 | aa5490a167ee |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 1998, 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; |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
27 |
|
2 | 28 |
import java.lang.ref.WeakReference; |
29 |
import java.lang.ref.ReferenceQueue; |
|
18166
a24e00a7c5ae
8010325: Remove hash32() method and hash32 int field from java.lang.String
bchristi
parents:
17949
diff
changeset
|
30 |
import java.util.concurrent.ThreadLocalRandom; |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
31 |
import java.util.function.BiConsumer; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
32 |
import java.util.function.BiFunction; |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
33 |
import java.util.function.Consumer; |
2 | 34 |
|
35 |
||
36 |
/** |
|
56
48451b4616e8
5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents:
2
diff
changeset
|
37 |
* Hash table based implementation of the <tt>Map</tt> interface, with |
48451b4616e8
5080227: (coll spec) Bug in documentation for WeakHashMap
martin
parents:
2
diff
changeset
|
38 |
* <em>weak keys</em>. |
2 | 39 |
* An entry in a <tt>WeakHashMap</tt> will automatically be removed when |
40 |
* its key is no longer in ordinary use. More precisely, the presence of a |
|
41 |
* mapping for a given key will not prevent the key from being discarded by the |
|
42 |
* garbage collector, that is, made finalizable, finalized, and then reclaimed. |
|
43 |
* When a key has been discarded its entry is effectively removed from the map, |
|
44 |
* so this class behaves somewhat differently from other <tt>Map</tt> |
|
45 |
* implementations. |
|
46 |
* |
|
47 |
* <p> Both null values and the null key are supported. This class has |
|
48 |
* performance characteristics similar to those of the <tt>HashMap</tt> |
|
49 |
* class, and has the same efficiency parameters of <em>initial capacity</em> |
|
50 |
* and <em>load factor</em>. |
|
51 |
* |
|
52 |
* <p> Like most collection classes, this class is not synchronized. |
|
53 |
* A synchronized <tt>WeakHashMap</tt> may be constructed using the |
|
54 |
* {@link Collections#synchronizedMap Collections.synchronizedMap} |
|
55 |
* method. |
|
56 |
* |
|
57 |
* <p> This class is intended primarily for use with key objects whose |
|
58 |
* <tt>equals</tt> methods test for object identity using the |
|
59 |
* <tt>==</tt> operator. Once such a key is discarded it can never be |
|
60 |
* recreated, so it is impossible to do a lookup of that key in a |
|
61 |
* <tt>WeakHashMap</tt> at some later time and be surprised that its entry |
|
62 |
* has been removed. This class will work perfectly well with key objects |
|
63 |
* whose <tt>equals</tt> methods are not based upon object identity, such |
|
64 |
* as <tt>String</tt> instances. With such recreatable key objects, |
|
65 |
* however, the automatic removal of <tt>WeakHashMap</tt> entries whose |
|
66 |
* keys have been discarded may prove to be confusing. |
|
67 |
* |
|
68 |
* <p> The behavior of the <tt>WeakHashMap</tt> class depends in part upon |
|
69 |
* the actions of the garbage collector, so several familiar (though not |
|
70 |
* required) <tt>Map</tt> invariants do not hold for this class. Because |
|
71 |
* the garbage collector may discard keys at any time, a |
|
72 |
* <tt>WeakHashMap</tt> may behave as though an unknown thread is silently |
|
73 |
* removing entries. In particular, even if you synchronize on a |
|
74 |
* <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it |
|
75 |
* is possible for the <tt>size</tt> method to return smaller values over |
|
76 |
* time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and |
|
77 |
* then <tt>true</tt>, for the <tt>containsKey</tt> method to return |
|
78 |
* <tt>true</tt> and later <tt>false</tt> for a given key, for the |
|
79 |
* <tt>get</tt> method to return a value for a given key but later return |
|
80 |
* <tt>null</tt>, for the <tt>put</tt> method to return |
|
81 |
* <tt>null</tt> and the <tt>remove</tt> method to return |
|
82 |
* <tt>false</tt> for a key that previously appeared to be in the map, and |
|
83 |
* for successive examinations of the key set, the value collection, and |
|
84 |
* the entry set to yield successively smaller numbers of elements. |
|
85 |
* |
|
86 |
* <p> Each key object in a <tt>WeakHashMap</tt> is stored indirectly as |
|
87 |
* the referent of a weak reference. Therefore a key will automatically be |
|
88 |
* removed only after the weak references to it, both inside and outside of the |
|
89 |
* map, have been cleared by the garbage collector. |
|
90 |
* |
|
91 |
* <p> <strong>Implementation note:</strong> The value objects in a |
|
92 |
* <tt>WeakHashMap</tt> are held by ordinary strong references. Thus care |
|
93 |
* should be taken to ensure that value objects do not strongly refer to their |
|
94 |
* own keys, either directly or indirectly, since that will prevent the keys |
|
95 |
* from being discarded. Note that a value object may refer indirectly to its |
|
96 |
* key via the <tt>WeakHashMap</tt> itself; that is, a value object may |
|
97 |
* strongly refer to some other key object whose associated value object, in |
|
12864
c404d4a709de
7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents:
12859
diff
changeset
|
98 |
* turn, strongly refers to the key of the first value object. If the values |
c404d4a709de
7166055: Javadoc for WeakHashMap contains misleading advice
littlee
parents:
12859
diff
changeset
|
99 |
* in the map do not rely on the map holding strong references to them, one way |
2 | 100 |
* to deal with this is to wrap values themselves within |
101 |
* <tt>WeakReferences</tt> before |
|
102 |
* inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>, |
|
103 |
* and then unwrapping upon each <tt>get</tt>. |
|
104 |
* |
|
105 |
* <p>The iterators returned by the <tt>iterator</tt> method of the collections |
|
106 |
* returned by all of this class's "collection view methods" are |
|
107 |
* <i>fail-fast</i>: if the map is structurally modified at any time after the |
|
108 |
* iterator is created, in any way except through the iterator's own |
|
109 |
* <tt>remove</tt> method, the iterator will throw a {@link |
|
110 |
* ConcurrentModificationException}. Thus, in the face of concurrent |
|
111 |
* modification, the iterator fails quickly and cleanly, rather than risking |
|
112 |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
|
113 |
* |
|
114 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
|
115 |
* as it is, generally speaking, impossible to make any hard guarantees in the |
|
116 |
* presence of unsynchronized concurrent modification. Fail-fast iterators |
|
117 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
|
118 |
* Therefore, it would be wrong to write a program that depended on this |
|
119 |
* exception for its correctness: <i>the fail-fast behavior of iterators |
|
120 |
* should be used only to detect bugs.</i> |
|
121 |
* |
|
122 |
* <p>This class is a member of the |
|
123 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
124 |
* Java Collections Framework</a>. |
|
125 |
* |
|
126 |
* @param <K> the type of keys maintained by this map |
|
127 |
* @param <V> the type of mapped values |
|
128 |
* |
|
129 |
* @author Doug Lea |
|
130 |
* @author Josh Bloch |
|
131 |
* @author Mark Reinhold |
|
132 |
* @since 1.2 |
|
133 |
* @see java.util.HashMap |
|
134 |
* @see java.lang.ref.WeakReference |
|
135 |
*/ |
|
136 |
public class WeakHashMap<K,V> |
|
137 |
extends AbstractMap<K,V> |
|
138 |
implements Map<K,V> { |
|
139 |
||
140 |
/** |
|
141 |
* The default initial capacity -- MUST be a power of two. |
|
142 |
*/ |
|
143 |
private static final int DEFAULT_INITIAL_CAPACITY = 16; |
|
144 |
||
145 |
/** |
|
146 |
* The maximum capacity, used if a higher value is implicitly specified |
|
147 |
* by either of the constructors with arguments. |
|
148 |
* MUST be a power of two <= 1<<30. |
|
149 |
*/ |
|
150 |
private static final int MAXIMUM_CAPACITY = 1 << 30; |
|
151 |
||
152 |
/** |
|
153 |
* The load factor used when none specified in constructor. |
|
154 |
*/ |
|
155 |
private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
|
156 |
||
157 |
/** |
|
158 |
* The table, resized as necessary. Length MUST Always be a power of two. |
|
159 |
*/ |
|
160 |
Entry<K,V>[] table; |
|
161 |
||
162 |
/** |
|
163 |
* The number of key-value mappings contained in this weak hash map. |
|
164 |
*/ |
|
165 |
private int size; |
|
166 |
||
167 |
/** |
|
168 |
* The next size value at which to resize (capacity * load factor). |
|
169 |
*/ |
|
170 |
private int threshold; |
|
171 |
||
172 |
/** |
|
173 |
* The load factor for the hash table. |
|
174 |
*/ |
|
175 |
private final float loadFactor; |
|
176 |
||
177 |
/** |
|
178 |
* Reference queue for cleared WeakEntries |
|
179 |
*/ |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
180 |
private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
2 | 181 |
|
182 |
/** |
|
183 |
* The number of times this WeakHashMap has been structurally modified. |
|
184 |
* Structural modifications are those that change the number of |
|
185 |
* mappings in the map or otherwise modify its internal structure |
|
186 |
* (e.g., rehash). This field is used to make iterators on |
|
187 |
* Collection-views of the map fail-fast. |
|
188 |
* |
|
189 |
* @see ConcurrentModificationException |
|
190 |
*/ |
|
65 | 191 |
int modCount; |
2 | 192 |
|
193 |
@SuppressWarnings("unchecked") |
|
194 |
private Entry<K,V>[] newTable(int n) { |
|
12448 | 195 |
return (Entry<K,V>[]) new Entry<?,?>[n]; |
2 | 196 |
} |
197 |
||
198 |
/** |
|
199 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the given initial |
|
200 |
* capacity and the given load factor. |
|
201 |
* |
|
202 |
* @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt> |
|
203 |
* @param loadFactor The load factor of the <tt>WeakHashMap</tt> |
|
204 |
* @throws IllegalArgumentException if the initial capacity is negative, |
|
205 |
* or if the load factor is nonpositive. |
|
206 |
*/ |
|
207 |
public WeakHashMap(int initialCapacity, float loadFactor) { |
|
208 |
if (initialCapacity < 0) |
|
209 |
throw new IllegalArgumentException("Illegal Initial Capacity: "+ |
|
210 |
initialCapacity); |
|
211 |
if (initialCapacity > MAXIMUM_CAPACITY) |
|
212 |
initialCapacity = MAXIMUM_CAPACITY; |
|
213 |
||
214 |
if (loadFactor <= 0 || Float.isNaN(loadFactor)) |
|
215 |
throw new IllegalArgumentException("Illegal Load factor: "+ |
|
216 |
loadFactor); |
|
217 |
int capacity = 1; |
|
218 |
while (capacity < initialCapacity) |
|
219 |
capacity <<= 1; |
|
220 |
table = newTable(capacity); |
|
221 |
this.loadFactor = loadFactor; |
|
222 |
threshold = (int)(capacity * loadFactor); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the given initial |
|
227 |
* capacity and the default load factor (0.75). |
|
228 |
* |
|
229 |
* @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt> |
|
230 |
* @throws IllegalArgumentException if the initial capacity is negative |
|
231 |
*/ |
|
232 |
public WeakHashMap(int initialCapacity) { |
|
233 |
this(initialCapacity, DEFAULT_LOAD_FACTOR); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Constructs a new, empty <tt>WeakHashMap</tt> with the default initial |
|
238 |
* capacity (16) and load factor (0.75). |
|
239 |
*/ |
|
240 |
public WeakHashMap() { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
241 |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
2 | 242 |
} |
243 |
||
244 |
/** |
|
245 |
* Constructs a new <tt>WeakHashMap</tt> with the same mappings as the |
|
246 |
* specified map. The <tt>WeakHashMap</tt> is created with the default |
|
247 |
* load factor (0.75) and an initial capacity sufficient to hold the |
|
248 |
* mappings in the specified map. |
|
249 |
* |
|
250 |
* @param m the map whose mappings are to be placed in this map |
|
251 |
* @throws NullPointerException if the specified map is null |
|
252 |
* @since 1.3 |
|
253 |
*/ |
|
254 |
public WeakHashMap(Map<? extends K, ? extends V> m) { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
255 |
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
256 |
DEFAULT_INITIAL_CAPACITY), |
2 | 257 |
DEFAULT_LOAD_FACTOR); |
258 |
putAll(m); |
|
259 |
} |
|
260 |
||
261 |
// internal utilities |
|
262 |
||
263 |
/** |
|
264 |
* Value representing null keys inside tables. |
|
265 |
*/ |
|
266 |
private static final Object NULL_KEY = new Object(); |
|
267 |
||
268 |
/** |
|
269 |
* Use NULL_KEY for key if it is null. |
|
270 |
*/ |
|
271 |
private static Object maskNull(Object key) { |
|
272 |
return (key == null) ? NULL_KEY : key; |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Returns internal representation of null key back to caller as null. |
|
277 |
*/ |
|
278 |
static Object unmaskNull(Object key) { |
|
279 |
return (key == NULL_KEY) ? null : key; |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Checks for equality of non-null reference x and possibly-null y. By |
|
284 |
* default uses Object.equals. |
|
285 |
*/ |
|
286 |
private static boolean eq(Object x, Object y) { |
|
287 |
return x == y || x.equals(y); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
291 |
* Retrieve object hash code and applies a supplemental hash function to the |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
292 |
* result hash, which defends against poor quality hash functions. This is |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
293 |
* critical because HashMap uses power-of-two length hash tables, that |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
294 |
* otherwise encounter collisions for hashCodes that do not differ |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
295 |
* in lower bits. |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
296 |
*/ |
13018 | 297 |
final int hash(Object k) { |
19853
832b09e2714c
8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents:
19208
diff
changeset
|
298 |
int h = k.hashCode(); |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
299 |
|
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
300 |
// This function ensures that hashCodes that differ only by |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
301 |
// constant multiples at each bit position have a bounded |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
302 |
// number of collisions (approximately 8 at default load factor). |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
303 |
h ^= (h >>> 20) ^ (h >>> 12); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
304 |
return h ^ (h >>> 7) ^ (h >>> 4); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
305 |
} |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
306 |
|
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
307 |
/** |
2 | 308 |
* Returns index for hash code h. |
309 |
*/ |
|
310 |
private static int indexFor(int h, int length) { |
|
311 |
return h & (length-1); |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Expunges stale entries from the table. |
|
316 |
*/ |
|
317 |
private void expungeStaleEntries() { |
|
318 |
for (Object x; (x = queue.poll()) != null; ) { |
|
319 |
synchronized (queue) { |
|
320 |
@SuppressWarnings("unchecked") |
|
321 |
Entry<K,V> e = (Entry<K,V>) x; |
|
322 |
int i = indexFor(e.hash, table.length); |
|
323 |
||
324 |
Entry<K,V> prev = table[i]; |
|
325 |
Entry<K,V> p = prev; |
|
326 |
while (p != null) { |
|
327 |
Entry<K,V> next = p.next; |
|
328 |
if (p == e) { |
|
329 |
if (prev == e) |
|
330 |
table[i] = next; |
|
331 |
else |
|
332 |
prev.next = next; |
|
333 |
// Must not null out e.next; |
|
334 |
// stale entries may be in use by a HashIterator |
|
335 |
e.value = null; // Help GC |
|
336 |
size--; |
|
337 |
break; |
|
338 |
} |
|
339 |
prev = p; |
|
340 |
p = next; |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Returns the table after first expunging stale entries. |
|
348 |
*/ |
|
349 |
private Entry<K,V>[] getTable() { |
|
350 |
expungeStaleEntries(); |
|
351 |
return table; |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Returns the number of key-value mappings in this map. |
|
356 |
* This result is a snapshot, and may not reflect unprocessed |
|
357 |
* entries that will be removed before next attempted access |
|
358 |
* because they are no longer referenced. |
|
359 |
*/ |
|
360 |
public int size() { |
|
361 |
if (size == 0) |
|
362 |
return 0; |
|
363 |
expungeStaleEntries(); |
|
364 |
return size; |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
|
369 |
* This result is a snapshot, and may not reflect unprocessed |
|
370 |
* entries that will be removed before next attempted access |
|
371 |
* because they are no longer referenced. |
|
372 |
*/ |
|
373 |
public boolean isEmpty() { |
|
374 |
return size() == 0; |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Returns the value to which the specified key is mapped, |
|
379 |
* or {@code null} if this map contains no mapping for the key. |
|
380 |
* |
|
381 |
* <p>More formally, if this map contains a mapping from a key |
|
382 |
* {@code k} to a value {@code v} such that {@code (key==null ? k==null : |
|
383 |
* key.equals(k))}, then this method returns {@code v}; otherwise |
|
384 |
* it returns {@code null}. (There can be at most one such mapping.) |
|
385 |
* |
|
386 |
* <p>A return value of {@code null} does not <i>necessarily</i> |
|
387 |
* indicate that the map contains no mapping for the key; it's also |
|
388 |
* possible that the map explicitly maps the key to {@code null}. |
|
389 |
* The {@link #containsKey containsKey} operation may be used to |
|
390 |
* distinguish these two cases. |
|
391 |
* |
|
392 |
* @see #put(Object, Object) |
|
393 |
*/ |
|
394 |
public V get(Object key) { |
|
395 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
396 |
int h = hash(k); |
2 | 397 |
Entry<K,V>[] tab = getTable(); |
398 |
int index = indexFor(h, tab.length); |
|
399 |
Entry<K,V> e = tab[index]; |
|
400 |
while (e != null) { |
|
401 |
if (e.hash == h && eq(k, e.get())) |
|
402 |
return e.value; |
|
403 |
e = e.next; |
|
404 |
} |
|
405 |
return null; |
|
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* Returns <tt>true</tt> if this map contains a mapping for the |
|
410 |
* specified key. |
|
411 |
* |
|
412 |
* @param key The key whose presence in this map is to be tested |
|
413 |
* @return <tt>true</tt> if there is a mapping for <tt>key</tt>; |
|
414 |
* <tt>false</tt> otherwise |
|
415 |
*/ |
|
416 |
public boolean containsKey(Object key) { |
|
417 |
return getEntry(key) != null; |
|
418 |
} |
|
419 |
||
420 |
/** |
|
421 |
* Returns the entry associated with the specified key in this map. |
|
422 |
* Returns null if the map contains no mapping for this key. |
|
423 |
*/ |
|
424 |
Entry<K,V> getEntry(Object key) { |
|
425 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
426 |
int h = hash(k); |
2 | 427 |
Entry<K,V>[] tab = getTable(); |
428 |
int index = indexFor(h, tab.length); |
|
429 |
Entry<K,V> e = tab[index]; |
|
430 |
while (e != null && !(e.hash == h && eq(k, e.get()))) |
|
431 |
e = e.next; |
|
432 |
return e; |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Associates the specified value with the specified key in this map. |
|
437 |
* If the map previously contained a mapping for this key, the old |
|
438 |
* value is replaced. |
|
439 |
* |
|
440 |
* @param key key with which the specified value is to be associated. |
|
441 |
* @param value value to be associated with the specified key. |
|
442 |
* @return the previous value associated with <tt>key</tt>, or |
|
443 |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
|
444 |
* (A <tt>null</tt> return can also indicate that the map |
|
445 |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
|
446 |
*/ |
|
447 |
public V put(K key, V value) { |
|
448 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
449 |
int h = hash(k); |
2 | 450 |
Entry<K,V>[] tab = getTable(); |
451 |
int i = indexFor(h, tab.length); |
|
452 |
||
453 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) { |
|
454 |
if (h == e.hash && eq(k, e.get())) { |
|
455 |
V oldValue = e.value; |
|
456 |
if (value != oldValue) |
|
457 |
e.value = value; |
|
458 |
return oldValue; |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
modCount++; |
|
463 |
Entry<K,V> e = tab[i]; |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
464 |
tab[i] = new Entry<>(k, value, queue, h, e); |
2 | 465 |
if (++size >= threshold) |
466 |
resize(tab.length * 2); |
|
467 |
return null; |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Rehashes the contents of this map into a new array with a |
|
472 |
* larger capacity. This method is called automatically when the |
|
473 |
* number of keys in this map reaches its threshold. |
|
474 |
* |
|
475 |
* If current capacity is MAXIMUM_CAPACITY, this method does not |
|
476 |
* resize the map, but sets threshold to Integer.MAX_VALUE. |
|
477 |
* This has the effect of preventing future calls. |
|
478 |
* |
|
479 |
* @param newCapacity the new capacity, MUST be a power of two; |
|
480 |
* must be greater than current capacity unless current |
|
481 |
* capacity is MAXIMUM_CAPACITY (in which case value |
|
482 |
* is irrelevant). |
|
483 |
*/ |
|
484 |
void resize(int newCapacity) { |
|
485 |
Entry<K,V>[] oldTable = getTable(); |
|
486 |
int oldCapacity = oldTable.length; |
|
487 |
if (oldCapacity == MAXIMUM_CAPACITY) { |
|
488 |
threshold = Integer.MAX_VALUE; |
|
489 |
return; |
|
490 |
} |
|
491 |
||
492 |
Entry<K,V>[] newTable = newTable(newCapacity); |
|
493 |
transfer(oldTable, newTable); |
|
494 |
table = newTable; |
|
495 |
||
496 |
/* |
|
497 |
* If ignoring null elements and processing ref queue caused massive |
|
498 |
* shrinkage, then restore old table. This should be rare, but avoids |
|
499 |
* unbounded expansion of garbage-filled tables. |
|
500 |
*/ |
|
501 |
if (size >= threshold / 2) { |
|
502 |
threshold = (int)(newCapacity * loadFactor); |
|
503 |
} else { |
|
504 |
expungeStaleEntries(); |
|
505 |
transfer(newTable, oldTable); |
|
506 |
table = oldTable; |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
/** Transfers all entries from src to dest tables */ |
|
511 |
private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) { |
|
512 |
for (int j = 0; j < src.length; ++j) { |
|
513 |
Entry<K,V> e = src[j]; |
|
514 |
src[j] = null; |
|
515 |
while (e != null) { |
|
516 |
Entry<K,V> next = e.next; |
|
517 |
Object key = e.get(); |
|
518 |
if (key == null) { |
|
519 |
e.next = null; // Help GC |
|
520 |
e.value = null; // " " |
|
521 |
size--; |
|
522 |
} else { |
|
523 |
int i = indexFor(e.hash, dest.length); |
|
524 |
e.next = dest[i]; |
|
525 |
dest[i] = e; |
|
526 |
} |
|
527 |
e = next; |
|
528 |
} |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
/** |
|
533 |
* Copies all of the mappings from the specified map to this map. |
|
534 |
* These mappings will replace any mappings that this map had for any |
|
535 |
* of the keys currently in the specified map. |
|
536 |
* |
|
537 |
* @param m mappings to be stored in this map. |
|
538 |
* @throws NullPointerException if the specified map is null. |
|
539 |
*/ |
|
540 |
public void putAll(Map<? extends K, ? extends V> m) { |
|
541 |
int numKeysToBeAdded = m.size(); |
|
542 |
if (numKeysToBeAdded == 0) |
|
543 |
return; |
|
544 |
||
545 |
/* |
|
546 |
* Expand the map if the map if the number of mappings to be added |
|
547 |
* is greater than or equal to threshold. This is conservative; the |
|
548 |
* obvious condition is (m.size() + size) >= threshold, but this |
|
549 |
* condition could result in a map with twice the appropriate capacity, |
|
550 |
* if the keys to be added overlap with the keys already in this map. |
|
551 |
* By using the conservative calculation, we subject ourself |
|
552 |
* to at most one extra resize. |
|
553 |
*/ |
|
554 |
if (numKeysToBeAdded > threshold) { |
|
555 |
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1); |
|
556 |
if (targetCapacity > MAXIMUM_CAPACITY) |
|
557 |
targetCapacity = MAXIMUM_CAPACITY; |
|
558 |
int newCapacity = table.length; |
|
559 |
while (newCapacity < targetCapacity) |
|
560 |
newCapacity <<= 1; |
|
561 |
if (newCapacity > table.length) |
|
562 |
resize(newCapacity); |
|
563 |
} |
|
564 |
||
565 |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
|
566 |
put(e.getKey(), e.getValue()); |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Removes the mapping for a key from this weak hash map if it is present. |
|
571 |
* More formally, if this map contains a mapping from key <tt>k</tt> to |
|
572 |
* value <tt>v</tt> such that <code>(key==null ? k==null : |
|
573 |
* key.equals(k))</code>, that mapping is removed. (The map can contain |
|
574 |
* at most one such mapping.) |
|
575 |
* |
|
576 |
* <p>Returns the value to which this map previously associated the key, |
|
577 |
* or <tt>null</tt> if the map contained no mapping for the key. A |
|
578 |
* return value of <tt>null</tt> does not <i>necessarily</i> indicate |
|
579 |
* that the map contained no mapping for the key; it's also possible |
|
580 |
* that the map explicitly mapped the key to <tt>null</tt>. |
|
581 |
* |
|
582 |
* <p>The map will not contain a mapping for the specified key once the |
|
583 |
* call returns. |
|
584 |
* |
|
585 |
* @param key key whose mapping is to be removed from the map |
|
586 |
* @return the previous value associated with <tt>key</tt>, or |
|
587 |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
|
588 |
*/ |
|
589 |
public V remove(Object key) { |
|
590 |
Object k = maskNull(key); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
591 |
int h = hash(k); |
2 | 592 |
Entry<K,V>[] tab = getTable(); |
593 |
int i = indexFor(h, tab.length); |
|
594 |
Entry<K,V> prev = tab[i]; |
|
595 |
Entry<K,V> e = prev; |
|
596 |
||
597 |
while (e != null) { |
|
598 |
Entry<K,V> next = e.next; |
|
599 |
if (h == e.hash && eq(k, e.get())) { |
|
600 |
modCount++; |
|
601 |
size--; |
|
602 |
if (prev == e) |
|
603 |
tab[i] = next; |
|
604 |
else |
|
605 |
prev.next = next; |
|
606 |
return e.value; |
|
607 |
} |
|
608 |
prev = e; |
|
609 |
e = next; |
|
610 |
} |
|
611 |
||
612 |
return null; |
|
613 |
} |
|
614 |
||
615 |
/** Special version of remove needed by Entry set */ |
|
616 |
boolean removeMapping(Object o) { |
|
617 |
if (!(o instanceof Map.Entry)) |
|
618 |
return false; |
|
619 |
Entry<K,V>[] tab = getTable(); |
|
620 |
Map.Entry<?,?> entry = (Map.Entry<?,?>)o; |
|
621 |
Object k = maskNull(entry.getKey()); |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
12448
diff
changeset
|
622 |
int h = hash(k); |
2 | 623 |
int i = indexFor(h, tab.length); |
624 |
Entry<K,V> prev = tab[i]; |
|
625 |
Entry<K,V> e = prev; |
|
626 |
||
627 |
while (e != null) { |
|
628 |
Entry<K,V> next = e.next; |
|
629 |
if (h == e.hash && e.equals(entry)) { |
|
630 |
modCount++; |
|
631 |
size--; |
|
632 |
if (prev == e) |
|
633 |
tab[i] = next; |
|
634 |
else |
|
635 |
prev.next = next; |
|
636 |
return true; |
|
637 |
} |
|
638 |
prev = e; |
|
639 |
e = next; |
|
640 |
} |
|
641 |
||
642 |
return false; |
|
643 |
} |
|
644 |
||
645 |
/** |
|
646 |
* Removes all of the mappings from this map. |
|
647 |
* The map will be empty after this call returns. |
|
648 |
*/ |
|
649 |
public void clear() { |
|
650 |
// clear out ref queue. We don't need to expunge entries |
|
651 |
// since table is getting cleared. |
|
652 |
while (queue.poll() != null) |
|
653 |
; |
|
654 |
||
655 |
modCount++; |
|
656 |
Arrays.fill(table, null); |
|
657 |
size = 0; |
|
658 |
||
659 |
// Allocation of array may have caused GC, which may have caused |
|
660 |
// additional entries to go stale. Removing these entries from the |
|
661 |
// reference queue will make them eligible for reclamation. |
|
662 |
while (queue.poll() != null) |
|
663 |
; |
|
664 |
} |
|
665 |
||
666 |
/** |
|
667 |
* Returns <tt>true</tt> if this map maps one or more keys to the |
|
668 |
* specified value. |
|
669 |
* |
|
670 |
* @param value value whose presence in this map is to be tested |
|
671 |
* @return <tt>true</tt> if this map maps one or more keys to the |
|
672 |
* specified value |
|
673 |
*/ |
|
674 |
public boolean containsValue(Object value) { |
|
675 |
if (value==null) |
|
676 |
return containsNullValue(); |
|
677 |
||
678 |
Entry<K,V>[] tab = getTable(); |
|
679 |
for (int i = tab.length; i-- > 0;) |
|
680 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
|
681 |
if (value.equals(e.value)) |
|
682 |
return true; |
|
683 |
return false; |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Special-case code for containsValue with null argument |
|
688 |
*/ |
|
689 |
private boolean containsNullValue() { |
|
690 |
Entry<K,V>[] tab = getTable(); |
|
691 |
for (int i = tab.length; i-- > 0;) |
|
692 |
for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
|
693 |
if (e.value==null) |
|
694 |
return true; |
|
695 |
return false; |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* The entries in this hash table extend WeakReference, using its main ref |
|
700 |
* field as the key. |
|
701 |
*/ |
|
702 |
private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { |
|
703 |
V value; |
|
704 |
final int hash; |
|
705 |
Entry<K,V> next; |
|
706 |
||
707 |
/** |
|
708 |
* Creates new entry. |
|
709 |
*/ |
|
710 |
Entry(Object key, V value, |
|
711 |
ReferenceQueue<Object> queue, |
|
712 |
int hash, Entry<K,V> next) { |
|
713 |
super(key, queue); |
|
714 |
this.value = value; |
|
715 |
this.hash = hash; |
|
716 |
this.next = next; |
|
717 |
} |
|
718 |
||
719 |
@SuppressWarnings("unchecked") |
|
720 |
public K getKey() { |
|
721 |
return (K) WeakHashMap.unmaskNull(get()); |
|
722 |
} |
|
723 |
||
724 |
public V getValue() { |
|
725 |
return value; |
|
726 |
} |
|
727 |
||
728 |
public V setValue(V newValue) { |
|
729 |
V oldValue = value; |
|
730 |
value = newValue; |
|
731 |
return oldValue; |
|
732 |
} |
|
733 |
||
734 |
public boolean equals(Object o) { |
|
735 |
if (!(o instanceof Map.Entry)) |
|
736 |
return false; |
|
737 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
|
738 |
K k1 = getKey(); |
|
739 |
Object k2 = e.getKey(); |
|
740 |
if (k1 == k2 || (k1 != null && k1.equals(k2))) { |
|
741 |
V v1 = getValue(); |
|
742 |
Object v2 = e.getValue(); |
|
743 |
if (v1 == v2 || (v1 != null && v1.equals(v2))) |
|
744 |
return true; |
|
745 |
} |
|
746 |
return false; |
|
747 |
} |
|
748 |
||
749 |
public int hashCode() { |
|
750 |
K k = getKey(); |
|
751 |
V v = getValue(); |
|
19853
832b09e2714c
8024009: Remove jdk.map.useRandomSeed system property
bchristi
parents:
19208
diff
changeset
|
752 |
return Objects.hashCode(k) ^ Objects.hashCode(v); |
2 | 753 |
} |
754 |
||
755 |
public String toString() { |
|
756 |
return getKey() + "=" + getValue(); |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
private abstract class HashIterator<T> implements Iterator<T> { |
|
761 |
private int index; |
|
23746 | 762 |
private Entry<K,V> entry; |
763 |
private Entry<K,V> lastReturned; |
|
2 | 764 |
private int expectedModCount = modCount; |
765 |
||
766 |
/** |
|
767 |
* Strong reference needed to avoid disappearance of key |
|
768 |
* between hasNext and next |
|
769 |
*/ |
|
23746 | 770 |
private Object nextKey; |
2 | 771 |
|
772 |
/** |
|
773 |
* Strong reference needed to avoid disappearance of key |
|
774 |
* between nextEntry() and any use of the entry |
|
775 |
*/ |
|
23746 | 776 |
private Object currentKey; |
2 | 777 |
|
778 |
HashIterator() { |
|
779 |
index = isEmpty() ? 0 : table.length; |
|
780 |
} |
|
781 |
||
782 |
public boolean hasNext() { |
|
783 |
Entry<K,V>[] t = table; |
|
784 |
||
785 |
while (nextKey == null) { |
|
786 |
Entry<K,V> e = entry; |
|
787 |
int i = index; |
|
788 |
while (e == null && i > 0) |
|
789 |
e = t[--i]; |
|
790 |
entry = e; |
|
791 |
index = i; |
|
792 |
if (e == null) { |
|
793 |
currentKey = null; |
|
794 |
return false; |
|
795 |
} |
|
796 |
nextKey = e.get(); // hold on to key in strong ref |
|
797 |
if (nextKey == null) |
|
798 |
entry = entry.next; |
|
799 |
} |
|
800 |
return true; |
|
801 |
} |
|
802 |
||
803 |
/** The common parts of next() across different types of iterators */ |
|
804 |
protected Entry<K,V> nextEntry() { |
|
805 |
if (modCount != expectedModCount) |
|
806 |
throw new ConcurrentModificationException(); |
|
807 |
if (nextKey == null && !hasNext()) |
|
808 |
throw new NoSuchElementException(); |
|
809 |
||
810 |
lastReturned = entry; |
|
811 |
entry = entry.next; |
|
812 |
currentKey = nextKey; |
|
813 |
nextKey = null; |
|
814 |
return lastReturned; |
|
815 |
} |
|
816 |
||
817 |
public void remove() { |
|
818 |
if (lastReturned == null) |
|
819 |
throw new IllegalStateException(); |
|
820 |
if (modCount != expectedModCount) |
|
821 |
throw new ConcurrentModificationException(); |
|
822 |
||
823 |
WeakHashMap.this.remove(currentKey); |
|
824 |
expectedModCount = modCount; |
|
825 |
lastReturned = null; |
|
826 |
currentKey = null; |
|
827 |
} |
|
828 |
||
829 |
} |
|
830 |
||
831 |
private class ValueIterator extends HashIterator<V> { |
|
832 |
public V next() { |
|
833 |
return nextEntry().value; |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
private class KeyIterator extends HashIterator<K> { |
|
838 |
public K next() { |
|
839 |
return nextEntry().getKey(); |
|
840 |
} |
|
841 |
} |
|
842 |
||
843 |
private class EntryIterator extends HashIterator<Map.Entry<K,V>> { |
|
844 |
public Map.Entry<K,V> next() { |
|
845 |
return nextEntry(); |
|
846 |
} |
|
847 |
} |
|
848 |
||
849 |
// Views |
|
850 |
||
23746 | 851 |
private transient Set<Map.Entry<K,V>> entrySet; |
2 | 852 |
|
853 |
/** |
|
854 |
* Returns a {@link Set} view of the keys contained in this map. |
|
855 |
* The set is backed by the map, so changes to the map are |
|
856 |
* reflected in the set, and vice-versa. If the map is modified |
|
857 |
* while an iteration over the set is in progress (except through |
|
858 |
* the iterator's own <tt>remove</tt> operation), the results of |
|
859 |
* the iteration are undefined. The set supports element removal, |
|
860 |
* which removes the corresponding mapping from the map, via the |
|
861 |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
|
862 |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
|
863 |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
|
864 |
* operations. |
|
865 |
*/ |
|
866 |
public Set<K> keySet() { |
|
867 |
Set<K> ks = keySet; |
|
868 |
return (ks != null ? ks : (keySet = new KeySet())); |
|
869 |
} |
|
870 |
||
871 |
private class KeySet extends AbstractSet<K> { |
|
872 |
public Iterator<K> iterator() { |
|
873 |
return new KeyIterator(); |
|
874 |
} |
|
875 |
||
876 |
public int size() { |
|
877 |
return WeakHashMap.this.size(); |
|
878 |
} |
|
879 |
||
880 |
public boolean contains(Object o) { |
|
881 |
return containsKey(o); |
|
882 |
} |
|
883 |
||
884 |
public boolean remove(Object o) { |
|
885 |
if (containsKey(o)) { |
|
886 |
WeakHashMap.this.remove(o); |
|
887 |
return true; |
|
888 |
} |
|
889 |
else |
|
890 |
return false; |
|
891 |
} |
|
892 |
||
893 |
public void clear() { |
|
894 |
WeakHashMap.this.clear(); |
|
895 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
896 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
897 |
public Spliterator<K> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
898 |
return new KeySpliterator<>(WeakHashMap.this, 0, -1, 0, 0); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
899 |
} |
2 | 900 |
} |
901 |
||
902 |
/** |
|
903 |
* Returns a {@link Collection} view of the values contained in this map. |
|
904 |
* The collection is backed by the map, so changes to the map are |
|
905 |
* reflected in the collection, and vice-versa. If the map is |
|
906 |
* modified while an iteration over the collection is in progress |
|
907 |
* (except through the iterator's own <tt>remove</tt> operation), |
|
908 |
* the results of the iteration are undefined. The collection |
|
909 |
* supports element removal, which removes the corresponding |
|
910 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
911 |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
|
912 |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
|
913 |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
|
914 |
*/ |
|
915 |
public Collection<V> values() { |
|
916 |
Collection<V> vs = values; |
|
917 |
return (vs != null) ? vs : (values = new Values()); |
|
918 |
} |
|
919 |
||
920 |
private class Values extends AbstractCollection<V> { |
|
921 |
public Iterator<V> iterator() { |
|
922 |
return new ValueIterator(); |
|
923 |
} |
|
924 |
||
925 |
public int size() { |
|
926 |
return WeakHashMap.this.size(); |
|
927 |
} |
|
928 |
||
929 |
public boolean contains(Object o) { |
|
930 |
return containsValue(o); |
|
931 |
} |
|
932 |
||
933 |
public void clear() { |
|
934 |
WeakHashMap.this.clear(); |
|
935 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
936 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
937 |
public Spliterator<V> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
938 |
return new ValueSpliterator<>(WeakHashMap.this, 0, -1, 0, 0); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
939 |
} |
2 | 940 |
} |
941 |
||
942 |
/** |
|
943 |
* Returns a {@link Set} view of the mappings contained in this map. |
|
944 |
* The set is backed by the map, so changes to the map are |
|
945 |
* reflected in the set, and vice-versa. If the map is modified |
|
946 |
* while an iteration over the set is in progress (except through |
|
947 |
* the iterator's own <tt>remove</tt> operation, or through the |
|
948 |
* <tt>setValue</tt> operation on a map entry returned by the |
|
949 |
* iterator) the results of the iteration are undefined. The set |
|
950 |
* supports element removal, which removes the corresponding |
|
951 |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
|
952 |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and |
|
953 |
* <tt>clear</tt> operations. It does not support the |
|
954 |
* <tt>add</tt> or <tt>addAll</tt> operations. |
|
955 |
*/ |
|
956 |
public Set<Map.Entry<K,V>> entrySet() { |
|
957 |
Set<Map.Entry<K,V>> es = entrySet; |
|
958 |
return es != null ? es : (entrySet = new EntrySet()); |
|
959 |
} |
|
960 |
||
961 |
private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
|
962 |
public Iterator<Map.Entry<K,V>> iterator() { |
|
963 |
return new EntryIterator(); |
|
964 |
} |
|
965 |
||
966 |
public boolean contains(Object o) { |
|
967 |
if (!(o instanceof Map.Entry)) |
|
968 |
return false; |
|
969 |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
|
970 |
Entry<K,V> candidate = getEntry(e.getKey()); |
|
971 |
return candidate != null && candidate.equals(e); |
|
972 |
} |
|
973 |
||
974 |
public boolean remove(Object o) { |
|
975 |
return removeMapping(o); |
|
976 |
} |
|
977 |
||
978 |
public int size() { |
|
979 |
return WeakHashMap.this.size(); |
|
980 |
} |
|
981 |
||
982 |
public void clear() { |
|
983 |
WeakHashMap.this.clear(); |
|
984 |
} |
|
985 |
||
986 |
private List<Map.Entry<K,V>> deepCopy() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
987 |
List<Map.Entry<K,V>> list = new ArrayList<>(size()); |
2 | 988 |
for (Map.Entry<K,V> e : this) |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
989 |
list.add(new AbstractMap.SimpleEntry<>(e)); |
2 | 990 |
return list; |
991 |
} |
|
992 |
||
993 |
public Object[] toArray() { |
|
994 |
return deepCopy().toArray(); |
|
995 |
} |
|
996 |
||
997 |
public <T> T[] toArray(T[] a) { |
|
998 |
return deepCopy().toArray(a); |
|
999 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1000 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1001 |
public Spliterator<Map.Entry<K,V>> spliterator() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1002 |
return new EntrySpliterator<>(WeakHashMap.this, 0, -1, 0, 0); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1003 |
} |
2 | 1004 |
} |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1005 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18280
diff
changeset
|
1006 |
@SuppressWarnings("unchecked") |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1007 |
@Override |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1008 |
public void forEach(BiConsumer<? super K, ? super V> action) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1009 |
Objects.requireNonNull(action); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1010 |
int expectedModCount = modCount; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1011 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1012 |
Entry<K, V>[] tab = getTable(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1013 |
for (Entry<K, V> entry : tab) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1014 |
while (entry != null) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1015 |
Object key = entry.get(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1016 |
if (key != null) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1017 |
action.accept((K)WeakHashMap.unmaskNull(key), entry.value); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1018 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1019 |
entry = entry.next; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1020 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1021 |
if (expectedModCount != modCount) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1022 |
throw new ConcurrentModificationException(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1023 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1024 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1025 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1026 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1027 |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18280
diff
changeset
|
1028 |
@SuppressWarnings("unchecked") |
18280
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1029 |
@Override |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1030 |
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1031 |
Objects.requireNonNull(function); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1032 |
int expectedModCount = modCount; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1033 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1034 |
Entry<K, V>[] tab = getTable();; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1035 |
for (Entry<K, V> entry : tab) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1036 |
while (entry != null) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1037 |
Object key = entry.get(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1038 |
if (key != null) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1039 |
entry.value = function.apply((K)WeakHashMap.unmaskNull(key), entry.value); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1040 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1041 |
entry = entry.next; |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1042 |
|
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1043 |
if (expectedModCount != modCount) { |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1044 |
throw new ConcurrentModificationException(); |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1045 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1046 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1047 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1048 |
} |
6c3c0ff49eb5
8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents:
18166
diff
changeset
|
1049 |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1050 |
/** |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1051 |
* Similar form as other hash Spliterators, but skips dead |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1052 |
* elements. |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1053 |
*/ |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1054 |
static class WeakHashMapSpliterator<K,V> { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1055 |
final WeakHashMap<K,V> map; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1056 |
WeakHashMap.Entry<K,V> current; // current node |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1057 |
int index; // current index, modified on advance/split |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1058 |
int fence; // -1 until first use; then one past last index |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1059 |
int est; // size estimate |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1060 |
int expectedModCount; // for comodification checks |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1061 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1062 |
WeakHashMapSpliterator(WeakHashMap<K,V> m, int origin, |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1063 |
int fence, int est, |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1064 |
int expectedModCount) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1065 |
this.map = m; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1066 |
this.index = origin; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1067 |
this.fence = fence; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1068 |
this.est = est; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1069 |
this.expectedModCount = expectedModCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1070 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1071 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1072 |
final int getFence() { // initialize fence and size on first use |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1073 |
int hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1074 |
if ((hi = fence) < 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1075 |
WeakHashMap<K,V> m = map; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1076 |
est = m.size(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1077 |
expectedModCount = m.modCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1078 |
hi = fence = m.table.length; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1079 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1080 |
return hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1081 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1082 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1083 |
public final long estimateSize() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1084 |
getFence(); // force init |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1085 |
return (long) est; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1086 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1087 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1088 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1089 |
static final class KeySpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1090 |
extends WeakHashMapSpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1091 |
implements Spliterator<K> { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1092 |
KeySpliterator(WeakHashMap<K,V> m, int origin, int fence, int est, |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1093 |
int expectedModCount) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1094 |
super(m, origin, fence, est, expectedModCount); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1095 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1096 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1097 |
public KeySpliterator<K,V> trySplit() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1098 |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1099 |
return (lo >= mid) ? null : |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1100 |
new KeySpliterator<>(map, lo, index = mid, est >>>= 1, |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1101 |
expectedModCount); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1102 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1103 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1104 |
public void forEachRemaining(Consumer<? super K> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1105 |
int i, hi, mc; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1106 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1107 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1108 |
WeakHashMap<K,V> m = map; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1109 |
WeakHashMap.Entry<K,V>[] tab = m.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1110 |
if ((hi = fence) < 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1111 |
mc = expectedModCount = m.modCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1112 |
hi = fence = tab.length; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1113 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1114 |
else |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1115 |
mc = expectedModCount; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1116 |
if (tab.length >= hi && (i = index) >= 0 && |
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1117 |
(i < (index = hi) || current != null)) { |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1118 |
WeakHashMap.Entry<K,V> p = current; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1119 |
current = null; // exhaust |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1120 |
do { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1121 |
if (p == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1122 |
p = tab[i++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1123 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1124 |
Object x = p.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1125 |
p = p.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1126 |
if (x != null) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1127 |
@SuppressWarnings("unchecked") K k = |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1128 |
(K) WeakHashMap.unmaskNull(x); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1129 |
action.accept(k); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1130 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1131 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1132 |
} while (p != null || i < hi); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1133 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1134 |
if (m.modCount != mc) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1135 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1136 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1137 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1138 |
public boolean tryAdvance(Consumer<? super K> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1139 |
int hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1140 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1141 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1142 |
WeakHashMap.Entry<K,V>[] tab = map.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1143 |
if (tab.length >= (hi = getFence()) && index >= 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1144 |
while (current != null || index < hi) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1145 |
if (current == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1146 |
current = tab[index++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1147 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1148 |
Object x = current.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1149 |
current = current.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1150 |
if (x != null) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1151 |
@SuppressWarnings("unchecked") K k = |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1152 |
(K) WeakHashMap.unmaskNull(x); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1153 |
action.accept(k); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1154 |
if (map.modCount != expectedModCount) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1155 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1156 |
return true; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1157 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1158 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1159 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1160 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1161 |
return false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1162 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1163 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1164 |
public int characteristics() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1165 |
return Spliterator.DISTINCT; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1166 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1167 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1168 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1169 |
static final class ValueSpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1170 |
extends WeakHashMapSpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1171 |
implements Spliterator<V> { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1172 |
ValueSpliterator(WeakHashMap<K,V> m, int origin, int fence, int est, |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1173 |
int expectedModCount) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1174 |
super(m, origin, fence, est, expectedModCount); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1175 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1176 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1177 |
public ValueSpliterator<K,V> trySplit() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1178 |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1179 |
return (lo >= mid) ? null : |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1180 |
new ValueSpliterator<>(map, lo, index = mid, est >>>= 1, |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1181 |
expectedModCount); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1182 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1183 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1184 |
public void forEachRemaining(Consumer<? super V> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1185 |
int i, hi, mc; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1186 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1187 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1188 |
WeakHashMap<K,V> m = map; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1189 |
WeakHashMap.Entry<K,V>[] tab = m.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1190 |
if ((hi = fence) < 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1191 |
mc = expectedModCount = m.modCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1192 |
hi = fence = tab.length; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1193 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1194 |
else |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1195 |
mc = expectedModCount; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1196 |
if (tab.length >= hi && (i = index) >= 0 && |
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1197 |
(i < (index = hi) || current != null)) { |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1198 |
WeakHashMap.Entry<K,V> p = current; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1199 |
current = null; // exhaust |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1200 |
do { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1201 |
if (p == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1202 |
p = tab[i++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1203 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1204 |
Object x = p.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1205 |
V v = p.value; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1206 |
p = p.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1207 |
if (x != null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1208 |
action.accept(v); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1209 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1210 |
} while (p != null || i < hi); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1211 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1212 |
if (m.modCount != mc) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1213 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1214 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1215 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1216 |
public boolean tryAdvance(Consumer<? super V> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1217 |
int hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1218 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1219 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1220 |
WeakHashMap.Entry<K,V>[] tab = map.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1221 |
if (tab.length >= (hi = getFence()) && index >= 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1222 |
while (current != null || index < hi) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1223 |
if (current == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1224 |
current = tab[index++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1225 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1226 |
Object x = current.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1227 |
V v = current.value; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1228 |
current = current.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1229 |
if (x != null) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1230 |
action.accept(v); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1231 |
if (map.modCount != expectedModCount) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1232 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1233 |
return true; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1234 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1235 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1236 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1237 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1238 |
return false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1239 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1240 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1241 |
public int characteristics() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1242 |
return 0; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1243 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1244 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1245 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1246 |
static final class EntrySpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1247 |
extends WeakHashMapSpliterator<K,V> |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1248 |
implements Spliterator<Map.Entry<K,V>> { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1249 |
EntrySpliterator(WeakHashMap<K,V> m, int origin, int fence, int est, |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1250 |
int expectedModCount) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1251 |
super(m, origin, fence, est, expectedModCount); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1252 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1253 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1254 |
public EntrySpliterator<K,V> trySplit() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1255 |
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1256 |
return (lo >= mid) ? null : |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1257 |
new EntrySpliterator<>(map, lo, index = mid, est >>>= 1, |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1258 |
expectedModCount); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1259 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1260 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1261 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1262 |
public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1263 |
int i, hi, mc; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1264 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1265 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1266 |
WeakHashMap<K,V> m = map; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1267 |
WeakHashMap.Entry<K,V>[] tab = m.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1268 |
if ((hi = fence) < 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1269 |
mc = expectedModCount = m.modCount; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1270 |
hi = fence = tab.length; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1271 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1272 |
else |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1273 |
mc = expectedModCount; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1274 |
if (tab.length >= hi && (i = index) >= 0 && |
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1275 |
(i < (index = hi) || current != null)) { |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1276 |
WeakHashMap.Entry<K,V> p = current; |
17949
6c33d8f2601e
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents:
17939
diff
changeset
|
1277 |
current = null; // exhaust |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1278 |
do { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1279 |
if (p == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1280 |
p = tab[i++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1281 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1282 |
Object x = p.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1283 |
V v = p.value; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1284 |
p = p.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1285 |
if (x != null) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1286 |
@SuppressWarnings("unchecked") K k = |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1287 |
(K) WeakHashMap.unmaskNull(x); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1288 |
action.accept |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1289 |
(new AbstractMap.SimpleImmutableEntry<>(k, v)); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1290 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1291 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1292 |
} while (p != null || i < hi); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1293 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1294 |
if (m.modCount != mc) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1295 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1296 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1297 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1298 |
public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1299 |
int hi; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1300 |
if (action == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1301 |
throw new NullPointerException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1302 |
WeakHashMap.Entry<K,V>[] tab = map.table; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1303 |
if (tab.length >= (hi = getFence()) && index >= 0) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1304 |
while (current != null || index < hi) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1305 |
if (current == null) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1306 |
current = tab[index++]; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1307 |
else { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1308 |
Object x = current.get(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1309 |
V v = current.value; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1310 |
current = current.next; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1311 |
if (x != null) { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1312 |
@SuppressWarnings("unchecked") K k = |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1313 |
(K) WeakHashMap.unmaskNull(x); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1314 |
action.accept |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
19853
diff
changeset
|
1315 |
(new AbstractMap.SimpleImmutableEntry<>(k, v)); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1316 |
if (map.modCount != expectedModCount) |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1317 |
throw new ConcurrentModificationException(); |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1318 |
return true; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1319 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1320 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1321 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1322 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1323 |
return false; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1324 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1325 |
|
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1326 |
public int characteristics() { |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1327 |
return Spliterator.DISTINCT; |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1328 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1329 |
} |
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
1330 |
|
2 | 1331 |
} |