Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 88 additions & 65 deletions src/eu/webtoolkit/jwt/utils/WeakValueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ public class WeakValueMap<K, V> implements Map<K, V> {

private static class KeyedWeakReference<K, V> extends WeakReference<V> {
private K key;

KeyedWeakReference(K key, V value, ReferenceQueue<V> referenceQueue) {
super(value, referenceQueue);
this.key = key;
}

K getKey() { return key; }
}

private HashMap<K, KeyedWeakReference<K, V>> storage = new HashMap<K, KeyedWeakReference<K, V>>();

public int size() {
prune();
return storage.size();
synchronized(storage)
{
return storage.size();
}
}

public boolean isEmpty() {
Expand All @@ -35,109 +38,129 @@ public boolean isEmpty() {

public boolean containsKey(Object key) {
prune();
return storage.containsKey(key);
synchronized(storage) {
return storage.containsKey(key);
}
}

public boolean containsValue(Object value) {
for (KeyedWeakReference<K, V> v : storage.values()) {
V vv = v.get();
if (vv != null)
if (value.equals(vv))
return true;
synchronized(storage) {
for (KeyedWeakReference<K, V> v : storage.values()) {
V vv = v.get();
if (vv != null)
if (value.equals(vv))
return true;
}
}
return false;
}

public V get(Object key) {
prune();
WeakReference<V> v = storage.get(key);
if (v != null)
return v.get();
else
return null;
synchronized(storage) {
WeakReference<V> v = storage.get(key);
if (v != null)
return v.get();
else
return null;
}
}

public V put(K key, V value) {
storage.put(key, new KeyedWeakReference<K, V>(key, value, referenceQueue));
return value;
synchronized(storage) {
storage.put(key, new KeyedWeakReference<K, V>(key, value, referenceQueue));
return value;
}
}

public V remove(Object key) {
KeyedWeakReference<K, V> k = storage.get(key);
if (k != null) {
V result = k.get();
storage.remove(key);
return result;
} else
return null;
synchronized(storage) {
KeyedWeakReference<K, V> k = storage.get(key);
if (k != null) {
V result = k.get();
storage.remove(key);
return result;
} else
return null;
}
}

public void putAll(Map<? extends K, ? extends V> m) {
for (K k : m.keySet())
put(k, m.get(k));
synchronized(storage) {
for (K k : m.keySet())
put(k, m.get(k));
}
}

public void clear() {
storage.clear();
synchronized(storage) {
storage.clear();
}
}

public Set<K> keySet() {
prune();
return storage.keySet();
synchronized(storage) {
return storage.keySet();
}
}

public Collection<V> values() {
Set<V> result = new HashSet<V>();

for (KeyedWeakReference<K, V> v : storage.values()) {
V vv = v.get();
if (vv != null)
result.add(vv);
synchronized(storage) {
Set<V> result = new HashSet<V>();

for (KeyedWeakReference<K, V> v : storage.values()) {
V vv = v.get();
if (vv != null)
result.add(vv);
}

return result;
}

return result;
}

public Set<java.util.Map.Entry<K, V>> entrySet() {
prune();

@SuppressWarnings("unused")
Set<java.util.Map.Entry<K, V>> result = new HashSet<Map.Entry<K,V>>();

for (final java.util.Map.Entry<K, KeyedWeakReference<K, V>> i : storage.entrySet()) {
final V vv = i.getValue().get();
if (vv != null)
result.add(new Entry<K, V>() {
@Override
public K getKey() {
return i.getKey();
}

@Override
public V getValue() {
return vv;
}

@Override
public V setValue(V value) {
throw new RuntimeException("Not implemented");
}
});
Set<java.util.Map.Entry<K, V>> result = new HashSet<Map.Entry<K,V>>();
synchronized(storage) {
for (final java.util.Map.Entry<K, KeyedWeakReference<K, V>> i : storage.entrySet()) {
final V vv = i.getValue().get();
if (vv != null)
result.add(new Entry<K, V>() {
@Override
public K getKey() {
return i.getKey();
}

@Override
public V getValue() {
return vv;
}

@Override
public V setValue(V value) {
throw new RuntimeException("Not implemented");
}
});
}
}

return result;
}

private void prune() {
//System.gc();
//Runtime.getRuntime().runFinalization();
for (;;) {
@SuppressWarnings("unchecked")
KeyedWeakReference<K, V> ref = (KeyedWeakReference<K, V>) referenceQueue.poll();
if (ref != null) {
storage.remove(ref.getKey());
} else
break;
synchronized(storage) {
for (;;) {
@SuppressWarnings("unchecked")
KeyedWeakReference<K, V> ref = (KeyedWeakReference<K, V>) referenceQueue.poll();
if (ref != null) {
storage.remove(ref.getKey());
} else
break;
}
}
}
}