com.cafesoft.core.util
Class LruCache

java.lang.Object
  extended by com.cafesoft.core.util.LruCache

public class LruCache
extends Object

Implements a "Least Recently Used" object cache. The cache is limited to a configureable maxiumum number of objects, which are accessible by key. If a new object is added to the cache, but the cache is full, then the least recently used object is removed and the new one is added.

Since:
12/4/2002

Constructor Summary
LruCache(int maxSize)
          Creates a new LruCache.
 
Method Summary
 Object add(String key, Object newObject)
          Add a cached object.
 void clear()
          Clear the cache.
 void dump(PrintStream out)
          Dump the LruCache to the specified PrintStream.
 Object get(String key)
          Get a cached object by key.
 int getMaxSize()
          Get the maxium cache size.
 int getSize()
          Get the current cache size.
 Iterator keys()
          Get an Iterator of all keys in the cache.
static void main(String[] arg)
          Test a LruCache.
 Object remove(String key)
          Remove a cached object by key.
 Iterator values()
          Get an Iterator of all values in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LruCache

public LruCache(int maxSize)
Creates a new LruCache.

Parameters:
maxSize - the maxiumum size of the cache
Method Detail

getMaxSize

public int getMaxSize()
Get the maxium cache size.

Returns:
the maximum cache size.

getSize

public int getSize()
Get the current cache size.

Returns:
the current cache size.

get

public Object get(String key)
Get a cached object by key.

If the object is cached, it becomes the "Most Recently Used" object.

Parameters:
key - The key by which the cached object is hashed.
Returns:
the object if it exists or null if it does not exist.

keys

public Iterator keys()
Get an Iterator of all keys in the cache.

Returns:
an Iterator of all keys in the cache.

values

public Iterator values()
Get an Iterator of all values in the cache.

Returns:
an Iterator of all values in the cache (from most recently used to least recently used).

add

public Object add(String key,
                  Object newObject)
Add a cached object.
  1. If an object with the same key is already cached, then the previously cached object is removed and the new one is added.
  2. If the cache is full, then remove the "least recently used" cached object before inserting the newly cached object.

The newly added object becomes the "most recently used".

Parameters:
key - the key by which the cached object is hashed.
newObject - the object to be newly cached.
Returns:
a previously cached object if it corresponds to the same key or if it must be removed from the cache to make room for thew newly-added object.

remove

public Object remove(String key)
Remove a cached object by key.

Parameters:
key - the key associated with the cached object.
Returns:
the removed cached object or null if it did not exist.

clear

public void clear()
Clear the cache.

References to cached objects are removed.


dump

public void dump(PrintStream out)
Dump the LruCache to the specified PrintStream.

Parameters:
out - the PrintSteam.

main

public static void main(String[] arg)
Test a LruCache.



Generated on 10:38:35 AM April 14, 2011, © 1996-2010 Cafésoft LLC. All rights reserved.