net.walend.collection
Class SoftHashMap

java.lang.Object
  |
  +--net.walend.collection.AbstractHashMap
        |
        +--net.walend.collection.SoftHashMap
All Implemented Interfaces:
java.lang.Cloneable, HasState, Map, MutableMap, java.io.Serializable
Direct Known Subclasses:
NullValueHashMap

public class SoftHashMap
extends AbstractHashMap
implements MutableMap

This MutableHashMap's values are held by soft references which may disapear if memory gets short. Be prepared for the occasional null value. This class is inherently multithreaded; get the guard from getGuard() if you need to keep other threads out of the map while you work.

Since:
20010827
Author:
David Walend

Nested Class Summary
private  class SoftHashMap.SoftEntry
          Entries for the hash map.
 
Nested classes inherited from class net.walend.collection.AbstractHashMap
 
Field Summary
private  java.lang.Object guard
           
 
Fields inherited from class net.walend.collection.AbstractHashMap
 
Fields inherited from interface net.walend.collection.Map
EMPTY
 
Constructor Summary
SoftHashMap()
           
SoftHashMap(Identitor identitor)
           
SoftHashMap(Identitor identitor, int capacity)
           
SoftHashMap(Identitor identitor, int capacity, float loadFactor)
           
SoftHashMap(int capacity)
           
SoftHashMap(int capacity, float loadFactor)
           
SoftHashMap(java.util.Map orig)
           
SoftHashMap(Map orig)
           
 
Method Summary
 void clear()
          Removes all key, value pairs from this Map.
 boolean containsAll(Map c)
          Returns true if this collection contains all of the Objects in the specified collection.
 boolean containsKey(java.lang.Object key)
          Returns true if this map contains an entry for key.
 boolean containsValue(java.lang.Object value)
          Returns true if this map contains value.
 java.lang.Object get(java.lang.Object key)
          Returns the value for the key.
 java.lang.Object getGuard()
          Return the synchronization guard for this SoftHashMap.
 java.util.Map getJavaMap()
          Return a java.util.Collection of these Objects.
 Set getKeys()
          Returns an immutable shallow copy of the Set of keys.
 Collection getValues()
          Returns an immutable shallow copy of the Collection of values.
 boolean isEmpty()
          Returns true if this Map has no entries.
 java.util.Iterator keyIterator()
          Returns an iterator over the keys.
protected  AbstractHashMap.Entry newEntry(int hash, java.lang.Object key, java.lang.Object value, AbstractHashMap.Entry next)
          Overridden to return entries with soft reference values.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Puts the key,value pair in the map.
 void putAll(Map map)
          Puts all of the key, value pairs in map into this Map.
 java.lang.Object remove(java.lang.Object key)
          Removes the key, value pair from this Map.
(package private)  void removeEntry(AbstractHashMap.Entry doomed)
          Removes the specified entry from this AbstractHashMap (and increments modCount).
 boolean sameContentsAs(Map c)
          Returns true if this Collection's contents are equal to c's.
 boolean sameStateAs(HasState victem)
          If two HasStates have the same internal state, return true.
 int size()
          Returns the number of key-value pairs in this map.
 void sweepNulls()
          Cleans out keys for null values.
 java.util.Iterator valueIterator()
          Returns an iterator over the values.
 
Methods inherited from class net.walend.collection.AbstractHashMap
capacity, clone, getIdentitor, getPrincipleInterface, loadFactor, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.walend.collection.Map
getIdentitor
 
Methods inherited from interface net.walend.collection.HasState
getPrincipleInterface
 

Field Detail

guard

private final java.lang.Object guard
Constructor Detail

SoftHashMap

public SoftHashMap()

SoftHashMap

public SoftHashMap(int capacity)

SoftHashMap

public SoftHashMap(int capacity,
                   float loadFactor)

SoftHashMap

public SoftHashMap(Identitor identitor)

SoftHashMap

public SoftHashMap(Identitor identitor,
                   int capacity)

SoftHashMap

public SoftHashMap(Identitor identitor,
                   int capacity,
                   float loadFactor)

SoftHashMap

public SoftHashMap(java.util.Map orig)

SoftHashMap

public SoftHashMap(Map orig)
Method Detail

sameStateAs

public boolean sameStateAs(HasState victem)
If two HasStates have the same internal state, return true.

For objects with subobjects, Generally this method should only return true if the internal objects are equal. Implement a contentsHaveSameState() method to determine if the contents have the same state.

Specified by:
sameStateAs in interface HasState
Overrides:
sameStateAs in class AbstractHashMap

size

public int size()
Returns the number of key-value pairs in this map.

