org.apache.commons.collections
Class LRUMap

java.lang.Object
  |
  +--org.apache.commons.collections.SequencedHashMap
        |
        +--org.apache.commons.collections.LRUMap
All Implemented Interfaces:
Cloneable, Externalizable, Map, Serializable

public class LRUMap
extends SequencedHashMap
implements Externalizable

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 that 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().

Author:
James Strachan, Morgan Delagrange
See Also:
Serialized Form

Inner classes inherited from class java.util.Map
Map.Entry
 
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
 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)
           
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)
           
 
Methods inherited from class org.apache.commons.collections.SequencedHashMap
clear, clone, containsKey, containsValue, entrySet, get, get, getFirst, getFirstKey, getFirstValue, getLast, getLastKey, getLastValue, getValue, indexOf, isEmpty, iterator, keySet, lastIndexOf, putAll, remove, remove, sequence, size, values
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

LRUMap

public LRUMap()
Default constructor, primarily for the purpose of de-externalization. This constructors sets a default LRU limit of 100 keys, but this value may be overridden internally as a result of de-externalization.

LRUMap

public LRUMap(int i)
Create a new LRUMap with a maximum capacity of i. Once i capacity is achieved, subsequent gets and puts will push keys out of the map. See .
Parameters:
i - Maximum capacity of the LRUMap
Method Detail

getMaximumSize

public int getMaximumSize()
Getter for property maximumSize.
Returns:
Value of property maximumSize.

processRemovedLRU

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. By default, this method does nothing.
Parameters:
key - key that was removed
value - value of that key (can be null)

put

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.)

Overrides:
put in class SequencedHashMap
Parameters:
key - Key of the Object to add.
value - Object to add
Returns:
Former value of the key
See Also:
removeLRU()

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
Overrides:
readExternal in class SequencedHashMap

removeLRU

protected void removeLRU()
This method is used internally by the class for finding and removing the LRU Object.

setMaximumSize

public void setMaximumSize(int maximumSize)
Setter for property maximumSize.
Parameters:
maximumSize - New value of property maximumSize.

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Overrides:
writeExternal in class SequencedHashMap


Copyright © 2001 Apache Software Foundation. Documenation generated April 2 2002.