|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.collections.map.AbstractHashedMap | +--org.apache.commons.collections.map.AbstractLinkedMap | +--org.apache.commons.collections.map.LRUMap
A Map
implementation with a fixed maximum size which removes
the least recently used entry if an entry is added when full.
The least recently used algorithm works on the get and put operations only. Iteration of any kind, including setting the value by iteration, does not change the order. Queries such as containsKey and containsValue or access via views also do not change the order.
The map implements OrderedMap
and entries may be queried using
the bidirectional OrderedMapIterator
. The order returned is
least recently used to most recently used. Iterators from map views can
also be cast to OrderedIterator
if required.
All the available iterators can be reset back to the start by casting to
ResettableIterator
and calling reset()
.
Inner classes inherited from class org.apache.commons.collections.map.AbstractLinkedMap |
AbstractLinkedMap.EntrySetIterator, AbstractLinkedMap.KeySetIterator, AbstractLinkedMap.LinkEntry, AbstractLinkedMap.LinkIterator, AbstractLinkedMap.LinkMapIterator, AbstractLinkedMap.ValuesIterator |
Inner classes inherited from class org.apache.commons.collections.map.AbstractHashedMap |
AbstractHashedMap.EntrySet, AbstractHashedMap.EntrySetIterator, AbstractHashedMap.HashEntry, AbstractHashedMap.HashIterator, AbstractHashedMap.HashMapIterator, AbstractHashedMap.KeySet, AbstractHashedMap.KeySetIterator, AbstractHashedMap.Values, AbstractHashedMap.ValuesIterator |
Inner classes inherited from class java.util.Map |
Map.Entry |
Field Summary | |
protected static int |
DEFAULT_MAX_SIZE
Default maximum size |
Fields inherited from class org.apache.commons.collections.map.AbstractLinkedMap |
header |
Fields inherited from class org.apache.commons.collections.map.AbstractHashedMap |
data, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD, entrySet, GETKEY_INVALID, GETVALUE_INVALID, keySet, loadFactor, MAXIMUM_CAPACITY, modCount, NO_NEXT_ENTRY, NO_PREVIOUS_ENTRY, NULL, REMOVE_INVALID, SETVALUE_INVALID, size, threshold, values |
Constructor Summary | |
LRUMap()
Constructs a new empty map with a maximum size of 100. |
|
LRUMap(int maxSize)
Constructs a new, empty map with the specified maximum size. |
|
LRUMap(int maxSize,
float loadFactor)
Constructs a new, empty map with the specified initial capacity and load factor. |
|
LRUMap(Map map)
Constructor copying elements from another map. |
Method Summary | |
protected void |
addMapping(int hashIndex,
int hashCode,
Object key,
Object value)
Adds a new key-value mapping into this map. |
Object |
clone()
Clones the map without cloning the keys or values. |
protected void |
doReadObject(ObjectInputStream in)
Reads the data necessary for put() to work in the superclass. |
protected void |
doWriteObject(ObjectOutputStream out)
Writes the data necessary for put() to work in deserialization. |
Object |
get(Object key)
Gets the value mapped to the key specified. |
boolean |
isFull()
Returns true if this map is full and no new mappings can be added. |
int |
maxSize()
Gets the maximum size of the map (the bound). |
protected void |
moveToMRU(AbstractLinkedMap.LinkEntry entry)
Moves an entry to the MRU position at the end of the list. |
protected boolean |
removeLRU(AbstractLinkedMap.LinkEntry entry)
Subclass method to control removal of the least recently used entry from the map. |
protected void |
reuseMapping(AbstractLinkedMap.LinkEntry entry,
int hashIndex,
int hashCode,
Object key,
Object value)
Reuses an entry by removing it and moving it to a new place in the map. |
protected void |
updateEntry(AbstractHashedMap.HashEntry entry,
Object newValue)
Updates an existing key-value mapping. |
Methods inherited from class org.apache.commons.collections.map.AbstractLinkedMap |
addEntry, clear, containsValue, createEntry, createEntrySetIterator, createKeySetIterator, createValuesIterator, firstKey, getEntry, init, lastKey, mapIterator, nextKey, orderedMapIterator, previousKey, removeEntry |
Methods inherited from class org.apache.commons.collections.map.AbstractHashedMap |
calculateNewCapacity, calculateThreshold, checkCapacity, containsKey, convertKey, destroyEntry, ensureCapacity, entrySet, equals, getEntry, hash, hashCode, hashIndex, isEmpty, isEqualKey, isEqualValue, keySet, put, putAll, remove, removeMapping, reuseEntry, size, toString, values |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Map |
clear, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values |
Field Detail |
protected static final int DEFAULT_MAX_SIZE
Constructor Detail |
public LRUMap()
public LRUMap(int maxSize)
maxSize
- the maximum size of the mapIllegalArgumentException
- if the maximum size is less than onepublic LRUMap(int maxSize, float loadFactor)
maxSize
- the maximum size of the map, -1 for no limit,loadFactor
- the load factorIllegalArgumentException
- if the maximum size is less than oneIllegalArgumentException
- if the load factor is less than zeropublic LRUMap(Map map)
The maximum size is set from the map's size.
map
- the map to copyNullPointerException
- if the map is nullIllegalArgumentException
- if the map is emptyMethod Detail |
public Object get(Object key)
This operation changes the position of the key in the map to the most recently used position (first).
get
in interface Map
get
in class AbstractHashedMap
key
- the keyprotected void moveToMRU(AbstractLinkedMap.LinkEntry entry)
entry
- the entry to updateprotected void updateEntry(AbstractHashedMap.HashEntry entry, Object newValue)
updateEntry
in class AbstractHashedMap
entry
- the entry to updatenewValue
- the new value to storeprotected void addMapping(int hashIndex, int hashCode, Object key, Object value)
addMapping
in class AbstractHashedMap
hashIndex
- the index into the data array to store athashCode
- the hash code of the key to addkey
- the key to addvalue
- the value to addprotected void reuseMapping(AbstractLinkedMap.LinkEntry entry, int hashIndex, int hashCode, Object key, Object value)
entry
- the entry to reusehashIndex
- the index into the data array to store athashCode
- the hash code of the key to addkey
- the key to addvalue
- the value to addprotected boolean removeLRU(AbstractLinkedMap.LinkEntry entry)
This method exists for subclasses to override. A subclass may wish to provide cleanup of resources when an entry is removed. For example:
protected boolean removeLRU(LinkEntry entry) { releaseResources(entry.getValue()); // release resources held by entry return true; // actually delete entry }
Alternatively, a subclass may choose to not remove the entry or selectively keep certain LRU entries. For example:
protected boolean removeLRU(LinkEntry entry) { if (entry.getKey().toString().startsWith("System.")) { return false; // entry not removed from LRUMap } else { return true; // actually delete entry } }Note that the effect of not removing an LRU is for the Map to exceed the maximum size.
entry
- the entry to be removedpublic boolean isFull()
isFull
in interface BoundedMap
true
if the map is fullpublic int maxSize()
maxSize
in interface BoundedMap
public Object clone()
clone
in class AbstractHashedMap
protected void doWriteObject(ObjectOutputStream out) throws IOException
put()
to work in deserialization.doWriteObject
in class AbstractHashedMap
org.apache.commons.collections.map.AbstractHashedMap
out
- the output streamprotected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException
put()
to work in the superclass.doReadObject
in class AbstractHashedMap
org.apache.commons.collections.map.AbstractHashedMap
in
- the input stream
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |