1 | static final class WeakHashMap2<K,V> extends AbstractMap<K,V> implements Map<K,V> { |
2 | |
3 | /** |
4 | * The default initial capacity -- MUST be a power of two. |
5 | */ |
6 | private static final int DEFAULT_INITIAL_CAPACITY = 16; |
7 | |
8 | /** |
9 | * The maximum capacity, used if a higher value is implicitly specified |
10 | * by either of the constructors with arguments. |
11 | * MUST be a power of two <= 1<<30. |
12 | */ |
13 | private static final int MAXIMUM_CAPACITY = 1 << 30; |
14 | |
15 | /** |
16 | * The load factor used when none specified in constructor. |
17 | */ |
18 | private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
19 | |
20 | /** |
21 | * The table, resized as necessary. Length MUST Always be a power of two. |
22 | */ |
23 | Entry<K,V>[] table; |
24 | |
25 | /** |
26 | * The number of key-value mappings contained in this weak hash map. |
27 | */ |
28 | private int size; |
29 | |
30 | /** |
31 | * The next size value at which to resize (capacity * load factor). |
32 | */ |
33 | private int threshold; |
34 | |
35 | /** |
36 | * The load factor for the hash table. |
37 | */ |
38 | private final float loadFactor; |
39 | |
40 | /** |
41 | * Reference queue for cleared WeakEntries |
42 | */ |
43 | private final ReferenceQueue<Object> queue = new ReferenceQueue(); |
44 | |
45 | int modCount; |
46 | |
47 | @SuppressWarnings("unchecked") |
48 | Entry<K,V>[] newTable(int n) { |
49 | return (Entry<K,V>[]) new Entry<?,?>[n]; |
50 | } |
51 | |
52 | *(int initialCapacity, float loadFactor) { |
53 | if (initialCapacity < 0) |
54 | throw new IllegalArgumentException("Illegal Initial Capacity: "+ |
55 | initialCapacity); |
56 | if (initialCapacity > MAXIMUM_CAPACITY) |
57 | initialCapacity = MAXIMUM_CAPACITY; |
58 | |
59 | if (loadFactor <= 0 || Float.isNaN(loadFactor)) |
60 | throw new IllegalArgumentException("Illegal Load factor: "+ |
61 | loadFactor); |
62 | int capacity = 1; |
63 | while (capacity < initialCapacity) |
64 | capacity <<= 1; |
65 | table = newTable(capacity); |
66 | this.loadFactor = loadFactor; |
67 | threshold = (int)(capacity * loadFactor); |
68 | } |
69 | |
70 | /** |
71 | * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial |
72 | * capacity and the default load factor (0.75). |
73 | * |
74 | * @param initialCapacity The initial capacity of the <tt>WeakHashMap</tt> |
75 | * @throws IllegalArgumentException if the initial capacity is negative |
76 | */ |
77 | *(int initialCapacity) { |
78 | this(initialCapacity, DEFAULT_LOAD_FACTOR); |
79 | } |
80 | |
81 | /** |
82 | * Constructs a new, empty <tt>WeakHashMap</tt> with the default initial |
83 | * capacity (16) and load factor (0.75). |
84 | */ |
85 | *() { |
86 | this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
87 | } |
88 | |
89 | /** |
90 | * Constructs a new <tt>WeakHashMap</tt> with the same mappings as the |
91 | * specified map. The <tt>WeakHashMap</tt> is created with the default |
92 | * load factor (0.75) and an initial capacity sufficient to hold the |
93 | * mappings in the specified map. |
94 | * |
95 | * @param m the map whose mappings are to be placed in this map |
96 | * @throws NullPointerException if the specified map is null |
97 | * @since 1.3 |
98 | */ |
99 | *(Map<? extends K, ? extends V> m) { |
100 | this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, |
101 | DEFAULT_INITIAL_CAPACITY), |
102 | DEFAULT_LOAD_FACTOR); |
103 | putAll(m); |
104 | } |
105 | |
106 | // internal utilities |
107 | |
108 | /** |
109 | * Value representing null keys inside tables. |
110 | */ |
111 | private static final Object NULL_KEY = new Object(); |
112 | |
113 | /** |
114 | * Use NULL_KEY for key if it is null. |
115 | */ |
116 | private static Object maskNull(Object key) { |
117 | return (key == null) ? NULL_KEY : key; |
118 | } |
119 | |
120 | /** |
121 | * Returns internal representation of null key back to caller as null. |
122 | */ |
123 | static Object unmaskNull(Object key) { |
124 | return (key == NULL_KEY) ? null : key; |
125 | } |
126 | |
127 | /** |
128 | * Retrieve object hash code and applies a supplemental hash function to the |
129 | * result hash, which defends against poor quality hash functions. This is |
130 | * critical because HashMap uses power-of-two length hash tables, that |
131 | * otherwise encounter collisions for hashCodes that do not differ |
132 | * in lower bits. |
133 | */ |
134 | final int hash(Object k) { |
135 | int h = keyHashCode(k); |
136 | |
137 | // This function ensures that hashCodes that differ only by |
138 | // constant multiples at each bit position have a bounded |
139 | // number of collisions (approximately 8 at default load factor). |
140 | h ^= (h >>> 20) ^ (h >>> 12); |
141 | return h ^ (h >>> 7) ^ (h >>> 4); |
142 | } |
143 | |
144 | /** |
145 | * Returns index for hash code h. |
146 | */ |
147 | private static int indexFor(int h, int length) { |
148 | return h & (length-1); |
149 | } |
150 | |
151 | /** |
152 | * Expunges stale entries from the table. |
153 | */ |
154 | private void expungeStaleEntries() { |
155 | for (Object x; (x = queue.poll()) != null; ) { |
156 | synchronized (queue) { |
157 | @SuppressWarnings("unchecked") |
158 | Entry<K,V> e = (Entry<K,V>) x; |
159 | int i = indexFor(e.hash, table.length); |
160 | |
161 | Entry<K,V> prev = table[i]; |
162 | Entry<K,V> p = prev; |
163 | while (p != null) { |
164 | Entry<K,V> next = p.next; |
165 | if (p == e) { |
166 | if (prev == e) |
167 | table[i] = next; |
168 | else |
169 | prev.next = next; |
170 | // Must not null out e.next; |
171 | // stale entries may be in use by a HashIterator |
172 | e.value = null; // Help GC |
173 | size--; |
174 | break; |
175 | } |
176 | prev = p; |
177 | p = next; |
178 | } |
179 | } |
180 | } |
181 | } |
182 | |
183 | /** |
184 | * Returns the table after first expunging stale entries. |
185 | */ |
186 | private Entry<K,V>[] getTable() { |
187 | expungeStaleEntries(); |
188 | return table; |
189 | } |
190 | |
191 | /** |
192 | * Returns the number of key-value mappings in this map. |
193 | * This result is a snapshot, and may not reflect unprocessed |
194 | * entries that will be removed before next attempted access |
195 | * because they are no longer referenced. |
196 | */ |
197 | public int size() { |
198 | if (size == 0) |
199 | return 0; |
200 | expungeStaleEntries(); |
201 | return size; |
202 | } |
203 | |
204 | /** |
205 | * Returns <tt>true</tt> if this map contains no key-value mappings. |
206 | * This result is a snapshot, and may not reflect unprocessed |
207 | * entries that will be removed before next attempted access |
208 | * because they are no longer referenced. |
209 | */ |
210 | public boolean isEmpty() { |
211 | return size() == 0; |
212 | } |
213 | |
214 | /** |
215 | * Returns the value to which the specified key is mapped, |
216 | * or {@code null} if this map contains no mapping for the key. |
217 | * |
218 | * <p>More formally, if this map contains a mapping from a key |
219 | * {@code k} to a value {@code v} such that {@code (key==null ? k==null : |
220 | * key.equals(k))}, then this method returns {@code v}; otherwise |
221 | * it returns {@code null}. (There can be at most one such mapping.) |
222 | * |
223 | * <p>A return value of {@code null} does not <i>necessarily</i> |
224 | * indicate that the map contains no mapping for the key; it's also |
225 | * possible that the map explicitly maps the key to {@code null}. |
226 | * The {@link #containsKey containsKey} operation may be used to |
227 | * distinguish these two cases. |
228 | * |
229 | * @see #put(Object, Object) |
230 | */ |
231 | public V get(Object key) { |
232 | Object k = maskNull(key); |
233 | int h = hash(k); |
234 | Entry<K,V>[] tab = getTable(); |
235 | int index = indexFor(h, tab.length); |
236 | Entry<K,V> e = tab[index]; |
237 | while (e != null) { |
238 | if (e.hash == h && eq(k, e.get())) |
239 | return e.value; |
240 | e = e.next; |
241 | } |
242 | return null; |
243 | } |
244 | |
245 | /** |
246 | * Returns <tt>true</tt> if this map contains a mapping for the |
247 | * specified key. |
248 | * |
249 | * @param key The key whose presence in this map is to be tested |
250 | * @return <tt>true</tt> if there is a mapping for <tt>key</tt>; |
251 | * <tt>false</tt> otherwise |
252 | */ |
253 | public boolean containsKey(Object key) { |
254 | return getEntry(key) != null; |
255 | } |
256 | |
257 | /** |
258 | * Returns the entry associated with the specified key in this map. |
259 | * Returns null if the map contains no mapping for this key. |
260 | */ |
261 | Entry<K,V> getEntry(Object key) { |
262 | Object k = maskNull(key); |
263 | int h = hash(k); |
264 | Entry<K,V>[] tab = getTable(); |
265 | int index = indexFor(h, tab.length); |
266 | Entry<K,V> e = tab[index]; |
267 | while (e != null && !(e.hash == h && eq(k, e.get()))) |
268 | e = e.next; |
269 | return e; |
270 | } |
271 | |
272 | /** |
273 | * Associates the specified value with the specified key in this map. |
274 | * If the map previously contained a mapping for this key, the old |
275 | * value is replaced. |
276 | * |
277 | * @param key key with which the specified value is to be associated. |
278 | * @param value value to be associated with the specified key. |
279 | * @return the previous value associated with <tt>key</tt>, or |
280 | * <tt>null</tt> if there was no mapping for <tt>key</tt>. |
281 | * (A <tt>null</tt> return can also indicate that the map |
282 | * previously associated <tt>null</tt> with <tt>key</tt>.) |
283 | */ |
284 | public V put(K key, V value) { |
285 | Object k = maskNull(key); |
286 | int h = hash(k); |
287 | Entry<K,V>[] tab = getTable(); |
288 | int i = indexFor(h, tab.length); |
289 | |
290 | for (Entry<K,V> e = tab[i]; e != null; e = e.next) { |
291 | if (h == e.hash && eq(k, e.get())) { |
292 | V oldValue = e.value; |
293 | if (value != oldValue) |
294 | e.value = value; |
295 | return oldValue; |
296 | } |
297 | } |
298 | |
299 | modCount++; |
300 | Entry<K,V> e = tab[i]; |
301 | tab[i] = new Entry<K,V>(k, value, queue, h, e); |
302 | if (++size >= threshold) |
303 | resize(tab.length * 2); |
304 | return null; |
305 | } |
306 | |
307 | /** |
308 | * Rehashes the contents of this map into a new array with a |
309 | * larger capacity. This method is called automatically when the |
310 | * number of keys in this map reaches its threshold. |
311 | * |
312 | * If current capacity is MAXIMUM_CAPACITY, this method does not |
313 | * resize the map, but sets threshold to Integer.MAX_VALUE. |
314 | * This has the effect of preventing future calls. |
315 | * |
316 | * @param newCapacity the new capacity, MUST be a power of two; |
317 | * must be greater than current capacity unless current |
318 | * capacity is MAXIMUM_CAPACITY (in which case value |
319 | * is irrelevant). |
320 | */ |
321 | void resize(int newCapacity) { |
322 | Entry<K,V>[] oldTable = getTable(); |
323 | int oldCapacity = oldTable.length; |
324 | if (oldCapacity == MAXIMUM_CAPACITY) { |
325 | threshold = Integer.MAX_VALUE; |
326 | return; |
327 | } |
328 | |
329 | Entry<K,V>[] newTable = newTable(newCapacity); |
330 | transfer(oldTable, newTable); |
331 | table = newTable; |
332 | |
333 | /* |
334 | * If ignoring null elements and processing ref queue caused massive |
335 | * shrinkage, then restore old table. This should be rare, but avoids |
336 | * unbounded expansion of garbage-filled tables. |
337 | */ |
338 | if (size >= threshold / 2) { |
339 | threshold = (int)(newCapacity * loadFactor); |
340 | } else { |
341 | expungeStaleEntries(); |
342 | transfer(newTable, oldTable); |
343 | table = oldTable; |
344 | } |
345 | } |
346 | |
347 | /** Transfers all entries from src to dest tables */ |
348 | private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) { |
349 | for (int j = 0; j < src.length; ++j) { |
350 | Entry<K,V> e = src[j]; |
351 | src[j] = null; |
352 | while (e != null) { |
353 | Entry<K,V> next = e.next; |
354 | Object key = e.get(); |
355 | if (key == null) { |
356 | e.next = null; // Help GC |
357 | e.value = null; // " " |
358 | size--; |
359 | } else { |
360 | int i = indexFor(e.hash, dest.length); |
361 | e.next = dest[i]; |
362 | dest[i] = e; |
363 | } |
364 | e = next; |
365 | } |
366 | } |
367 | } |
368 | |
369 | /** |
370 | * Copies all of the mappings from the specified map to this map. |
371 | * These mappings will replace any mappings that this map had for any |
372 | * of the keys currently in the specified map. |
373 | * |
374 | * @param m mappings to be stored in this map. |
375 | * @throws NullPointerException if the specified map is null. |
376 | */ |
377 | public void putAll(Map<? extends K, ? extends V> m) { |
378 | int numKeysToBeAdded = m.size(); |
379 | if (numKeysToBeAdded == 0) |
380 | return; |
381 | |
382 | /* |
383 | * Expand the map if the map if the number of mappings to be added |
384 | * is greater than or equal to threshold. This is conservative; the |
385 | * obvious condition is (m.size() + size) >= threshold, but this |
386 | * condition could result in a map with twice the appropriate capacity, |
387 | * if the keys to be added overlap with the keys already in this map. |
388 | * By using the conservative calculation, we subject ourself |
389 | * to at most one extra resize. |
390 | */ |
391 | if (numKeysToBeAdded > threshold) { |
392 | int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1); |
393 | if (targetCapacity > MAXIMUM_CAPACITY) |
394 | targetCapacity = MAXIMUM_CAPACITY; |
395 | int newCapacity = table.length; |
396 | while (newCapacity < targetCapacity) |
397 | newCapacity <<= 1; |
398 | if (newCapacity > table.length) |
399 | resize(newCapacity); |
400 | } |
401 | |
402 | for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
403 | put(e.getKey(), e.getValue()); |
404 | } |
405 | |
406 | /** |
407 | * Removes the mapping for a key from this weak hash map if it is present. |
408 | * More formally, if this map contains a mapping from key <tt>k</tt> to |
409 | * value <tt>v</tt> such that <code>(key==null ? k==null : |
410 | * key.equals(k))</code>, that mapping is removed. (The map can contain |
411 | * at most one such mapping.) |
412 | * |
413 | * <p>Returns the value to which this map previously associated the key, |
414 | * or <tt>null</tt> if the map contained no mapping for the key. A |
415 | * return value of <tt>null</tt> does not <i>necessarily</i> indicate |
416 | * that the map contained no mapping for the key; it's also possible |
417 | * that the map explicitly mapped the key to <tt>null</tt>. |
418 | * |
419 | * <p>The map will not contain a mapping for the specified key once the |
420 | * call returns. |
421 | * |
422 | * @param key key whose mapping is to be removed from the map |
423 | * @return the previous value associated with <tt>key</tt>, or |
424 | * <tt>null</tt> if there was no mapping for <tt>key</tt> |
425 | */ |
426 | public V remove(Object key) { |
427 | Object k = maskNull(key); |
428 | int h = hash(k); |
429 | Entry<K,V>[] tab = getTable(); |
430 | int i = indexFor(h, tab.length); |
431 | Entry<K,V> prev = tab[i]; |
432 | Entry<K,V> e = prev; |
433 | |
434 | while (e != null) { |
435 | Entry<K,V> next = e.next; |
436 | if (h == e.hash && eq(k, e.get())) { |
437 | modCount++; |
438 | size--; |
439 | if (prev == e) |
440 | tab[i] = next; |
441 | else |
442 | prev.next = next; |
443 | return e.value; |
444 | } |
445 | prev = e; |
446 | e = next; |
447 | } |
448 | |
449 | return null; |
450 | } |
451 | |
452 | /** Special version of remove needed by Entry set */ |
453 | boolean removeMapping(Object o) { |
454 | if (!(o instanceof Map.Entry)) |
455 | return false; |
456 | Entry<K,V>[] tab = getTable(); |
457 | Map.Entry<?,?> entry = (Map.Entry<?,?>)o; |
458 | Object k = maskNull(entry.getKey()); |
459 | int h = hash(k); |
460 | int i = indexFor(h, tab.length); |
461 | Entry<K,V> prev = tab[i]; |
462 | Entry<K,V> e = prev; |
463 | |
464 | while (e != null) { |
465 | Entry<K,V> next = e.next; |
466 | if (h == e.hash && e.equals(entry)) { |
467 | modCount++; |
468 | size--; |
469 | if (prev == e) |
470 | tab[i] = next; |
471 | else |
472 | prev.next = next; |
473 | return true; |
474 | } |
475 | prev = e; |
476 | e = next; |
477 | } |
478 | |
479 | return false; |
480 | } |
481 | |
482 | /** |
483 | * Removes all of the mappings from this map. |
484 | * The map will be empty after this call returns. |
485 | */ |
486 | public void clear() { |
487 | // clear out ref queue. We don't need to expunge entries |
488 | // since table is getting cleared. |
489 | while (queue.poll() != null) |
490 | ; |
491 | |
492 | modCount++; |
493 | Arrays.fill(table, null); |
494 | size = 0; |
495 | |
496 | // Allocation of array may have caused GC, which may have caused |
497 | // additional entries to go stale. Removing these entries from the |
498 | // reference queue will make them eligible for reclamation. |
499 | while (queue.poll() != null) |
500 | ; |
501 | } |
502 | |
503 | /** |
504 | * Returns <tt>true</tt> if this map maps one or more keys to the |
505 | * specified value. |
506 | * |
507 | * @param value value whose presence in this map is to be tested |
508 | * @return <tt>true</tt> if this map maps one or more keys to the |
509 | * specified value |
510 | */ |
511 | public boolean containsValue(Object value) { |
512 | if (value==null) |
513 | return containsNullValue(); |
514 | |
515 | Entry<K,V>[] tab = getTable(); |
516 | for (int i = tab.length; i-- > 0;) |
517 | for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
518 | if (value.equals(e.value)) |
519 | return true; |
520 | return false; |
521 | } |
522 | |
523 | /** |
524 | * Special-case code for containsValue with null argument |
525 | */ |
526 | private boolean containsNullValue() { |
527 | Entry<K,V>[] tab = getTable(); |
528 | for (int i = tab.length; i-- > 0;) |
529 | for (Entry<K,V> e = tab[i]; e != null; e = e.next) |
530 | if (e.value==null) |
531 | return true; |
532 | return false; |
533 | } |
534 | |
535 | /** |
536 | * The entries in this hash table extend WeakReference, using its main ref |
537 | * field as the key. |
538 | Not sure about the semantics if keyHashCode/keyEquals are overridden. |
539 | */ |
540 | static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { |
541 | V value; |
542 | final int hash; |
543 | Entry<K,V> next; |
544 | |
545 | /** |
546 | * Creates new entry. |
547 | */ |
548 | Entry(Object key, V value, |
549 | ReferenceQueue<Object> queue, |
550 | int hash, Entry<K,V> next) { |
551 | super(key, queue); |
552 | this.value = value; |
553 | this.hash = hash; |
554 | this.next = next; |
555 | } |
556 | |
557 | @SuppressWarnings("unchecked") |
558 | public K getKey() { |
559 | return (K) WeakHashMap2.unmaskNull(get()); |
560 | } |
561 | |
562 | public V getValue() { |
563 | return value; |
564 | } |
565 | |
566 | public V setValue(V newValue) { |
567 | V oldValue = value; |
568 | value = newValue; |
569 | return oldValue; |
570 | } |
571 | |
572 | public boolean equals(Object o) { |
573 | if (!(o instanceof Map.Entry)) |
574 | return false; |
575 | Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
576 | K k1 = getKey(); |
577 | Object k2 = e.getKey(); |
578 | if (k1 == k2 || (k1 != null && k1.equals(k2))) { |
579 | V v1 = getValue(); |
580 | Object v2 = e.getValue(); |
581 | if (v1 == v2 || (v1 != null && v1.equals(v2))) |
582 | return true; |
583 | } |
584 | return false; |
585 | } |
586 | |
587 | public int hashCode() { |
588 | K k = getKey(); |
589 | V v = getValue(); |
590 | return Objects.hashCode(k) ^ Objects.hashCode(v); |
591 | } |
592 | |
593 | public String toString() { |
594 | return getKey() + "=" + getValue(); |
595 | } |
596 | } |
597 | |
598 | private abstract class HashIterator<T> implements Iterator<T> { |
599 | private int index; |
600 | private Entry<K,V> entry; |
601 | private Entry<K,V> lastReturned; |
602 | private int expectedModCount = modCount; |
603 | |
604 | /** |
605 | * Strong reference needed to avoid disappearance of key |
606 | * between hasNext and next |
607 | */ |
608 | private Object nextKey; |
609 | |
610 | /** |
611 | * Strong reference needed to avoid disappearance of key |
612 | * between nextEntry() and any use of the entry |
613 | */ |
614 | private Object currentKey; |
615 | |
616 | HashIterator() { |
617 | index = isEmpty() ? 0 : table.length; |
618 | } |
619 | |
620 | public boolean hasNext() { |
621 | Entry<K,V>[] t = table; |
622 | |
623 | while (nextKey == null) { |
624 | Entry<K,V> e = entry; |
625 | int i = index; |
626 | while (e == null && i > 0) |
627 | e = t[--i]; |
628 | entry = e; |
629 | index = i; |
630 | if (e == null) { |
631 | currentKey = null; |
632 | return false; |
633 | } |
634 | nextKey = e.get(); // hold on to key in strong ref |
635 | if (nextKey == null) |
636 | entry = entry.next; |
637 | } |
638 | return true; |
639 | } |
640 | |
641 | /** The common parts of next() across different types of iterators */ |
642 | protected Entry<K,V> nextEntry() { |
643 | if (modCount != expectedModCount) |
644 | throw new ConcurrentModificationException(); |
645 | if (nextKey == null && !hasNext()) |
646 | throw new NoSuchElementException(); |
647 | |
648 | lastReturned = entry; |
649 | entry = entry.next; |
650 | currentKey = nextKey; |
651 | nextKey = null; |
652 | return lastReturned; |
653 | } |
654 | |
655 | public void remove() { |
656 | if (lastReturned == null) |
657 | throw new IllegalStateException(); |
658 | if (modCount != expectedModCount) |
659 | throw new ConcurrentModificationException(); |
660 | |
661 | WeakHashMap2.this.remove(currentKey); |
662 | expectedModCount = modCount; |
663 | lastReturned = null; |
664 | currentKey = null; |
665 | } |
666 | |
667 | } |
668 | |
669 | private class ValueIterator extends HashIterator<V> { |
670 | public V next() { |
671 | return nextEntry().value; |
672 | } |
673 | } |
674 | |
675 | private class KeyIterator extends HashIterator<K> { |
676 | public K next() { |
677 | return nextEntry().getKey(); |
678 | } |
679 | } |
680 | |
681 | private class EntryIterator extends HashIterator<Map.Entry<K,V>> { |
682 | public Map.Entry<K,V> next() { |
683 | return nextEntry(); |
684 | } |
685 | } |
686 | |
687 | // Views |
688 | |
689 | private transient Set<Map.Entry<K,V>> entrySet; |
690 | |
691 | /** |
692 | * Returns a {@link Set} view of the keys contained in this map. |
693 | * The set is backed by the map, so changes to the map are |
694 | * reflected in the set, and vice-versa. If the map is modified |
695 | * while an iteration over the set is in progress (except through |
696 | * the iterator's own <tt>remove</tt> operation), the results of |
697 | * the iteration are undefined. The set supports element removal, |
698 | * which removes the corresponding mapping from the map, via the |
699 | * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
700 | * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
701 | * operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
702 | * operations. |
703 | */ |
704 | public Set<K> keySet() { |
705 | Set<K> ks = (Set) _get(this, 'keySet); |
706 | if (ks == null) { |
707 | ks = new KeySet(); |
708 | set(this, 'keySet, ks); |
709 | } |
710 | return ks; |
711 | } |
712 | |
713 | private class KeySet extends AbstractSet<K> { |
714 | public Iterator<K> iterator() { |
715 | return new KeyIterator(); |
716 | } |
717 | |
718 | public int size() { |
719 | return WeakHashMap2.this.size(); |
720 | } |
721 | |
722 | public boolean contains(Object o) { |
723 | return containsKey(o); |
724 | } |
725 | |
726 | public boolean remove(Object o) { |
727 | if (containsKey(o)) { |
728 | WeakHashMap2.this.remove(o); |
729 | return true; |
730 | } |
731 | else |
732 | return false; |
733 | } |
734 | |
735 | public void clear() { |
736 | WeakHashMap2.this.clear(); |
737 | } |
738 | } |
739 | |
740 | /** |
741 | * Returns a {@link Collection} view of the values contained in this map. |
742 | * The collection is backed by the map, so changes to the map are |
743 | * reflected in the collection, and vice-versa. If the map is |
744 | * modified while an iteration over the collection is in progress |
745 | * (except through the iterator's own <tt>remove</tt> operation), |
746 | * the results of the iteration are undefined. The collection |
747 | * supports element removal, which removes the corresponding |
748 | * mapping from the map, via the <tt>Iterator.remove</tt>, |
749 | * <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
750 | * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
751 | * support the <tt>add</tt> or <tt>addAll</tt> operations. |
752 | */ |
753 | public Collection<V> values() { |
754 | Collection<V> vs = (Collection) _get(this, 'values); |
755 | if (vs == null) { |
756 | vs = new Values(); |
757 | set(this, 'values, vs); |
758 | } |
759 | return vs; |
760 | } |
761 | |
762 | private class Values extends AbstractCollection<V> { |
763 | public Iterator<V> iterator() { |
764 | return new ValueIterator(); |
765 | } |
766 | |
767 | public int size() { |
768 | return WeakHashMap2.this.size(); |
769 | } |
770 | |
771 | public boolean contains(Object o) { |
772 | return containsValue(o); |
773 | } |
774 | |
775 | public void clear() { |
776 | WeakHashMap2.this.clear(); |
777 | } |
778 | } |
779 | |
780 | public Set<Map.Entry<K,V>> entrySet() { |
781 | Set<Map.Entry<K,V>> es = entrySet; |
782 | return es != null ? es : (entrySet = new EntrySet()); |
783 | } |
784 | |
785 | private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
786 | public Iterator<Map.Entry<K,V>> iterator() { |
787 | return new EntryIterator(); |
788 | } |
789 | |
790 | public boolean contains(Object o) { |
791 | if (!(o instanceof Map.Entry)) |
792 | return false; |
793 | Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
794 | Entry<K,V> candidate = getEntry(e.getKey()); |
795 | return candidate != null && candidate.equals(e); |
796 | } |
797 | |
798 | public boolean remove(Object o) { |
799 | return removeMapping(o); |
800 | } |
801 | |
802 | public int size() { |
803 | return WeakHashMap2.this.size(); |
804 | } |
805 | |
806 | public void clear() { |
807 | WeakHashMap2.this.clear(); |
808 | } |
809 | |
810 | private List<Map.Entry<K,V>> deepCopy() { |
811 | List<Map.Entry<K,V>> list = new ArrayList(size()); |
812 | for (Map.Entry<K,V> e : this) |
813 | list.add(new AbstractMap.SimpleEntry(e)); |
814 | return list; |
815 | } |
816 | |
817 | public Object[] toArray() { |
818 | return deepCopy().toArray(); |
819 | } |
820 | |
821 | public <T> T[] toArray(T[] a) { |
822 | return deepCopy().toArray(a); |
823 | } |
824 | } |
825 | |
826 | int keyHashCode(O o) { |
827 | ret _hashCode(o); |
828 | } |
829 | |
830 | bool keyEquals(O a, O b) { |
831 | ret eq(a, b); |
832 | } |
833 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): cfunsshuasjs, mqqgnosmbjvj, onxytkatvevr
No comments. add comment
Snippet ID: | #1010605 |
Snippet name: | WeakHashMap2 [does not implement everything, pluggable hash function not ready] |
Eternal ID of this version: | #1010605/19 |
Text MD5: | 91323f5eaed8e53883b61d322e0db7a1 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | Yes |
Created/modified: | 2017-12-10 05:32:41 |
Source code size: | 26116 bytes / 833 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 545 / 1307 |
Version history: | 18 change(s) |
Referenced in: | [show references] |