org.gjt.rec.cache
Class MapCache

java.lang.Object
  |
  +--org.gjt.rec.cache.MapCache
All Implemented Interfaces:
Cache
Direct Known Subclasses:
FixedCache

public class MapCache
extends Object
implements Cache

MapCache is an implementation of the Cache interface that uses a Map to store its elements. Elements can be either strongly or weakly referred to. If strong references are used, elements put in this cache cannot be finalized until their entries in this cache have been invalidated.

Author:
Neil Stockbridge
See Also:
Cache, CacheElementSource

Nested Class Summary
static class MapCache.MapType
           
 
Field Summary
static String REVISION
           
 
Constructor Summary
MapCache()
          Constructs a MapCache that refers to its elements using weak references.
MapCache(CacheElementSource src)
          Constructs a MapCache that refers to its elements using weak references.
MapCache(MapCache.MapType type)
          Constructs a MapCache that refers to its elements in a particular manner.
MapCache(MapCache.MapType type, CacheElementSource src)
          Constructs a MapCache that refers to its elements in a particular manner.
 
Method Summary
 int elementCount()
           
 int hitCount()
           
 void invalidate(Object id)
          Invalidates the entry in this cache with a particular id.
 void invalidateAll()
          Invalidates all entries in this cache.
 Object lookup(Object id)
          Tries to find an element with a particular id in this cache.
 void put(Object id, Object element)
          Puts a particular element in this cache associated with a particular id.
 int requestCount()
           
 void resetStatistics()
          Resets the cache performance statistics to the values as they were when the cache was created.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values
Constructor Detail

MapCache

public MapCache()
Constructs a MapCache that refers to its elements using weak references.


MapCache

public MapCache(MapCache.MapType type)
Constructs a MapCache that refers to its elements in a particular manner.

Parameters:
type - if this argument is MapCache.MapType.WEAK, this cache will refer to its elements using weak references. if the argument is MapCache.MapType.STRONG, strong references will be used instead.

MapCache

public MapCache(CacheElementSource src)
Constructs a MapCache that refers to its elements using weak references. Cache elements will be obtained from the supplied CacheElementSource.

Parameters:
src - the element source from which the cache will obtain its elements in the event of a cache miss.

MapCache

public MapCache(MapCache.MapType type,
                CacheElementSource src)
Constructs a MapCache that refers to its elements in a particular manner. Cache elements will be obtained from the supplied CacheElementSource.

Parameters:
type - if this argument is MapCache.MapType.WEAK, this cache will refer to its elements using weak references. if the argument is MapCache.MapType.STRONG, strong references will be used instead.
src - the element source from which the cache will obtain its elements in the event of a cache miss.
Method Detail

lookup

public Object lookup(Object id)
Description copied from interface: Cache
Tries to find an element with a particular id in this cache.

Specified by:
lookup in interface Cache
Parameters:
id - the id of the desired element.
Returns:
the element corresponding to the supplied id or null if no such element could be found.

put

public void put(Object id,
                Object element)
Description copied from interface: Cache
Puts a particular element in this cache associated with a particular id. Note that invoking this method does not guarantee that the element will stay in the cache even long enough to be returned by a subsequent invocation of the lookup(Object) method.

Specified by:
put in interface Cache
Parameters:
id - the id associated with the supplied element.
element - the element to store in the cache.

invalidate

public void invalidate(Object id)
Description copied from interface: Cache
Invalidates the entry in this cache with a particular id.

Specified by:
invalidate in interface Cache
Parameters:
id - the id corresponding to the element to invalidate.

invalidateAll

public void invalidateAll()
Description copied from interface: Cache
Invalidates all entries in this cache.

Specified by:
invalidateAll in interface Cache

elementCount

public int elementCount()
Specified by:
elementCount in interface Cache
Returns:
the number of elements in this cache. this number is the actual number of elements cached, not the size of the element store.

requestCount

public int requestCount()
Specified by:
requestCount in interface Cache
Returns:
the number of cache requests made of this cache.

hitCount

public int hitCount()
Specified by:
hitCount in interface Cache
Returns:
the number of cache requests that have been satisfied by this cache.

resetStatistics

public void resetStatistics()
Description copied from interface: Cache
Resets the cache performance statistics to the values as they were when the cache was created.

Specified by:
resetStatistics in interface Cache

toString

public String toString()
Overrides:
toString in class Object