1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 00:38:52 -05:00

* refactor

This commit is contained in:
Reinhard Pointner 2015-11-14 18:52:35 +00:00
parent 333510b7e2
commit 65542919fa

View File

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