org.apache.commons.collections

Class LRUMap

Implemented Interfaces:
Cloneable, Externalizable, Map<K,V>, Serializable
Known Direct Subclasses:
JRAbstractLRUVirtualizer.Cache.LRUScanMap

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

Since:
Commons Collections 1.0
See Also:
Serialized Form

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)
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays.
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)
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.

Methods inherited from class org.apache.commons.collections.SequencedHashMap

clear, clone, containsKey, containsValue, entrySet, equals, get, get, getFirst, getFirstKey, getFirstValue, getLast, getLastKey, getLastValue, getValue, hashCode, indexOf, isEmpty, iterator, keySet, lastIndexOf, put, putAll, readExternal, remove, remove, sequence, size, toString, values, writeExternal

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

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.
Usages and Demos :

View More Examples of LRUMap()
   1:         else
   2:             dataMap = new LRUMap();
   3:     }
   4: 
   5:     public synchronized void clear() {

View Full Code Here

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
Usages and Demos :

View More Examples of LRUMap(int i)
   1:     public CacheProvider(int size) {
   2:         uriCache = Collections.synchronizedMap(new LRUMap(size));
   3:         ...
   4:         uriIdCache = Collections.synchronizedMap(new LRUMap(size));
   5:         ...
   6:         varCache = Collections.synchronizedMap(new LRUMap(size));
   7:         varIdCache = Collections.synchronizedMap(new LRUMap(size));

View Full Code Here
   1:     public CacheProvider(int size) {
   2:         uriCache = Collections.synchronizedMap(new LRUMap(size));
   3:         ...
   4:         uriIdCache = Collections.synchronizedMap(new LRUMap(size));
   5:         ...
   6:         varCache = Collections.synchronizedMap(new LRUMap(size));
   7:         varIdCache = Collections.synchronizedMap(new LRUMap(size));

View Full Code Here
   1:     transient private Date lastVisitDate;
   2:     transient private Map searchTextMap = new LRUMap(100);
   3:     transient private IssueIdListHolder issueListHolder = new IssueIdListHolder();
   4: 
   5:     transient private static UserContainer anonymousUserContainer;

View Full Code Here
   1:             hash = new HashMap();
   2:             hardCacheMap = new LRUMap(hardSize);
   3:             queue = new ReferenceQueue();
   4:         }

View Full Code Here
   1:         if (_cache == null) {
   2:             _cache = new LRUMap(convertersConfig.getCacheSize());
   3:         }
   4: 
   5:         CacheKey key = new CacheKey(name, sourceClass, targetClass);

View Full Code Here

Method Details

get

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.
Specified by:
get in interface Map<K,V>
Overrides:
get in interface SequencedHashMap
Parameters:
key - Key to retrieve
Returns:
Returns the value. Returns null if the key has a null value or if the key has no value.
Usages and Demos :

View More Examples of get(Object key)
   1: 
   2: import org.apache.commons.collections.LRUMap;
   3: import org.apache.ojb.broker.Identity;
   4:         ...
   5:         {
   6:             if(cache.get(oid) == null)
   7:             {
   8:         ...
   9:     {
  10:         return cache.get(oid);
  11:     }
  12:         ...
  13:         private HashMap hash;
  14:         private LRUMap hardCacheMap;

View Full Code Here

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 interface SequencedHashMap
Parameters:
key - Key of the Object to add.
value - Object to add
Returns:
Former value of the key
Usages and Demos :

View More Examples of put(Object key,Object value)
   1: 
   2: import org.apache.commons.collections.LRUMap;
   3: import org.apache.ojb.broker.Identity;
   4:         ...
   5:         {
   6:             cache.put(oid, obj);
   7:         }
   8:         ...
   9:             {
  10:                 cache.put(oid, obj);
  11:                 return true;
  12:         ...
  13:         private HashMap hash;
  14:         private LRUMap hardCacheMap;

View Full Code Here
   1: 
   2: import org.apache.commons.collections.LRUMap;
   3: import org.apache.commons.collections.primitives.ArrayIntList;
   4:         ...
   5:     private void createRowCache() {
   6:         _rowCache = new LRUMap(100);
   7:     }
   8:         ...
   9:         if(null != _rowCache) {
  10:             _rowCache.put(rowid,row);
  11:         }
  12:         ...
  13:     private int _savedAtModCount = 0;
  14:     private LRUMap _rowCache = null;

View Full Code Here

readExternal

public void readExternal(ObjectInput in)
            throws IOException,
                   ClassNotFoundException
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.
Specified by:
readExternal in interface Externalizable
Overrides:
readExternal in interface SequencedHashMap
Parameters:
in - the stream to read data from in order to restore the object
Throws:
IOException - if I/O errors occur
ClassNotFoundException - If the class for an object being restored cannot be found.

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
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.
Specified by:
writeExternal in interface Externalizable
Overrides:
writeExternal in interface SequencedHashMap
Parameters:
out - the stream to write the object to
Throws:
IOException - Includes any I/O exceptions that may occur