org.j4me.collections
Class Cache

java.lang.Object
  extended by org.j4me.collections.Cache

public class Cache
extends java.lang.Object

A least recently used (LRU) cache. Data is stored internally in a hashtable, which maps keys to values. Once the cache is full and a new entry is added, the least recently used entry is discarded. Therefore a cache is like a hashtable except it stops growing at a certain point.

Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a cache, the objects used as keys must implement the hashCode method and the equals method.

See Also:
Hashtable

Constructor Summary
Cache(int maxCapacity)
          Constructs the cache.
 
Method Summary
 void add(java.lang.Object key, java.lang.Object data)
          Adds an Object to the cache that is associated with key.
 void clear()
          Clears this cache so that it contains no keys.
 java.lang.Object get(java.lang.Object key)
          Gets a cached Object associated with key.
 int getMaxCapacity()
          Returns the maximum number of keys that can be stored in this cache.
 void setMaxCapacity(int maxCapacity)
          Sets the maximum number of keys that can be stored in this cache.
 int size()
          Returns the number of keys in this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(int maxCapacity)
Constructs the cache.

Parameters:
maxCapacity - is the number of key/value pairs that can be stored before adding new entries ejects the least recently used ones.
Method Detail

clear

public void clear()
Clears this cache so that it contains no keys.


size

public int size()
Returns the number of keys in this cache.

Returns:
The number of keys in this cache.

getMaxCapacity

public int getMaxCapacity()
Returns the maximum number of keys that can be stored in this cache. The value of size can never be greater than this number.

Returns:
The maximum number of keys that can be stored in this cache.

setMaxCapacity

public void setMaxCapacity(int maxCapacity)
Sets the maximum number of keys that can be stored in this cache.

The value of size can never be greater than this number. If the maximum capicity is shrinking and too many elements are already in the cache, the least recently used ones will be discarded until size is the same as maxCapacity.

Parameters:
maxCapacity - is the total number of keys that can be stored in the cache.

add

public void add(java.lang.Object key,
                java.lang.Object data)
Adds an Object to the cache that is associated with key. The new item will become the most recently used. If the cache is full it will replace the least recently used entry.

Parameters:
key - is the indexing object.
data - is the object to cache.

get

public java.lang.Object get(java.lang.Object key)
Gets a cached Object associated with key.

Parameters:
key - is the indexing object.
Returns:
The Object associated with key; null if key is not in the cache.