|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.collections.SequencedHashMap | +--org.apache.commons.collections.LRUMap
An implementation of a Map which has a maximum size and uses a Least Recently Used algorithm to remove items from the Map when the maximum size is reached and new items are added.
A synchronized version can be obtained with:
Collections.synchronizedMap( theMapToSynchronize )
If it will be accessed by multiple threads, you _must_ synchronize access
to this Map. Even concurrent get(Object) operations produce indeterminate
behaviour.
Unlike the Collections 1.0 version, this version of LRUMap does use a true LRU algorithm. The keys for all gets and puts are moved to the front of the list. LRUMap is now a subclass of SequencedHashMap, and the "LRU" key is now equivalent to LRUMap.getFirst().
Constructor Summary | |
LRUMap()
Default constructor, primarily for the purpose of de-externalization. |
|
LRUMap(int i)
Create a new LRUMap with a maximum capacity of i. |
Method Summary | |
Object |
get(Object key)
Get the value for a key from the Map. |
int |
getMaximumSize()
Getter for property maximumSize. |
protected void |
processRemovedLRU(Object key,
Object value)
Subclasses of LRUMap may hook into this method to provide specialized actions whenever an Object is automatically removed from the cache. |
Object |
put(Object key,
Object value)
Removes the key and its Object from the Map. |
void |
readExternal(ObjectInput in)
Deserializes this map from the given stream. |
protected void |
removeLRU()
This method is used internally by the class for finding and removing the LRU Object. |
void |
setMaximumSize(int maximumSize)
Setter for property maximumSize. |
void |
writeExternal(ObjectOutput out)
Serializes this map to the given stream. |
Methods inherited from class org.apache.commons.collections.SequencedHashMap |
clear, clone, containsKey, containsValue, entrySet, equals, get, getFirst, getFirstKey, getFirstValue, getLast, getLastKey, getLastValue, getValue, hashCode, indexOf, isEmpty, iterator, keySet, lastIndexOf, putAll, remove, remove, sequence, size, toString, values |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public LRUMap()
public LRUMap(int i)
i
- Maximum capacity of the LRUMapMethod Detail |
public Object get(Object key)
Get the value for a key from the Map. The key will be promoted to the Most Recently Used position. Note that get(Object) operations will modify the underlying Collection. Calling get(Object) inside of an iteration over keys, values, etc. is currently unsupported.
get
in interface Map
get
in class SequencedHashMap
key
- Key to retrieve
public int getMaximumSize()
protected void processRemovedLRU(Object key, Object value)
key
- key that was removedvalue
- value of that key (can be null)public Object put(Object key, Object value)
Removes the key and its Object from the Map.
(Note: this may result in the "Least Recently Used" object being removed from the Map. In that case, the removeLRU() method is called. See javadoc for removeLRU() for more details.)
put
in interface Map
put
in class SequencedHashMap
key
- Key of the Object to add.value
- Object to add
removeLRU()
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
SequencedHashMap
readExternal
in interface Externalizable
readExternal
in class SequencedHashMap
in
- the stream to deserialize from
ClassNotFoundException
- if the stream raises it
IOException
- if the stream raises itprotected void removeLRU()
public void setMaximumSize(int maximumSize)
maximumSize
- New value of property maximumSize.public void writeExternal(ObjectOutput out) throws IOException
SequencedHashMap
writeExternal
in interface Externalizable
writeExternal
in class SequencedHashMap
out
- the stream to serialize to
IOException
- if the stream raises it
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |