Package com.twelvemonkeys.util
Class LRUMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
com.twelvemonkeys.util.LinkedMap<K,V>
com.twelvemonkeys.util.LRUMap<K,V>
- All Implemented Interfaces:
ExpiringMap<K,
,V> Serializable
,Cloneable
,Map<K,
V>
Map implementation with size limit, that keeps its entries in LRU
(least recently used) order, also known as access-order.
When the size limit is reached, the least recently accessed mappings are
removed. The number of mappings to be removed from the map, is
controlled by the trim factor.
- Default size limit is 1000 elements.
See
setMaxSize(int)
/getMaxSize()
. - Default trim factor is 1% (
0.01f
). SeesetTrimFactor(float)
/getTrimFactor()
.
- Version:
- $Id: com/twelvemonkeys/util/LRUMap.java#1 $
- Author:
- Harald Kuhr
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.twelvemonkeys.util.LinkedMap
LinkedMap.LinkedEntry<K,
V> Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,
V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
FieldsFields inherited from class com.twelvemonkeys.util.LinkedMap
accessOrder
-
Constructor Summary
ConstructorsConstructorDescriptionLRUMap()
Creates an LRUMap with default max size (1000 entries).LRUMap
(int pMaxSize) Creates an LRUMap with the given max size.Creates an LRUMap with initial mappings from the given map, and default max size (1000 entries).Creates an LRUMap with initial mappings from the given map, and the given max size.Creates an LRUMap with initial mappings from the given map, and the given max size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object pKey) entrySet()
int
Returns the maximum number of mappings in this map.float
Returns the current trim factor.boolean
isEmpty()
keySet()
void
processRemoved
(Map.Entry<K, V> pRemoved) Default implementation does nothing.protected boolean
removeEldestEntry
(Map.Entry pEldest) always returnsfalse
, and instead invokesremoveLRU()
ifsize >= maxSize
.removeEntry
(Map.Entry<K, V> pEntry) Removes the given entry from the Map.void
Removes the least recently used mapping(s) from this map.void
setMaxSize
(int pMaxSize) Sets the maximum number of elements in this map.void
setTrimFactor
(float pTrimFactor) Sets the trim factor.int
size()
values()
Methods inherited from class com.twelvemonkeys.util.LinkedMap
clone, containsValue, get, init, newEntryIterator, newKeyIterator, newValueIterator, put, remove
Methods inherited from class java.util.AbstractMap
equals, hashCode, putAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Field Details
-
entries
-
modCount
protected transient volatile int modCount
-
-
Constructor Details
-
LRUMap
public LRUMap()Creates an LRUMap with default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in theMap
interface.- See Also:
-
LRUMap
public LRUMap(int pMaxSize) Creates an LRUMap with the given max size.- Parameters:
pMaxSize
- size limit
-
LRUMap
Creates an LRUMap with initial mappings from the given map, and default max size (1000 entries). This is constructor is here to comply with the reccomendations for "standard" constructors in theMap
interface.- Parameters:
pContents
- the map whose mappings are to be placed in this map. May benull
.- See Also:
-
LRUMap
Creates an LRUMap with initial mappings from the given map, and the given max size.- Parameters:
pContents
- the map whose mappings are to be placed in this map. May benull
.pMaxSize
- size limit
-
LRUMap
Creates an LRUMap with initial mappings from the given map, and the given max size.- Parameters:
pBacking
- the backing map of this map. Must be either empty, or the same map aspContents
.pContents
- the map whose mappings are to be placed in this map. May benull
.pMaxSize
- max size
-
-
Method Details
-
getMaxSize
public int getMaxSize()Returns the maximum number of mappings in this map.- Returns:
- the size limit
-
setMaxSize
public void setMaxSize(int pMaxSize) Sets the maximum number of elements in this map. If the current size is greater than the new max size, the map will be trimmed to fit the new max size constraint.- Parameters:
pMaxSize
- new size limit- See Also:
-
getTrimFactor
public float getTrimFactor()Returns the current trim factor.The trim factor controls how many percent of the maps current size is reclaimed, when performing an
removeLRU
operation. Defaults to 1% (0.01f
).- Returns:
- the current trim factor
-
setTrimFactor
public void setTrimFactor(float pTrimFactor) Sets the trim factor.The trim factor controls how many percent of the maps current size is reclaimed, when performing an
removeLRU
operation. Defaults to 1% (0.01f
).- Parameters:
pTrimFactor
- the new trim factor. Acceptable values are between 0 (inclusive) and 1 (exclusive).- See Also:
-
removeEldestEntry
always returnsfalse
, and instead invokesremoveLRU()
ifsize >= maxSize
.- Overrides:
removeEldestEntry
in classLinkedMap<K,
V> - Parameters:
pEldest
- The least recently inserted entry in the map, or if this is an access-ordered map, the least recently accessed entry. This is the entry that will be removed it this method returnstrue
. If the map was empty prior to theput
orputAll
invocation resulting in this invocation, this will be the entry that was just inserted; in other words, if the map contains a single entry, the eldest entry is also the newest.- Returns:
true
if the eldest entry should be removed from the map;false
if it should be retained.
-
removeEntry
Removes the given entry from the Map.- Parameters:
pEntry
- the entry to be removed- Returns:
- the removed entry, or
null
if nothing was removed.
-
processRemoved
Default implementation does nothing. May be used by clients as a call-back to notify when mappings expire from the map.- Specified by:
processRemoved
in interfaceExpiringMap<K,
V> - Parameters:
pRemoved
- the removed mapping
-
removeLRU
public void removeLRU()Removes the least recently used mapping(s) from this map.How many mappings are removed from the map, is controlled by the trim factor. In any case, at least one mapping will be removed.
- See Also:
-
size
public int size()- Specified by:
size
in interfaceMap<K,
V> - Overrides:
size
in classAbstractMap<K,
V>
-
clear
public void clear()- Specified by:
clear
in interfaceMap<K,
V> - Overrides:
clear
in classAbstractMap<K,
V>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceMap<K,
V> - Overrides:
isEmpty
in classAbstractMap<K,
V>
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V> - Overrides:
containsKey
in classAbstractMap<K,
V>
-
values
- Specified by:
values
in interfaceMap<K,
V> - Overrides:
values
in classAbstractMap<K,
V>
-
entrySet
- Specified by:
entrySet
in interfaceMap<K,
V> - Specified by:
entrySet
in classAbstractMap<K,
V>
-
keySet
- Specified by:
keySet
in interfaceMap<K,
V> - Overrides:
keySet
in classAbstractMap<K,
V>
-