public class ElementCache extends BoundedLruCache<IElement,java.lang.Object>
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.
LruCache.Entry<K,V>
Constructor and Description |
---|
ElementCache(int maxSize)
Constructs an empty
ElementCache with the given maximum size
and a default load factor . |
Modifier and Type | Method and 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 to
makeSpace . |
double |
getLoadFactor()
Returns the load factor of this cache.
|
int |
getOverflow()
Returns the size of cache overflow.
|
protected void |
makeSpace(int sizeNeeded)
Attempts to
evict
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 in
ensureMaxSize ,
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() |
add, maxSize, setMaxSize
public ElementCache(int maxSize)
ElementCache
with the given maximum size
and a default load factor
.maxSize
- the maximum size of the cache (the bound)java.lang.IllegalArgumentException
- if maxSize < 1
public int getOverflow()
public double getLoadFactor()
public void setLoadFactor(double loadFactor)
loadFactor
- a new value for load factorjava.lang.IllegalArgumentException
- if loadFactor <= 0
or
loadFactor > 1
public void ensureMaxSize(int childCount, IElement parent)
childCount
- the number of child elements (>= 0)parent
- the parent element (not null
)public void resetMaxSize(int maxSize, IElement parent)
ensureMaxSize
,
resets the maximum size of the cache to the given value.maxSize
- a new value for maximum size of the cache (> 0)parent
- the parent element (not null
)public java.lang.String toString()
protected void makeSpace(int sizeNeeded)
BoundedLruCache
evict
stale entries to make space as requested. Follows the access order,
starting from the least recently used entry.makeSpace
in class BoundedLruCache<IElement,java.lang.Object>
sizeNeeded
- the requested space (>= 0)protected void evict(LruCache.Entry<IElement,java.lang.Object> entry)
makeSpace
. 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))
evict
in class BoundedLruCache<IElement,java.lang.Object>
entry
- an existing entryCopyright (c) 2014, 2018 1C-Soft LLC and others. Made available under the Eclipse Public License 2.0