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
public class BoundedLruCache<K,V> extends LruCache<K,V>
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
sizeis never greater thanmaxSize.Subclasses may override the
evictmethod 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
Constructors Constructor Description BoundedLruCache(int maxSize)Constructs a bounded LRU cache that is initially empty.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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.intmaxSize()Returns the maximum size of this cache.voidsetMaxSize(int maxSize)Changes the maximum size of this cache.
-
-
-
Method Detail
-
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:
java.lang.IllegalArgumentException- ifmaxSize < 1
-
add
protected void add(LruCache.Entry<K,V> entry)
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
protected void evict(LruCache.Entry<K,V> entry)
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
-
-