Class ElementCache
- java.lang.Object
-
- org.eclipse.handly.util.LruCache<K,V>
-
- org.eclipse.handly.util.BoundedLruCache<IElement,java.lang.Object>
-
- org.eclipse.handly.model.impl.support.ElementCache
-
public class ElementCache extends BoundedLruCache<IElement,java.lang.Object>
A bounded LRU cache of element handle/body relationships that is intended to be used in advanced implementations ofIBodyCache
. 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 permitclosing
.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
Constructors Constructor Description ElementCache(int maxSize)
Constructs an emptyElementCache
with the given maximum size and a defaultload factor
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
ensureMaxSize(int childCount, IElement parent)
Ensures that there is enough room for adding the given number of child elements.protected void
evict(LruCache.Entry<IElement,java.lang.Object> entry)
Attempts to evict an existing entry from this cache in response to request tomakeSpace
.double
getLoadFactor()
Returns the load factor of this cache.int
getOverflow()
Returns the size of cache overflow.protected void
makeSpace(int sizeNeeded)
Attempts toevict
stale entries to make space as requested.void
resetMaxSize(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.void
setLoadFactor(double loadFactor)
Changes the load factor for this cache.java.lang.String
toString()
-
Methods inherited from class org.eclipse.handly.util.BoundedLruCache
add, maxSize, setMaxSize
-
-
-
-
Constructor Detail
-
ElementCache
public ElementCache(int maxSize)
Constructs an emptyElementCache
with the given maximum size and a defaultload factor
.- Parameters:
maxSize
- the maximum size of the cache (the bound)- Throws:
java.lang.IllegalArgumentException
- ifmaxSize < 1
-
-
Method Detail
-
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:
java.lang.IllegalArgumentException
- ifloadFactor <= 0
orloadFactor > 1
-
ensureMaxSize
public void ensureMaxSize(int childCount, IElement parent)
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
public void resetMaxSize(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.- Parameters:
maxSize
- a new value for maximum size of the cache (> 0)parent
- the parent element (notnull
)
-
toString
public java.lang.String toString()
-
makeSpace
protected void makeSpace(int sizeNeeded)
Description copied from class:BoundedLruCache
Attempts toevict
stale entries to make space as requested. Follows the access order, starting from the least recently used entry.- Overrides:
makeSpace
in classBoundedLruCache<IElement,java.lang.Object>
- Parameters:
sizeNeeded
- the requested space (>= 0)
-
evict
protected void evict(LruCache.Entry<IElement,java.lang.Object> 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
((IElementImplExtension)entry.key).
.close_
(of(CLOSE_HINT, CACHE_OVERFLOW))- Overrides:
evict
in classBoundedLruCache<IElement,java.lang.Object>
- Parameters:
entry
- an existing entry
-
-