Package org.eclipse.handly.util

# Class LruCache<K,V>

java.lang.Object
org.eclipse.handly.util.LruCache<K,V>

Direct Known Subclasses:
  `BoundedLruCache`

public class LruCache<K,V> extends Object
An LRU cache. Entries are ordered in the cache from most recently accessed to least recently accessed. When a cache entry is accessed via `get` or `put` methods, it is moved to the most recently used position in the cache. No other public methods generate entry accesses.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected static class`
  `LruCache.Entry<K,V>`
  An LRU cache entry.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `LruCache()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `add(LruCache.Entry<K,V> entry)`
  Adds a new entry to this cache in response to `put(Object, Object)`.
  `void`
  `clear()`
  Removes all entries from this cache.
  `protected void`
  `doAdd(LruCache.Entry<K,V> entry)`
  Actually adds a new entry to this cache.
  `protected void`
  `doRemove(LruCache.Entry<K,V> entry)`
  Actually removes an existing entry from this cache.
  `protected final LruCache.Entry<K,V>`
  `entryByKey(Object key)`
  Returns the corresponding entry for the given key, or `null` if this cache contains no entry for the key.
  `final V`
  `get(Object key)`
  Returns the corresponding value for the given key and moves the corresponding entry to the most recently used position in this cache.
  `protected final LruCache.Entry<K,V>`
  `getLruEntry()`
  Returns the least recently used cache entry, or `null` if this cache is empty.
  `protected final LruCache.Entry<K,V>`
  `getMruEntry()`
  Returns the most recently used cache entry, or `null` if this cache is empty.
  `final boolean`
  `isEmpty()`
  Returns whether this cache is empty.
  `protected void`
  `moveToMru(LruCache.Entry<K,V> entry)`
  Moves an existing cache entry to the MRU position.
  `protected LruCache.Entry<K,V>`
  `newEntry(K key, V value)`
  Creates a new cache entry with the given key and value.
  `final V`
  `peek(Object key)`
  Returns the corresponding value for the given key without disturbing cache ordering, or `null` if this cache contains no value for the key.
  `final V`
  `put(K key, V value)`
  Caches the given value for the given key and moves the corresponding entry to the most recently used position in this cache.
  `final V`
  `remove(Object key)`
  Removes the cache entry for the given key if it is present.
  `protected void`
  `remove(LruCache.Entry<K,V> entry)`
  Removes an existing entry from this cache in response to `remove(Object)`.
  `final int`
  `size()`
  Returns the size of this cache.
  `final Map<K,V>`
  `snapshot()`
  Returns a snapshot of the current contents of this cache, ordered from most recently accessed to least recently accessed.
  `String`
  `toString()`
  `protected void`
  `update(LruCache.Entry<K,V> entry, V value)`
  Updates an existing cache entry to change its value and moves it to the MRU position in response to `put(Object, Object)`.

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

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

- ## Constructor Details

  - ### LruCache

    public LruCache()

- ## Method Details

  - ### size

    public final int size()
    Returns the size of this cache.
    Returns:
      the size of the cache

  - ### isEmpty

    public final boolean isEmpty()
    Returns whether this cache is empty.
    Returns:
      `true` if the cache is empty, and `false` otherwise

  - ### get

    public final V get(Object key)
    Returns the corresponding value for the given key and moves the corresponding entry to the most recently used position in this cache. If the cache contains no value for the key, `null` is returned.
    Parameters:
      `key` - the key whose corresponding value is to be returned
    Returns:
      the corresponding value for the given key, or `null` if the cache contains no value for the key

  - ### peek

    public final V peek(Object key)
    Returns the corresponding value for the given key without disturbing cache ordering, or `null` if this cache contains no value for the key.
    Parameters:
      `key` - the key whose corresponding value is to be returned
    Returns:
      the corresponding value for the given key, or `null` if the cache contains no value for the key

  - ### put

    public final V put(K key, V value)
    Caches the given value for the given key and moves the corresponding entry to the most recently used position in this cache. Returns the previous value of the updated cache entry, or `null` if the cache contained no value for the key.
    Parameters:
      `key` - the key for which the given value is to be cached (not `null`)
      `value` - the value to be cached for the given key (not `null`)
    Returns:
      the previous value of the updated cache entry, or `null` if the cache contained no value for the key

  - ### remove

    public final V remove(Object key)
    Removes the cache entry for the given key if it is present. Returns the value of the removed cache entry, or `null` if this cache contained no value for the key.
    Parameters:
      `key` - the key whose entry is to be removed from the cache
    Returns:
      the value of the removed cache entry, or `null` if the cache contained no value for the key

  - ### clear

    public void clear()
    Removes all entries from this cache.

  - ### snapshot

    public final Map<K,V> snapshot()
    Returns a snapshot of the current contents of this cache, ordered from most recently accessed to least recently accessed.
    Returns:
      a snapshot of the current contents of the cache (never `null`)

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### getMruEntry

    protected final LruCache.Entry<K,V> getMruEntry()
    Returns the most recently used cache entry, or `null` if this cache is empty.
    Returns:
      the MRU entry, or `null` if the cache is empty

  - ### getLruEntry

    protected final LruCache.Entry<K,V> getLruEntry()
    Returns the least recently used cache entry, or `null` if this cache is empty.
    Returns:
      the LRU entry, or `null` if the cache is empty

  - ### entryByKey

    protected final LruCache.Entry<K,V> entryByKey(Object key)
    Returns the corresponding entry for the given key, or `null` if this cache contains no entry for the key.
    Parameters:
      `key` - the key whose corresponding entry is to be returned
    Returns:
      the corresponding entry for the given key, or `null` if the cache contains no entry for the key

  - ### newEntry

    protected LruCache.Entry<K,V> newEntry(K key, V value)
    Creates a new cache entry with the given key and value.
    Parameters:
      `key` - the key of the new entry (never `null`)
      `value` - the value of the new entry (never `null`)
    Returns:
      the created entry (not `null`)

  - ### add

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

    This implementation invokes `doAdd(Entry)`.

    Parameters:
      `entry` - the entry to add (never `null`)

  - ### update

    protected void update(LruCache.Entry<K,V> entry, V value)
    Updates an existing cache entry to change its value and moves it to the MRU position in response to `put(Object, Object)`.

    This implementation changes the entry value and then invokes `moveToMru(Entry)`.

    Parameters:
      `entry` - the entry to update (never `null`)
      `value` - a new value for the entry (never `null`)

  - ### remove

    protected void remove(LruCache.Entry<K,V> entry)
    Removes an existing entry from this cache in response to `remove(Object)`.

    This implementation invokes `doRemove(Entry)`.

    Parameters:
      `entry` - the entry to remove (never `null`)

  - ### doAdd

    protected void doAdd(LruCache.Entry<K,V> entry)
    Actually adds a new entry to this cache.
    Parameters:
      `entry` - the entry to add (never `null`)

  - ### doRemove

    protected void doRemove(LruCache.Entry<K,V> entry)
    Actually removes an existing entry from this cache.
    Parameters:
      `entry` - the entry to remove (never `null`)

  - ### moveToMru

    protected void moveToMru(LruCache.Entry<K,V> entry)
    Moves an existing cache entry to the MRU position.
    Parameters:
      `entry` - the entry to move (never `null`)