org.intermine.util
Class ReferenceMap<K,V>

java.lang.Object
  extended by org.intermine.util.ReferenceMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
java.util.Map<K,V>
Direct Known Subclasses:
SoftReferenceMap, WeakReferenceMap

public abstract class ReferenceMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>

This is a Map implementation designed specifically for people intending to create a cache. The class should be subclassed to provide soft or weak reference behaviour. The keys are held strongly, but the values are held weakly or softly, so the values can be garbage-collected. When an entry is garbage-collected, its key is removed from the Map on the next Map activity.

The entrySet() and values() methods of this class do not work.

See Also:
SoftReference

Nested Class Summary
protected static interface ReferenceMap.ReferenceWithKey<K>
          Interface for entries in the map.
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
protected  java.lang.String name
           
protected static org.intermine.util.ReferenceMap.NullValue NULL_VALUE
           
protected  java.lang.ref.ReferenceQueue<java.lang.Object> queue
           
protected  java.util.Map<K,java.lang.ref.Reference<java.lang.Object>> subMap
           
 
Constructor Summary
ReferenceMap()
           
 
Method Summary
 void clear()
          
 boolean containsKey(java.lang.Object key)
          
 boolean containsValue(java.lang.Object value)
          
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          
 boolean equals(java.lang.Object o)
          
 V get(java.lang.Object key)
          
 int hashCode()
          
 boolean isEmpty()
          
 java.util.Set<K> keySet()
          
protected abstract  java.lang.ref.Reference<java.lang.Object> newRef(java.lang.Object value, java.lang.ref.ReferenceQueue<java.lang.Object> queue, K key)
          Private method to create a new ReferenceWithKey object.
 V put(K key, V value)
          
 void putAll(java.util.Map<? extends K,? extends V> t)
          
 V remove(java.lang.Object key)
          
 int size()
          
 java.lang.String toString()
          
 java.util.Collection<V> values()
          
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL_VALUE

protected static final org.intermine.util.ReferenceMap.NullValue NULL_VALUE

subMap

protected java.util.Map<K,java.lang.ref.Reference<java.lang.Object>> subMap

queue

protected java.lang.ref.ReferenceQueue<java.lang.Object> queue

name

protected java.lang.String name
Constructor Detail

ReferenceMap

public ReferenceMap()
Method Detail

size

public int size()

Specified by:
size in interface java.util.Map<K,V>

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface java.util.Map<K,V>

keySet

public java.util.Set<K> keySet()

Specified by:
keySet in interface java.util.Map<K,V>

clear

public void clear()

Specified by:
clear in interface java.util.Map<K,V>

get

public V get(java.lang.Object key)

Specified by:
get in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)

Specified by:
containsKey in interface java.util.Map<K,V>

put

public V put(K key,
             V value)

Specified by:
put in interface java.util.Map<K,V>

newRef

protected abstract java.lang.ref.Reference<java.lang.Object> newRef(java.lang.Object value,
                                                                    java.lang.ref.ReferenceQueue<java.lang.Object> queue,
                                                                    K key)
Private method to create a new ReferenceWithKey object. This should be overridden by subclasses wishing to create soft/weak behaviour.

Parameters:
value - the value put into the Reference
queue - the ReferenceQueue to register the Reference in
key - the key
Returns:
a ReferenceWithKey, that is also a Reference (long story, no multiple inheritance in Java, and Reference is daftly an abstract class rather than an interface)

putAll

public void putAll(java.util.Map<? extends K,? extends V> t)

Specified by:
putAll in interface java.util.Map<K,V>

remove

public V remove(java.lang.Object key)

Specified by:
remove in interface java.util.Map<K,V>

containsValue

public boolean containsValue(java.lang.Object value)

Specified by:
containsValue in interface java.util.Map<K,V>

entrySet

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

Specified by:
entrySet in interface java.util.Map<K,V>

values

public java.util.Collection<V> values()

Specified by:
values in interface java.util.Map<K,V>

equals

public boolean equals(java.lang.Object o)

Specified by:
equals in interface java.util.Map<K,V>
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()

Specified by:
hashCode in interface java.util.Map<K,V>
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object