Package org.eclipse.handly.model.impl.support

# 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

public class ElementCache extends BoundedLruCache<IElement,Object>
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

  Constructors
  Constructor
  Description
  `ElementCache(int maxSize)`
  Constructs an empty `ElementCache` with the given maximum size and a default `load factor`.

- ## Method Summary

  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,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.
  `String`
  `toString()`

  ### Methods inherited from class org.eclipse.handly.util.BoundedLruCache

  `add, maxSize, setMaxSize`

  ### Methods inherited from class org.eclipse.handly.util.LruCache

  `clear, doAdd, doRemove, entryByKey, get, getLruEntry, getMruEntry, isEmpty, moveToMru, newEntry, peek, put, remove, remove, size, snapshot, update`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### ElementCache

    public ElementCache(int maxSize)
    Constructs an empty `ElementCache` with the given maximum size and a default `load factor`.
    Parameters:
      `maxSize` - the maximum size of the cache (the bound)
    Throws:
      `IllegalArgumentException` - if `maxSize < 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` - if `loadFactor <= 0` or `loadFactor > 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 (not `null`)

  - ### resetMaxSize

    public 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.
    Parameters:
      `maxSize` - a new value for maximum size of the cache (> 0)
      `parent` - the parent element (not `null`)

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `LruCache<IElement,Object>`

  - ### makeSpace

    protected void makeSpace(int sizeNeeded)
    Description copied from class: `BoundedLruCache`
    Attempts to `evict` stale entries to make space as requested. Follows the access order, starting from the least recently used entry.
    Overrides:
      `makeSpace` in class `BoundedLruCache<IElement,Object>`
    Parameters:
      `sizeNeeded` - the requested space (>= 0)

  - ### evict

    protected void evict(LruCache.Entry<IElement,Object> entry)
    Attempts to evict an existing entry from this cache in response to request to `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))`.

    Overrides:
      `evict` in class `BoundedLruCache<IElement,Object>`
    Parameters:
      `entry` - an existing entry