| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.lang.ref.Reference; |
| 9 |
import java.lang.ref.ReferenceQueue; |
| 10 |
import java.lang.ref.SoftReference; |
| 11 |
import java.util.AbstractMap; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.Map; |
| 14 |
import java.util.Set; |
| 15 |
|
| 16 |
/** |
| 17 |
* Map which stores items using SoftReference. Items can be garbage collected |
| 18 |
* and removed. It is not a general purpose cache, as it doesn't implement some |
| 19 |
* methods, and others not according to the map definition, to improve speed. |
| 20 |
* |
| 21 |
* @param <K> the key type |
| 22 |
* @param <V> the value type |
| 23 |
*/ |
| 24 |
public class SoftValuesHashMap<K, V> extends AbstractMap<K, V> { |
| 25 |
|
| 26 |
private final Map<K, SoftValue<V>> map; |
| 27 |
private final ReferenceQueue<V> queue = new ReferenceQueue<>(); |
| 28 |
|
| 29 |
public SoftValuesHashMap() { |
| 30 |
map = new HashMap<>(); |
| 31 |
} |
| 32 |
|
| 33 |
@SuppressWarnings("unchecked") |
| 34 |
private void processQueue() { |
| 35 |
while (true) { |
| 36 |
Reference<? extends V> o = queue.poll(); |
| 37 |
if (o == null) { |
| 38 |
return; |
| 39 |
} |
| 40 |
SoftValue<V> k = (SoftValue<V>) o; |
| 41 |
Object key = k.key; |
| 42 |
map.remove(key); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public V get(Object key) { |
| 48 |
processQueue(); |
| 49 |
SoftReference<V> o = map.get(key); |
| 50 |
if (o == null) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
return o.get(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Store the object. The return value of this method is null or a |
| 58 |
* SoftReference. |
| 59 |
* |
| 60 |
* @param key the key |
| 61 |
* @param value the value |
| 62 |
* @return null or the old object. |
| 63 |
*/ |
| 64 |
@Override |
| 65 |
public V put(K key, V value) { |
| 66 |
processQueue(); |
| 67 |
SoftValue<V> old = map.put(key, new SoftValue<>(value, queue, key)); |
| 68 |
return old == null ? null : old.get(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Remove an object. |
| 73 |
* |
| 74 |
* @param key the key |
| 75 |
* @return null or the old object |
| 76 |
*/ |
| 77 |
@Override |
| 78 |
public V remove(Object key) { |
| 79 |
processQueue(); |
| 80 |
SoftReference<V> ref = map.remove(key); |
| 81 |
return ref == null ? null : ref.get(); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void clear() { |
| 86 |
processQueue(); |
| 87 |
map.clear(); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Set<Entry<K, V>> entrySet() { |
| 92 |
throw new UnsupportedOperationException(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* A soft reference that has a hard reference to the key. |
| 97 |
*/ |
| 98 |
private static class SoftValue<T> extends SoftReference<T> { |
| 99 |
final Object key; |
| 100 |
|
| 101 |
public SoftValue(T ref, ReferenceQueue<T> q, Object key) { |
| 102 |
super(ref, q); |
| 103 |
this.key = key; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
} |