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 size is never greater than maxSize.

Subclasses may override the evict method 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.

  • Constructor Details

    • BoundedLruCache

      public BoundedLruCache(int maxSize)
      Constructs a bounded LRU cache that is initially empty.
      Parameters:
      maxSize - the maximum size of the cache (the bound)
      Throws:
      IllegalArgumentException - if maxSize < 1
  • Method Details

    • 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 invoking makeSpace.
      Parameters:
      maxSize - a new value for maximum size of the cache
      Throws:
      IllegalArgumentException - if maxSize < 1
    • add

      protected void add(LruCache.Entry<K,V> entry)
      Adds a new entry to this cache in response to LruCache.put(Object, Object).

      If the cache is full, this implementation attempts to makeSpace for the new entry. The actual addition is handled by the super implementation.

      Overrides:
      add in class LruCache<K,V>
      Parameters:
      entry - the entry to add
    • makeSpace

      protected void makeSpace(int sizeNeeded)
      Attempts to evict stale 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 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 doRemove.

      Parameters:
      entry - an existing entry