com.twelvemonkeys.util
Class LRUMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by com.twelvemonkeys.util.LinkedMap<K,V>
          extended by com.twelvemonkeys.util.LRUMap<K,V>
All Implemented Interfaces:
ExpiringMap<K,V>, Serializable, Cloneable, Map<K,V>

public class LRUMap<K,V>
extends LinkedMap<K,V>
implements ExpiringMap<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.

Version:
$Id: com/twelvemonkeys/util/LRUMap.java#1 $
Author:
Harald Kuhr
See Also:
Serialized Form

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,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Map<K,Map.Entry<K,V>> entries
           
protected  int modCount
           
 
Fields inherited from class com.twelvemonkeys.util.LinkedMap
accessOrder
 
Constructor Summary
LRUMap()
          Creates an LRUMap with default max size (1000 entries).
LRUMap(int pMaxSize)
          Creates an LRUMap with the given max size.
LRUMap(Map<? extends K,? extends V> pContents)
          Creates an LRUMap with initial mappings from the given map, and default max size (1000 entries).
LRUMap(Map<? extends K,? extends V> pContents, int pMaxSize)
          Creates an LRUMap with initial mappings from the given map, and the given max size.
LRUMap(Map<K,Map.Entry<K,V>> pBacking, Map<? extends K,? extends V> pContents, int pMaxSize)
          Creates an LRUMap with initial mappings from the given map, and the given max size.
 
Method Summary
 void clear()
           
 boolean containsKey(Object pKey)
           
 Set<Map.Entry<K,V>> entrySet()
           
 int getMaxSize()
          Returns the maximum number of mappings in this map.
 float getTrimFactor()
          Returns the current trim factor.
 boolean isEmpty()
           
 Set<K> keySet()
           
 void processRemoved(Map.Entry<K,V> pRemoved)
          Default implementation does nothing.
protected  boolean removeEldestEntry(Map.Entry pEldest)
          always returns false, and instead invokes removeLRU() if size >= maxSize.
protected  Map.Entry<K,V> removeEntry(Map.Entry<K,V> pEntry)
          Removes the given entry from the Map.
 void removeLRU()
          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()
           
 Collection<V> 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, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

entries

protected Map<K,Map.Entry<K,V>> entries

modCount

protected transient volatile int modCount
Constructor Detail

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 the Map interface.

See Also:
LRUMap(int)

LRUMap

public LRUMap(int pMaxSize)
Creates an LRUMap with the given max size.

Parameters:
pMaxSize - size limit

LRUMap

public LRUMap(Map<? extends K,? extends V> pContents)
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 the Map interface.

Parameters:
pContents - the map whose mappings are to be placed in this map. May be null.
See Also:
LRUMap(Map, int)

LRUMap

public LRUMap(Map<? extends K,? extends V> pContents,
              int pMaxSize)
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 be null.
pMaxSize - size limit

LRUMap

public LRUMap(Map<K,Map.Entry<K,V>> pBacking,
              Map<? extends K,? extends V> pContents,
              int pMaxSize)
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 as pContents.
pContents - the map whose mappings are to be placed in this map. May be null.
pMaxSize - max size
Method Detail

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:
removeLRU()

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:
removeLRU()

removeEldestEntry

protected boolean removeEldestEntry(Map.Entry pEldest)
always returns false, and instead invokes removeLRU() if size >= maxSize.

Overrides:
removeEldestEntry in class LinkedMap<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 returns true. If the map was empty prior to the put or putAll 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

protected Map.Entry<K,V> removeEntry(Map.Entry<K,V> pEntry)
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

public void processRemoved(Map.Entry<K,V> pRemoved)
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 interface ExpiringMap<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:
getTrimFactor()

size

public int size()
Specified by:
size in interface Map<K,V>
Overrides:
size in class AbstractMap<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Overrides:
clear in class AbstractMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in class AbstractMap<K,V>

containsKey

public boolean containsKey(Object pKey)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>
Overrides:
values in class AbstractMap<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in class AbstractMap<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in class AbstractMap<K,V>


Copyright © 2014. All Rights Reserved.