Skip to content

Commit

Permalink
[Truffle] Cleanup WeakValuedMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Dec 16, 2016
1 parent 7dc7106 commit 2743445
Showing 1 changed file with 3 additions and 13 deletions.
Expand Up @@ -40,8 +40,7 @@
*/
public class WeakValuedMap<Key, Value> {

private final Map<Key, KeyedReference<Key, Value>> map = newMap();
@SuppressWarnings("unchecked")
private final Map<Key, KeyedReference<Key, Value>> map = new ConcurrentHashMap<>();
private final ReferenceQueue<Value> deadRefs = new ReferenceQueue<>();

public final void put(Key key, Value value) {
Expand All @@ -66,15 +65,6 @@ public int size() {
return map.size();
}

/**
* Construct the backing store map for this WeakValuedMap. It should be capable of safe concurrent read and write.
*
* @return the backing store map
*/
protected Map<Key, KeyedReference<Key, Value>> newMap() {
return new ConcurrentHashMap<>();
}

protected static class KeyedReference<Key, Value> extends WeakReference<Value> {

protected final Key key;
Expand All @@ -89,8 +79,8 @@ public KeyedReference(Value object, Key key, ReferenceQueue<? super Value> queue
@SuppressWarnings("unchecked")
private void cleanReferences() {
KeyedReference<Key, Value> ref;
while ( ( ref = (KeyedReference) deadRefs.poll() ) != null ) {
map.remove( ref.key );
while ((ref = (KeyedReference<Key, Value>) deadRefs.poll()) != null) {
map.remove(ref.key);
}
}
}

0 comments on commit 2743445

Please sign in to comment.