org.ka2ddo.util
Class SoftCache

java.lang.Object
  extended by java.util.AbstractMap
      extended by org.ka2ddo.util.SoftCache
All Implemented Interfaces:
java.util.Map

public class SoftCache
extends java.util.AbstractMap

This is a HashMap based on using SoftReferences to the values. If a SoftReference becomes cleared by garbage-collection, the corresponding key is removed from the map. It uses conventional linked lists for the entries in the map, and is modifiable.

The SoftCache does not maintain any strong/hard references to any of its entry values. To prevent an entry in the SoftCache from being reclaimed by garbage-collection, external code must maintain a hard reference to the values object in question.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
SoftCache()
          Constructs an empty SoftCache with the default initial capacity (16) and the default load factor (0.75).
SoftCache(int initialCapacity)
          Constructs an empty SoftCache with the specified initial capacity and the default load factor (0.75).
SoftCache(int initialCapacity, float loadFactor)
          Constructs an empty SoftCache with the specified initial capacity and load factor.
SoftCache(java.util.Map m)
          Constructs a new SoftCache with the same mappings as the specified Map.
 
Method Summary
 void clear()
          Removes all mappings from this map (optional operation).
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains a mapping for the specified key.
 java.util.Set entrySet()
          Returns a set view of the mappings contained in this map.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which this map maps the specified key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this map (optional operation).
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for this key from this map if present.
 int size()
          Returns the number of key-value mappings in this map.
 
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SoftCache

public SoftCache(int initialCapacity,
                 float loadFactor)
Constructs an empty SoftCache with the specified initial capacity and load factor.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.

SoftCache

public SoftCache(int initialCapacity)
Constructs an empty SoftCache with the specified initial capacity and the default load factor (0.75).

Parameters:
initialCapacity - the initial capacity.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is negative.

SoftCache

public SoftCache()
Constructs an empty SoftCache with the default initial capacity (16) and the default load factor (0.75).


SoftCache

public SoftCache(java.util.Map m)
Constructs a new SoftCache with the same mappings as the specified Map. The SoftCache is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified Map.

Parameters:
m - the map whose mappings are to be placed in this map.
Throws:
java.lang.NullPointerException - if the specified map is null.
Method Detail

entrySet

public java.util.Set entrySet()
Returns a set view of the mappings contained in this map. Each element in this set is a Map.Entry. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. (If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.) The set supports element removal, which removes the corresponding entry from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap
Returns:
a set view of the mappings contained in this map.

size

public int size()
Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

This implementation returns entrySet().size().

Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap
Returns:
the number of key-value mappings in this map.

clear

public void clear()
Removes all mappings from this map (optional operation).

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

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains a mapping for the specified key.

This implementation iterates over entrySet() searching for an entry with the specified key. If such an entry is found, true is returned. If the iteration terminates without finding such an entry, false is returned. Note that this implementation requires linear time in the size of the map; many implementations will override this method.

Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap
Parameters:
key - key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key.
Throws:
java.lang.NullPointerException - key is null and this map does not not permit null keys.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which this map maps the specified key. Returns null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map formerly contained an entry for this key, but the value was garbage-collected. The containsKey operation may be used to distinguish these two cases.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key.
Throws:
java.lang.NullPointerException - if the key is null and this map does not not permit null keys.
See Also:
containsKey(Object)

remove

public java.lang.Object remove(java.lang.Object key)
Removes the mapping for this key from this map if present.

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap
Parameters:
key - key whose mapping is to be removed from the map.
Returns:
previous value associated with specified key, or null if there was no entry for key. (A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.)

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key.
Throws:
java.lang.NullPointerException - this map does not permit null keys or values, and the specified key or value is null.