Package org.simpleframework.xml.util
Class LimitedCache<T>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.HashMap<K,V>
-
- java.util.LinkedHashMap<java.lang.Object,T>
-
- org.simpleframework.xml.util.LimitedCache<T>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<java.lang.Object,T>
,Cache<T>
- Direct Known Subclasses:
Resolver.Cache
public class LimitedCache<T> extends java.util.LinkedHashMap<java.lang.Object,T> implements Cache<T>
TheLimitedCache
interface is used to represent a cache that will store key value pairs. This implementation is backed by aLinkedHashMap
so that only a specific number of elements can be stored in the cache at one time.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private int
capacity
This represents the capacity of this cache instance.
-
Constructor Summary
Constructors Constructor Description LimitedCache()
Constructor of theLimitedCache
object.LimitedCache(int capacity)
Constructor of theLimitedCache
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cache(java.lang.Object key, T value)
This method is used to insert a key value mapping in to the cache.boolean
contains(java.lang.Object key)
This is used to determine whether the specified key exists with in the cache.T
fetch(java.lang.Object key)
This method is used to get the value from the cache that is mapped to the specified key.protected boolean
removeEldestEntry(java.util.Map.Entry<java.lang.Object,T> entry)
This is used to remove the eldest entry from the cache.T
take(java.lang.Object key)
This is used to exclusively take the value mapped to the specified key 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
-
-
-
-
Constructor Detail
-
LimitedCache
public LimitedCache()
Constructor of theLimitedCache
object. This is used to create a cache with a fixed size. The strategy for this cache is least recently used. Any insert or fetch from the cache is considered to be a use.
-
LimitedCache
public LimitedCache(int capacity)
Constructor of theLimitedCache
object. This is used to create a cache with a fixed size. The strategy for this cache is least recently used. Any insert or fetch from the cache is considered to be a use.- Parameters:
capacity
- this is the capacity of the cache object
-
-
Method Detail
-
cache
public void cache(java.lang.Object key, T value)
This method is used to insert a key value mapping in to the cache. The value can later be retrieved or removed from the cache if desired. If the value associated with the key is null then nothing is stored within the cache.
-
take
public T take(java.lang.Object key)
This is used to exclusively take the value mapped to the specified key from the cache. Invoking this is effectively removing the value from the cache.
-
fetch
public T fetch(java.lang.Object key)
This method is used to get the value from the cache that is mapped to the specified key. If there is no value mapped to the specified key then this method will return a null.
-
contains
public boolean contains(java.lang.Object key)
This is used to determine whether the specified key exists with in the cache. Typically this can be done using the fetch method, which will acquire the object.
-
removeEldestEntry
protected boolean removeEldestEntry(java.util.Map.Entry<java.lang.Object,T> entry)
This is used to remove the eldest entry from the cache. The eldest entry is removed from the cache if the size of the map grows larger than the maximum entries permitted.- Overrides:
removeEldestEntry
in classjava.util.LinkedHashMap<java.lang.Object,T>
- Parameters:
entry
- this is the eldest entry that can be removed- Returns:
- this returns true if the entry should be removed
-
-