org.enhydra.shark.utilities
Class LRUMap

java.lang.Object
  extended by org.enhydra.shark.utilities.SequencedHashMap
      extended by org.enhydra.shark.utilities.LRUMap
All Implemented Interfaces:
java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, java.util.Map

public class LRUMap
extends org.enhydra.shark.utilities.SequencedHashMap
implements java.io.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 LRUCache does use a true LRU algorithm. The keys for all gets and puts are moved to the front of the list. LRUCache is now a subclass of SequencedHashMap, and the "LRU" key is now equivalent to LRUCache.getFirst().

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

Constructor Summary
LRUMap()
          Default constructor, primarily for the purpose of de-externalization.
LRUMap(int maxSize)
          Creates a new LRUMap with a maximum capacity of maxSize and with a default capacity (16) and load factor (0.75).
LRUMap(int initialCapacity, float loadFactor)
          Create a new LRUCache with a default maximum capacity (0) and with the capacity initialCapacity and load factor loadFactor.
LRUMap(int initialCapacity, float loadFactor, int maxSize)
          Create a new LRUCache with a maximum capacity of maxSize and with the capacity initialCapacity and load factor loadFactor.
LRUMap(java.util.Map m)
          Create a new LRUCache with the same mappings as the specified map m.
LRUMap(java.util.Map m, int maxSize)
          Create a new LRUCache with the same mappings as the specified map m.
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Returns true if this LRUCache contains a mapping for the specified key.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which the specified key is mapped in this LRUCache, or null if the map contains no mapping for this key.
 int getMaximumSize()
          Getter for property maximumSize.
protected  void processRemovedLRU(java.lang.Object key, java.lang.Object value)
          Subclasses of LRUCache may hook into this method to provide specialized actions whenever an Object is automatically removed from the cache.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this LRUCache.
 void putAll(java.util.Map t)
          Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per Map.putAll(Map)).
 void readExternal(java.io.ObjectInput in)
           
protected  void removeLRU()
          This method is used internally by the class for finding and removing the LRU Object.
 void setMaximumSize(int maxSize)
          Setter for property maximumSize.
 void writeExternal(java.io.ObjectOutput out)
           
 
Methods inherited from class org.enhydra.shark.utilities.SequencedHashMap
clear, clone, containsValue, entrySet, equals, get, getFirst, getFirstKey, getFirstValue, getLast, getLastKey, getLastValue, getValue, hashCode, indexOf, isEmpty, iterator, keySet, lastIndexOf, remove, remove, sequence, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LRUMap

public LRUMap()
Default constructor, primarily for the purpose of de-externalization. This constructors sets a default cache with maximum size set to 0 (cache is not used at the moment) and with a default capacity (16) and load factor (0.75).


LRUMap

public LRUMap(int maxSize)
Creates a new LRUMap with a maximum capacity of maxSize and with a default capacity (16) and load factor (0.75). Once maxSize capacity is achieved, subsequent gets and puts will push keys out of the map.

Parameters:
maxSize - Maximum capacity of the LRUMap.

LRUMap

public LRUMap(int initialCapacity,
              float loadFactor)
Create a new LRUCache with a default maximum capacity (0) and with the capacity initialCapacity and load factor loadFactor.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.

LRUMap

public LRUMap(int initialCapacity,
              float loadFactor,
              int maxSize)
Create a new LRUCache with a maximum capacity of maxSize and with the capacity initialCapacity and load factor loadFactor. Once maxSize capacity is achieved, subsequent gets and puts will push keys out of the map.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
maxSize - Maximum capacity of the LRUMap.

LRUMap

public LRUMap(java.util.Map m)
Create a new LRUCache with the same mappings as the specified map m. The LRUCache instance is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.

Parameters:
m - The map whose mappings are to be placed in this LRUCache.

LRUMap

public LRUMap(java.util.Map m,
              int maxSize)
Create a new LRUCache with the same mappings as the specified map m. The LRUCache instance is created with default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map. The attribute maximumSize is set to value maxSize if that value is greater than the size of the map m.

Parameters:
m - The map whose mappings are to be placed in this LRUCache.
maxSize - Maximum capacity of the LRUMap.
Method Detail

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this LRUCache contains a mapping for the specified key.

Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class org.enhydra.shark.utilities.SequencedHashMap
Parameters:
key - The key whose presence in this LRUCache is to be tested.
Returns:
true if this LRUCache contains a mapping for the specified key.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which the specified key is mapped in this LRUCache, or null if the map contains no mapping for this key. If found, 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 java.util.Map
Overrides:
get in class org.enhydra.shark.utilities.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.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Associates the specified value with the specified key in this LRUCache. If the map previously contained a mapping for this key, the old value is replaced.

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

Specified by:
put in interface java.util.Map
Overrides:
put in class org.enhydra.shark.utilities.SequencedHashMap
Parameters:
key - Key with which the specified value is to be associated.
value - Value to be associated with the specified key.
Returns:
Former value of the key.

putAll

public void putAll(java.util.Map t)
Adds all the mappings in the specified map to this map, replacing any mappings that already exist (as per Map.putAll(Map)). The order in which the entries are added is determined by the iterator returned from Map.entrySet() for the specified map.

Specified by:
putAll in interface java.util.Map
Overrides:
putAll in class org.enhydra.shark.utilities.SequencedHashMap
Parameters:
t - the mappings that should be added to this map.
Throws:
java.lang.NullPointerException - if t is null.

removeLRU

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


processRemovedLRU

protected void processRemovedLRU(java.lang.Object key,
                                 java.lang.Object value)
Subclasses of LRUCache 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).

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Specified by:
readExternal in interface java.io.Externalizable
Overrides:
readExternal in class org.enhydra.shark.utilities.SequencedHashMap
Throws:
java.io.IOException
java.lang.ClassNotFoundException

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Specified by:
writeExternal in interface java.io.Externalizable
Overrides:
writeExternal in class org.enhydra.shark.utilities.SequencedHashMap
Throws:
java.io.IOException

getMaximumSize

public int getMaximumSize()
Getter for property maximumSize.

Returns:
Value of property maximumSize.

setMaximumSize

public void setMaximumSize(int maxSize)
Setter for property maximumSize.

Parameters:
maxSize - New value of property maximumSize.