|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.enhydra.shark.utilities.SequencedHashMap
org.enhydra.shark.utilities.LRUMap
public class 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 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().
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 |
---|
public LRUMap()
public LRUMap(int maxSize)
maxSize
- Maximum capacity of the LRUMap.public LRUMap(int initialCapacity, float loadFactor)
initialCapacity
- The initial capacity.loadFactor
- The load factor.public LRUMap(int initialCapacity, float loadFactor, int maxSize)
initialCapacity
- The initial capacity.loadFactor
- The load factor.maxSize
- Maximum capacity of the LRUMap.public LRUMap(java.util.Map m)
m
- The map whose mappings are to be placed in this LRUCache.public LRUMap(java.util.Map m, int maxSize)
m
- The map whose mappings are to be placed in this LRUCache.maxSize
- Maximum capacity of the LRUMap.Method Detail |
---|
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
containsKey
in class org.enhydra.shark.utilities.SequencedHashMap
key
- The key whose presence in this LRUCache is to be tested.
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
get
in class org.enhydra.shark.utilities.SequencedHashMap
key
- Key to retrieve.
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
(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 java.util.Map
put
in class org.enhydra.shark.utilities.SequencedHashMap
key
- Key with which the specified value is to be associated.value
- Value to be associated with the specified key.
public void putAll(java.util.Map t)
Map.putAll(Map)
). The order
in which the entries are added is determined by the iterator returned
from Map.entrySet()
for the specified map.
putAll
in interface java.util.Map
putAll
in class org.enhydra.shark.utilities.SequencedHashMap
t
- the mappings that should be added to this map.
java.lang.NullPointerException
- if t
is null
.protected void removeLRU()
protected void processRemovedLRU(java.lang.Object key, java.lang.Object value)
key
- Key that was removed.value
- Value of that key (can be null).public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException
readExternal
in interface java.io.Externalizable
readExternal
in class org.enhydra.shark.utilities.SequencedHashMap
java.io.IOException
java.lang.ClassNotFoundException
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
writeExternal
in interface java.io.Externalizable
writeExternal
in class org.enhydra.shark.utilities.SequencedHashMap
java.io.IOException
public int getMaximumSize()
public void setMaximumSize(int maxSize)
maxSize
- New value of property maximumSize.
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |