|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.j4me.collections.Cache
public class Cache
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.
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 |
---|
public Cache(int maxCapacity)
maxCapacity
- is the number of key/value pairs that can be stored
before adding new entries ejects the least recently used ones.Method Detail |
---|
public void clear()
public int size()
public int getMaxCapacity()
size
can never be greater than this
number.
public void setMaxCapacity(int maxCapacity)
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
.
maxCapacity
- is the total number of keys that can be
stored in the cache.public void add(java.lang.Object key, java.lang.Object data)
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.
key
- is the indexing object.data
- is the object to cache.public java.lang.Object get(java.lang.Object key)
Object
associated with key
.
key
- is the indexing object.
Object
associated with key
; null
if
key
is not in the cache.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |