com.taco.data
Class SoftValueMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by com.taco.data.SoftValueMap
All Implemented Interfaces:
java.util.Map

public class SoftValueMap
extends java.util.AbstractMap

A map whose values may disappear when no strong references to them exist. This map does not support null keys or values! Use MapUtilities.wrap() and MapUtilities.unwrap() if you want to put null keys or values in this map. This class is not synchronized.


Nested Class Summary
protected  class SoftValueMap.SoftValue
          The values actually put into the storage map.
protected  class SoftValueMap.SoftValueEntrySet
          A class that acts as the entry set of a SoftValueMap.
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry
 
Field Summary
protected  java.util.Set _entrySet
          The entry set, lazily computed, and cached.
protected  java.util.Map _innerMap
          The map that provides the storage.
protected  java.lang.ref.ReferenceQueue _referenceQueue
          Cleared references are put here.
protected  long _version
          When the map is cleared, all instances of SoftValue shouldn't cause purgeClearedValues() to remove a new key/value pair when the soft reference is cleared.
 
Constructor Summary
SoftValueMap()
           
SoftValueMap(IMapFactory mapFactory, int capacity)
           
SoftValueMap(IMapFactory mapFactory, java.util.Map sourceMap)
           
SoftValueMap(int capacity)
           
SoftValueMap(java.util.Map sourceMap)
           
 
Method Summary
 void clear()
           
 java.lang.Object clone()
           
 boolean containsKey(java.lang.Object key)
           
 java.util.Set entrySet()
           
 java.lang.Object get(java.lang.Object key)
          If key is null, return null, since this map does not support null keys.
static void main(java.lang.String[] args)
          A simple test program.
 void purgeClearedValues()
          Remove all associations to values that have been cleared.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 java.lang.Object remove(java.lang.Object key)
           
 int size()
           
 
Methods inherited from class java.util.AbstractMap
containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_innerMap

protected java.util.Map _innerMap
The map that provides the storage.


_referenceQueue

protected java.lang.ref.ReferenceQueue _referenceQueue
Cleared references are put here.


_version

protected long _version

When the map is cleared, all instances of SoftValue shouldn't cause purgeClearedValues() to remove a new key/value pair when the soft reference is cleared.

To fix these problems, maintain a version number for the map, and mark instances of SoftValue with the version number. When the map is cleared, increment the version number. When purgeClearedValues() is called, compare the version numbers and only remove key/value pairs if the version number matches.

.


_entrySet

protected transient java.util.Set _entrySet
The entry set, lazily computed, and cached. The entry set by itself has no state, even though each iterator it returns does.

Constructor Detail

SoftValueMap

public SoftValueMap()

SoftValueMap

public SoftValueMap(int capacity)

SoftValueMap

public SoftValueMap(java.util.Map sourceMap)

SoftValueMap

public SoftValueMap(IMapFactory mapFactory,
                    int capacity)

SoftValueMap

public SoftValueMap(IMapFactory mapFactory,
                    java.util.Map sourceMap)
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.util.AbstractMap
Throws:
java.lang.CloneNotSupportedException

clear

public void clear()
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap

get

public java.lang.Object get(java.lang.Object key)
If key is null, return null, since this map does not support null keys. Otherwise, return value corresponding to key.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap

size

public int size()
Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap

purgeClearedValues

public void purgeClearedValues()
Remove all associations to values that have been cleared.


main

public static void main(java.lang.String[] args)
A simple test program.