rwth.i2.ltlrv.data
Class WeakValuesHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by rwth.i2.ltlrv.data.WeakValuesHashMap<K,V>
Type Parameters:
K - type parameter for key type
V - type parameter for value type
All Implemented Interfaces:
Serializable, Map<K,V>, WeakValuesMap<K,V>

public class WeakValuesHashMap<K,V>
extends AbstractMap<K,V>
implements WeakValuesMap<K,V>, Serializable

HashMap with weak keys. Bugfixed by Eric Bodden to make equals/hashCode work again. Eric Bodden also added locking.
The implementation is not very efficient, because an entrySet is generated for almost any operation. This is quite expensive.

Author:
Dr. Heinz M. Kabutz, Eric Bodden
See Also:
javaspecialists.co.za, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface rwth.i2.ltlrv.data.WeakValuesMap
WeakValuesMap.Util
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
private  Map<K,WeakReference<V>> hash
          The internal HashMap that will hold the WeakReference.
protected  int hashCode
           
protected  boolean hashCodeDirty
           
private  boolean locked
          If true, this map may not be modified any more.
private  ReferenceQueue<V> queue
          Reference queue for cleared WeakReference objects.
private  Map<WeakReference<V>,K> reverseLookup
           
private static long serialVersionUID
          Generated SUID.
protected  int size
           
protected  boolean sizeDirty
           
 
Constructor Summary
WeakValuesHashMap()
           
 
Method Summary
 void clear()
          
protected  void dirty()
          Marks hashCode and size as dirty.
 Set<Map.Entry<K,V>> entrySet()
          Returns a copy of the key/values in the map at the point of calling.
private  void expungeStaleEntries()
          Removes stale entries from the map.
 V get(Object key)
          
 int hashCode()
          
 boolean isLocked()
          Returns if this map is locked.
 void lock()
          Locks the map, so that no modifications are allowed any more.
 V put(K key, V value)
          
 V remove(Object key)
          
 int size()
          
 String toString()
          
 
Methods inherited from class java.util.AbstractMap
clone, containsKey, containsValue, equals, isEmpty, keySet, putAll, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, containsValue, equals, isEmpty, keySet, putAll, values
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Generated SUID.

See Also:
Constant Field Values

hash

private final Map<K,WeakReference<V>> hash
The internal HashMap that will hold the WeakReference.


reverseLookup

private final Map<WeakReference<V>,K> reverseLookup

queue

private final ReferenceQueue<V> queue
Reference queue for cleared WeakReference objects.


locked

private boolean locked
If true, this map may not be modified any more.


size

protected int size

sizeDirty

protected boolean sizeDirty

hashCode

protected int hashCode

hashCodeDirty

protected boolean hashCodeDirty
Constructor Detail

WeakValuesHashMap

public WeakValuesHashMap()
Method Detail

get

public V get(Object key)

Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>

expungeStaleEntries

private void expungeStaleEntries()
Removes stale entries from the map.


put

public V put(K key,
             V value)

Specified by:
put in interface Map<K,V>
Overrides:
put in class AbstractMap<K,V>

remove

public V remove(Object key)

Specified by:
remove in interface Map<K,V>
Overrides:
remove in class AbstractMap<K,V>

clear

public void clear()

Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

size

public int size()

Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

lock

public void lock()
Locks the map, so that no modifications are allowed any more. When a modifying method is called, a RuntimeException will be thrown.

Specified by:
lock in interface WeakValuesMap<K,V>

isLocked

public boolean isLocked()
Returns if this map is locked. When it is locked and a modifying method is called, a RuntimeException will be thrown.

Specified by:
isLocked in interface WeakValuesMap<K,V>
Returns:
true if the map is locked

entrySet

public Set<Map.Entry<K,V>> entrySet()
Returns a copy of the key/values in the map at the point of calling. However, setValue still sets the value in the actual WeakValuesHashMap.

Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>
Returns:
the entry set

hashCode

public int hashCode()

Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class AbstractMap<K,V>

dirty

protected void dirty()
Marks hashCode and size as dirty.


toString

public String toString()

Overrides:
toString in class AbstractMap<K,V>