Package org.eclipse.handly.util
Class BoundedLruCache<K,V>
java.lang.Object
org.eclipse.handly.util.LruCache<K,V>
org.eclipse.handly.util.BoundedLruCache<K,V>
- Direct Known Subclasses:
ElementCache
An LRU cache with a fixed maximum size (the bound).
If an entry is added when the cache is full, this implementation removes
the least recently used entry, so that cache size is never
greater than maxSize.
Subclasses may override the evict method to impose a different policy for removing stale entries when
new entries are added to the cache; e.g., permit cache overflow by retaining
cache entries that cannot currently be evicted.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.handly.util.LruCache
LruCache.Entry<K,V> -
Constructor Summary
ConstructorsConstructorDescriptionBoundedLruCache(int maxSize) Constructs a bounded LRU cache that is initially empty. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidadd(LruCache.Entry<K, V> entry) Adds a new entry to this cache in response toLruCache.put(Object, Object).protected voidevict(LruCache.Entry<K, V> entry) Attempts to evict an existing entry from this cache in response to request tomakeSpace.protected voidmakeSpace(int sizeNeeded) Attempts toevictstale entries to make space as requested.final intmaxSize()Returns the maximum size of this cache.final voidsetMaxSize(int maxSize) Changes the maximum size of this cache.
-
Constructor Details
-
BoundedLruCache
public BoundedLruCache(int maxSize) Constructs a bounded LRU cache that is initially empty.- Parameters:
maxSize- the maximum size of the cache (the bound)- Throws:
IllegalArgumentException- ifmaxSize < 1
-
-
Method Details
-
maxSize
public final int maxSize()Returns the maximum size of this cache.- Returns:
- the maximum size of the cache
-
setMaxSize
public final void setMaxSize(int maxSize) Changes the maximum size of this cache. If the current cache size is greater than the new value for maximum size, attempts to trim the cache by invokingmakeSpace.- Parameters:
maxSize- a new value for maximum size of the cache- Throws:
IllegalArgumentException- ifmaxSize < 1
-
add
Adds a new entry to this cache in response toLruCache.put(Object, Object).If the cache is full, this implementation attempts to
makeSpacefor the new entry. The actual addition is handled by the super implementation. -
makeSpace
protected void makeSpace(int sizeNeeded) Attempts toevictstale entries to make space as requested. Follows the access order, starting from the least recently used entry.- Parameters:
sizeNeeded- the requested space (>= 0)
-
evict
Attempts to evict an existing entry from this cache in response to request tomakeSpace. It is permitted for this method to remove other cache entries along with the given entry or, if the given entry cannot currently be evicted, retain it in the cache.This implementation invokes
doRemove.- Parameters:
entry- an existing entry
-