* refactor

This commit is contained in:
Reinhard Pointner 2015-11-14 18:52:35 +00:00
parent 333510b7e2
commit 65542919fa
1 changed files with 10 additions and 10 deletions

View File

@ -12,19 +12,18 @@ import java.util.Set;
public class WeakValueHashMap<K, V> extends AbstractMap<K, V> { public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
private HashMap<K, WeakValue<V>> references; private HashMap<K, WeakValue<V>> references;
private ReferenceQueue<V> referenceQueue; private ReferenceQueue<V> gcQueue;
public WeakValueHashMap(int capacity) { public WeakValueHashMap(int capacity) {
references = new HashMap<K, WeakValue<V>>(capacity); references = new HashMap<K, WeakValue<V>>(capacity);
referenceQueue = new ReferenceQueue<V>(); gcQueue = new ReferenceQueue<V>();
} }
@Override @Override
public V put(K key, V value) { public V put(K key, V value) {
processQueue(); processQueue();
WeakValue<V> valueRef = new WeakValue<V>(key, value, referenceQueue); return getReferenceValue(references.put(key, new WeakValue<V>(key, value, gcQueue)));
return getReferenceValue(references.put(key, valueRef));
}; };
@Override @Override
@ -36,7 +35,7 @@ public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
@Override @Override
public V remove(Object key) { public V remove(Object key) {
return getReferenceValue(references.get(key)); return getReferenceValue(references.remove(key));
} }
@Override @Override
@ -55,11 +54,12 @@ public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
public boolean containsValue(Object value) { public boolean containsValue(Object value) {
processQueue(); processQueue();
for (Entry<K, WeakValue<V>> entry : references.entrySet()) { for (WeakValue<V> it : references.values()) {
if (value == getReferenceValue(entry.getValue())) { if (value.equals(getReferenceValue(it))) {
return true; return true;
} }
} }
return false; return false;
} }
@ -81,7 +81,7 @@ public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
public Set<Entry<K, V>> entrySet() { public Set<Entry<K, V>> entrySet() {
processQueue(); processQueue();
Set<Entry<K, V>> entries = new LinkedHashSet<Entry<K, V>>(); Set<Entry<K, V>> entries = new LinkedHashSet<Entry<K, V>>(references.size());
for (Entry<K, WeakValue<V>> entry : references.entrySet()) { for (Entry<K, WeakValue<V>> entry : references.entrySet()) {
entries.add(new SimpleImmutableEntry<K, V>(entry.getKey(), getReferenceValue(entry.getValue()))); entries.add(new SimpleImmutableEntry<K, V>(entry.getKey(), getReferenceValue(entry.getValue())));
} }
@ -92,7 +92,7 @@ public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
public Collection<V> values() { public Collection<V> values() {
processQueue(); processQueue();
Collection<V> values = new ArrayList<V>(); Collection<V> values = new ArrayList<V>(references.size());
for (WeakValue<V> valueRef : references.values()) { for (WeakValue<V> valueRef : references.values()) {
values.add(getReferenceValue(valueRef)); values.add(getReferenceValue(valueRef));
} }
@ -106,7 +106,7 @@ public class WeakValueHashMap<K, V> extends AbstractMap<K, V> {
private void processQueue() { private void processQueue() {
WeakValue<?> valueRef; WeakValue<?> valueRef;
while ((valueRef = (WeakValue<?>) referenceQueue.poll()) != null) { while ((valueRef = (WeakValue<?>) gcQueue.poll()) != null) {
references.remove(valueRef.getKey()); references.remove(valueRef.getKey());
} }
} }