net.sourceforge.groboutils.util.datastruct.v1
Class HashCache

java.lang.Object
  extended bynet.sourceforge.groboutils.util.datastruct.v1.HashCache

public class HashCache
extends java.lang.Object

Stores objects that are created via a unique key in a limited-sized cache. The objects are retrieved and created through the key. If the object with a requested key exists in the cache, then it is returned and the object is marked as the latest accessed object. If a request is made for an object whose key is not in the cache, then the object is created, put into the cache, and returned. If the cache is full when a new object is created, then the oldest item in the cache is removed.

This class is NOT thread safe. All thread safety issues will need to be covered by the calling class.

Future versions should use the ObjectCache to cache the node instances.

Since:
May 23, 2003
Version:
$Date: 2003/05/23 22:37:57 $
Author:
Matt Albrecht groboclown@users.sourceforge.net

Nested Class Summary
static interface HashCache.ObjectManager
          An interface which needs to be implemented and given to the cache in order to create new instances.
 
Constructor Summary
HashCache(HashCache.ObjectManager om, int maxSize)
          Create a new HashCache.
 
Method Summary
 void clear()
          Cleans out the data structure, calling the clean-up on all items.
 java.lang.Object get(java.lang.Object key)
          Retrieves an item from the cache with the given key.
 int getMaxSize()
          Retrieves the maximum cache size.
 int getOverflowCount()
          Retrieves the metric that corresponds to the number of objects that were removed from the cache.
 int getSize()
          Retrieves the current number of elements in the cache.
 void resetOverflowCount()
          Resets the overflow count metric.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HashCache

public HashCache(HashCache.ObjectManager om,
                 int maxSize)
Create a new HashCache.

Parameters:
om - the manager for creating and cleaning up the objects. This cannot be null.
maxSize - the maximum size of the cache. This must be > 1 (this is for performance reasons).
Method Detail

get

public java.lang.Object get(java.lang.Object key)
Retrieves an item from the cache with the given key. If the object doesn't exist, then it is created, added to the cache, and returned. If it does exist, the cached version is returned and marked as the most recently found. If it was created, and the cache is full, then the oldest retrieved object is removed and cleaned up.


getSize

public int getSize()
Retrieves the current number of elements in the cache.

Returns:
the current size of the cache.

getMaxSize

public int getMaxSize()
Retrieves the maximum cache size.

Returns:
the maximum cache size.

getOverflowCount

public int getOverflowCount()
Retrieves the metric that corresponds to the number of objects that were removed from the cache.

Returns:
the overflow count.

resetOverflowCount

public void resetOverflowCount()
Resets the overflow count metric.


clear

public void clear()
Cleans out the data structure, calling the clean-up on all items. The manager and max size will not be changed, nor will the overflow count.



Copyright © 2001-2003 by The GroboUtils Project