Class ElementCache
java.lang.Object
org.eclipse.handly.util.LruCache<IElement,Object>
org.eclipse.handly.util.BoundedLruCache<IElement,Object>
org.eclipse.handly.model.impl.support.ElementCache
A bounded LRU cache of element handle/body relationships that is intended
to be used in advanced implementations of
IBodyCache. The cache is
not strictly bounded, but can overflow if an entry is added when the cache
is full but the current state of elements in the cache does not permit
closing.
This implementation is not thread-safe. If multiple threads access the cache concurrently, it must be synchronized externally.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.handly.util.LruCache
LruCache.Entry<K,V> -
Constructor Summary
ConstructorsConstructorDescriptionElementCache(int maxSize) Constructs an emptyElementCachewith the given maximum size and a defaultload factor. -
Method Summary
Modifier and TypeMethodDescriptionvoidensureMaxSize(int childCount, IElement parent) Ensures that there is enough room for adding the given number of child elements.protected voidevict(LruCache.Entry<IElement, Object> entry) Attempts to evict an existing entry from this cache in response to request tomakeSpace.doubleReturns the load factor of this cache.intReturns the size of cache overflow.protected voidmakeSpace(int sizeNeeded) Attempts toevictstale entries to make space as requested.voidresetMaxSize(int maxSize, IElement parent) If the given parent element was the one that increased the maximum size of this cache inensureMaxSize, resets the maximum size of the cache to the given value.voidsetLoadFactor(double loadFactor) Changes the load factor for this cache.toString()Methods inherited from class org.eclipse.handly.util.BoundedLruCache
add, maxSize, setMaxSize
-
Constructor Details
-
ElementCache
public ElementCache(int maxSize) Constructs an emptyElementCachewith the given maximum size and a defaultload factor.- Parameters:
maxSize- the maximum size of the cache (the bound)- Throws:
IllegalArgumentException- ifmaxSize < 1
-
-
Method Details
-
getOverflow
public int getOverflow()Returns the size of cache overflow.- Returns:
- the size of cache overflow
-
getLoadFactor
public double getLoadFactor()Returns the load factor of this cache. The load factor determines how much space is reclaimed when the cache overflows.- Returns:
- the load factor of the cache (a value in the interval (0, 1])
-
setLoadFactor
public void setLoadFactor(double loadFactor) Changes the load factor for this cache. The load factor determines how much space is reclaimed when the cache overflows.- Parameters:
loadFactor- a new value for load factor- Throws:
IllegalArgumentException- ifloadFactor <= 0orloadFactor > 1
-
ensureMaxSize
Ensures that there is enough room for adding the given number of child elements. If the maximum size of the cache must be increased, records the parent element that needed the new maximum size.- Parameters:
childCount- the number of child elements (>= 0)parent- the parent element (notnull)
-
resetMaxSize
If the given parent element was the one that increased the maximum size of this cache inensureMaxSize, resets the maximum size of the cache to the given value.- Parameters:
maxSize- a new value for maximum size of the cache (> 0)parent- the parent element (notnull)
-
toString
-
makeSpace
protected void makeSpace(int sizeNeeded) Description copied from class:BoundedLruCacheAttempts toevictstale entries to make space as requested. Follows the access order, starting from the least recently used entry.- Overrides:
makeSpacein classBoundedLruCache<IElement,Object> - 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
((IElementImplExtension)entry.key)..close_(of(CLOSE_HINT, CACHE_OVERFLOW))- Overrides:
evictin classBoundedLruCache<IElement,Object> - Parameters:
entry- an existing entry
-