FileDocCategorySizeDatePackage
LRUMap.javaAPI DocApache Axis 1.46701Sat Apr 22 18:57:28 BST 2006org.apache.axis.collections

LRUMap

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
author
James Strachan
author
Morgan Delagrange

Fields Summary
private int
maximumSize
private static final long
serialVersionUID
Constructors Summary
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.


                                        
      
        this( 100 );
    
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 .

param
i Maximum capacity of the LRUMap

        super( i );
        maximumSize = i;
    
Methods Summary
public java.lang.Objectget(java.lang.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.

param
key Key to retrieve
return
Returns the value. Returns null if the key has a null value or if the key has no value.

        if(!containsKey(key)) return null;

        Object value = remove(key);
        super.put(key,value);
        return value;
    
public intgetMaximumSize()
Getter for property maximumSize.

return
Value of property maximumSize.

        return maximumSize;
    
protected voidprocessRemovedLRU(java.lang.Object key, java.lang.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.

param
key key that was removed
param
value value of that key (can be null)

    
public java.lang.Objectput(java.lang.Object key, java.lang.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.)

param
key Key of the Object to add.
param
value Object to add
return
Former value of the key


        int mapSize = size();
        Object retval = null;

        if ( mapSize >= maximumSize ) {

            // don't retire LRU if you are just
            // updating an existing key
            if (!containsKey(key)) {
                // lets retire the least recently used item in the cache
                removeLRU();
            }
        }

        retval = super.put(key,value);

        return retval;
    
public voidreadExternal(java.io.ObjectInput in)

        maximumSize = in.readInt();
        int size = in.readInt();
        
        for( int i = 0; i < size; i++ )  {
            Object key = in.readObject();
            Object value = in.readObject();
            put(key,value);
        }
    
protected voidremoveLRU()
This method is used internally by the class for finding and removing the LRU Object.

        Object key = getFirstKey();
        // be sure to call super.get(key), or you're likely to 
        // get infinite promotion recursion
        Object value = super.get(key);
        
        remove(key);

        processRemovedLRU(key,value);
    
public voidsetMaximumSize(int maximumSize)
Setter for property maximumSize.

param
maximumSize New value of property maximumSize.

        this.maximumSize = maximumSize;
        while (size() > maximumSize) {
            removeLRU();
        }
    
public voidwriteExternal(java.io.ObjectOutput out)

        out.writeInt( maximumSize );
        out.writeInt( size() );
        for( Iterator iterator = keySet().iterator(); iterator.hasNext(); ) {
            Object key = iterator.next();
            out.writeObject( key );
            // be sure to call super.get(key), or you're likely to 
            // get infinite promotion recursion
            Object value = super.get( key );
            out.writeObject( value );
        }