org.metasyntactic.structures
Class SoftReferenceHashMap

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--org.metasyntactic.structures.SoftReferenceHashMap
All Implemented Interfaces:
java.util.Map

public class SoftReferenceHashMap
extends java.util.AbstractMap

A hashtable-based Map implementation with soft keys. An entry in a SoftReferenceHashMap will automatically be removed when its key is no longer in ordinary use and the garbage collector has exhausted all other means to reclaim enough memory for its current task.

More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

Both null values and the null key are supported. This class has performance characteristics similar to those of the HashMap class, and has the same efficiency parameters of initial capacity and load factor.

Like most collection classes, this class is not synchronized. A synchronized SoftReferenceHashMap may be constructed using the Collections.synchronizedMap method.

The behavior of the SoftReferenceHashMap class depends in part upon the actions of the garbage collector, so several familiar (though not required) Map invariants do not hold for this class. Because the garbage collector may discard keys at any time, a SoftReferenceHashMap may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a SoftReferenceHashMap instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the containsKey method to return true and later false for a given key, for the get method to return a value for a given key but later return null, for the put method to return null and the remove method to return false for a key that previously appeared to be in the map, and for successive examinations of the key set, the value set, and the entry set to yield successively smaller numbers of elements.

Each key object in a SoftReferenceHashMap is stored indirectly as the referent of a soft reference. Therefore a key will automatically be removed only after the soft references to it, both inside and outside of the map, have been cleared by the garbage collector.

Implementation note: The value objects in a SoftReferenceHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded. Note that a value object may refer indirectly to its key via the SoftReferenceHashMap itself; that is, a value object may strongly refer to some other key object whose associated value object, in turn, strongly refers to the key of the first value object. This problem may be fixed in a future release.

See Also:
, ,

Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
SoftReferenceHashMap()
          Constructs a new, empty SoftReferenceHashMap with the default initial capacity and the default load factor, which is 0.75.
SoftReferenceHashMap(int initialCapacity)
          Constructs a new, empty SoftReferenceHashMap with the given initial capacity and the default load factor, which is 0.75.
SoftReferenceHashMap(int initialCapacity, float loadFactor)
          Constructs a new, empty SoftReferenceHashMap with the given initial capacity and the given load factor.
SoftReferenceHashMap(java.util.Map t)
          Constructs a new SoftReferenceHashMap with the same mappings as the specified Map.
 
Method Summary
 void clear()
          Removes all mappings from this map.
 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 in this map.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which this map maps the specified key.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
static void main(java.lang.String[] args)
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Updates this map so that the given key maps to the given value.
 java.lang.Object remove(java.lang.Object key)
          Removes the mapping for the given 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, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SoftReferenceHashMap

public SoftReferenceHashMap(int initialCapacity,
                            float loadFactor)
Constructs a new, empty SoftReferenceHashMap with the given initial capacity and the given load factor.

Parameters:
initialCapacity - The initial capacity of the SoftReferenceHashMap
loadFactor - The load factor of the SoftReferenceHashMap
Throws:
java.lang.IllegalArgumentException - If the initial capacity is less than zero, or if the load factor is nonpositive

SoftReferenceHashMap

public SoftReferenceHashMap(int initialCapacity)
Constructs a new, empty SoftReferenceHashMap with the given initial capacity and the default load factor, which is 0.75.

Parameters:
initialCapacity - The initial capacity of the SoftReferenceHashMap
Throws:
java.lang.IllegalArgumentException - If the initial capacity is less than zero

SoftReferenceHashMap

public SoftReferenceHashMap()
Constructs a new, empty SoftReferenceHashMap with the default initial capacity and the default load factor, which is 0.75.


SoftReferenceHashMap

public SoftReferenceHashMap(java.util.Map t)
Constructs a new SoftReferenceHashMap with the same mappings as the specified Map. The SoftReferenceHashMap is created with an initial capacity of twice the number of mappings in the specified map or 11 (whichever is greater), and a default load factor, which is 0.75.

Parameters:
t - the map whose mappings are to be placed in this map.
Since:
1.3
Method Detail

size

public int size()
Returns the number of key-value mappings in this map. Note: In contrast with most implementations of the Map interface, the time required by this operation is linear in the size of the map.

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

isEmpty

public boolean isEmpty()
Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface java.util.Map
Overrides:
isEmpty in class java.util.AbstractMap
Returns:
true if this map contains no key-value mappings.

containsKey

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

Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap
Parameters:
key - The key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which this map maps the specified key. If this map does not contain a value for this key, then return null.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap
Parameters:
key - The key whose associated value, if any, is to be returned
Returns:

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Updates this map so that the given key maps to the given value. If the map previously contained a mapping for key then that mapping is replaced and the previous value is returned.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Parameters:
key - The key that is to be mapped to the given value
value - The value to which the given key is to be mapped
Returns:
The previous value to which this key was mapped, or null if if there was no mapping for the key

remove

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

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap
Parameters:
key - The key whose mapping is to be removed
Returns:
The value to which this key was mapped, or null if there was no mapping for the key

clear

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

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

entrySet

public java.util.Set entrySet()
Returns a Set view of the mappings in this map.

Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap
Returns:
The entry set for this Map

main

public static void main(java.lang.String[] args)
Parameters:
args -