Package org.simpleframework.common
Class LeastRecentlyUsedMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- java.util.LinkedHashMap<K,V>
-
- org.simpleframework.common.LeastRecentlyUsedMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
public class LeastRecentlyUsedMap<K,V> extends java.util.LinkedHashMap<K,V>
TheLeastRecentlyUsedMap
is a hash map that keeps only those entries most recently used. This acts much like a hot spot cache for specific keys that are used frequently. It also allows for algorithms to keep hot spot values available in the cache without the risk of running out of memory.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
LeastRecentlyUsedMap.RemovalListener<K,V>
TheRemovalListener
is used with the least recently used hash map to listen for removals.
-
Field Summary
Fields Modifier and Type Field Description private int
capacity
This is the number of items to keep within the cache.private LeastRecentlyUsedMap.RemovalListener<K,V>
listener
This is the listener that is called when an entry is removed.
-
Constructor Summary
Constructors Constructor Description LeastRecentlyUsedMap()
Constructor for theLeastRecentlyUsedMap
object.LeastRecentlyUsedMap(int capacity)
Constructor for theLeastRecentlyUsedMap
object.LeastRecentlyUsedMap(LeastRecentlyUsedMap.RemovalListener<K,V> listener)
Constructor for theLeastRecentlyUsedMap
object.LeastRecentlyUsedMap(LeastRecentlyUsedMap.RemovalListener<K,V> listener, int capacity)
Constructor for theLeastRecentlyUsedMap
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
removeEldestEntry(java.util.Map.Entry<K,V> eldest)
This is used to determine if an entry should be removed from the cache.-
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, replaceAll, values
-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, size
-
-
-
-
Field Detail
-
listener
private final LeastRecentlyUsedMap.RemovalListener<K,V> listener
This is the listener that is called when an entry is removed.
-
capacity
private final int capacity
This is the number of items to keep within the cache.
-
-
Constructor Detail
-
LeastRecentlyUsedMap
public LeastRecentlyUsedMap()
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.
-
LeastRecentlyUsedMap
public LeastRecentlyUsedMap(int capacity)
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
capacity
- this is the capacity of the hash container
-
LeastRecentlyUsedMap
public LeastRecentlyUsedMap(LeastRecentlyUsedMap.RemovalListener<K,V> listener)
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
listener
- this listens for entries that are removed
-
LeastRecentlyUsedMap
public LeastRecentlyUsedMap(LeastRecentlyUsedMap.RemovalListener<K,V> listener, int capacity)
Constructor for theLeastRecentlyUsedMap
object. This creates a hash container that keeps only those entries that have been recently added or accessed available within the collection.- Parameters:
listener
- this listens for entries that are removedcapacity
- this is the capacity of the hash container
-
-
Method Detail
-
removeEldestEntry
protected boolean removeEldestEntry(java.util.Map.Entry<K,V> eldest)
This is used to determine if an entry should be removed from the cache. If the cache has reached its capacity then the listener, if one was specified is given a callback to tell any other participating objects the entry has been removed.
-
-