This method calls sweepNulls(), and takes linear time.

Specified by:
size in interface Map
Overrides:
size in class AbstractHashMap
Following copied from class: net.walend.collection.AbstractHashMap
Returns:
the number of key-value mappings in this map.

isEmpty

public boolean isEmpty()
Returns true if this Map has no entries.

This method calls sweepNulls(), and takes linear time.

Specified by:
isEmpty in interface Map
Overrides:
isEmpty in class AbstractHashMap
Following copied from class: net.walend.collection.AbstractHashMap
Returns:
true if this map contains no key-value mappings.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this map contains an entry for key.

Specified by:
containsKey in interface Map
Overrides:
containsKey in class AbstractHashMap
Throws:
java.lang.NullPointerException - if key is null.

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this map contains value.

Specified by:
containsValue in interface Map
Overrides:
containsValue in class AbstractHashMap
Throws:
java.lang.NullPointerException - if value is null.

get

public java.lang.Object get(java.lang.Object key)
Returns the value for the key. Returns null if the Map contains no value for key.

Specified by:
get in interface Map
Overrides:
get in class AbstractHashMap
Throws:
java.lang.NullPointerException - if the key is null.

getKeys

public Set getKeys()
Returns an immutable shallow copy of the Set of keys.

Specified by:
getKeys in interface Map
Overrides:
getKeys in class AbstractHashMap
Following copied from class: net.walend.collection.AbstractHashMap
Returns:
a set view of the keys contained in this map.

getValues

public Collection getValues()
Returns an immutable shallow copy of the Collection of values.

Specified by:
getValues in interface Map
Overrides:
getValues in class AbstractHashMap
Following copied from class: net.walend.collection.AbstractHashMap
Returns:
a collection view of the values contained in this map.

keyIterator

public java.util.Iterator keyIterator()
Returns an iterator over the keys.

This method calls sweepNulls(), and takes linear time.

Specified by:
keyIterator in interface Map
Overrides:
keyIterator in class AbstractHashMap

valueIterator

public java.util.Iterator valueIterator()
Returns an iterator over the values.

This method calls sweepNulls(), and takes linear time.

Specified by:
valueIterator in interface Map
Overrides:
valueIterator in class AbstractHashMap

containsAll

public boolean containsAll(Map c)
Returns true if this collection contains all of the Objects in the specified collection. This method uses the contains() method.

Specified by:
containsAll in interface Map
Overrides:
containsAll in class AbstractHashMap

sameContentsAs

public boolean sameContentsAs(Map c)
Returns true if this Collection's contents are equal to c's.

Specified by:
sameContentsAs in interface Map
Overrides:
sameContentsAs in class AbstractHashMap

getJavaMap

public java.util.Map getJavaMap()
Return a java.util.Collection of these Objects.

Specified by:
getJavaMap in interface Map
Overrides:
getJavaMap in class AbstractHashMap

removeEntry

void removeEntry(AbstractHashMap.Entry doomed)
Description copied from class: AbstractHashMap
Removes the specified entry from this AbstractHashMap (and increments modCount).

Overrides:
removeEntry in class AbstractHashMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Puts the key,value pair in the map. Returns the previous value associated with key. If no value was associated, it returns null.

Specified by:
put in interface MutableMap
Overrides:
put in class AbstractHashMap
Throws:
java.lang.NullPointerException - if the key or value is null.

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key, value pair from this Map. Returns the value associated with key, or null if no value was associated.

Specified by:
remove in interface MutableMap
Overrides:
remove in class AbstractHashMap
Throws:
java.lang.NullPointerException - if the key is null.

putAll

public void putAll(Map map)
Puts all of the key, value pairs in map into this Map. map's pairs replace any conflicting pairs in this Map.

Specified by:
putAll in interface MutableMap
Overrides:
putAll in class AbstractHashMap
Throws:
java.lang.NullPointerException - if map is null.

clear

public void clear()
Removes all key, value pairs from this Map.

Specified by:
clear in interface MutableMap
Overrides:
clear in class AbstractHashMap

newEntry

protected AbstractHashMap.Entry newEntry(int hash,
                                         java.lang.Object key,
                                         java.lang.Object value,
                                         AbstractHashMap.Entry next)
Overridden to return entries with soft reference values.

Overrides:
newEntry in class AbstractHashMap

sweepNulls

public void sweepNulls()
Cleans out keys for null values. It scans the table, so don't call it without a good reason.


getGuard

public final java.lang.Object getGuard()
Return the synchronization guard for this SoftHashMap. Don't stand on this guard for too long; the garbage collection thread uses it to clear out SoftReferences.



Copyright (c) 2000, 2001, David Walend