Class Cache<K,V>

java.lang.Object
nl.colorize.util.stats.Cache<K,V>

public class Cache<K,V> extends Object
Data structure for key/value pairs, where the value for each key is based on an underlying compute function. The cache is lazy: values are only computed when first requested. If the cache capacity has been exceeded, the oldest values are removed. Note that "oldest" means the values that were least recently *calculated*, not the values that were least recently *accessed*.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    forget(K key)
    Forgets a cached key/value pairs in this cache.
    void
    Forgets all cached key/value pairs in this cache.
    static <K, V> Cache<K,V>
    from(Function<K,V> computeFunction)
    Creates a lookup table based on the specified function.
    static <K, V> Cache<K,V>
    from(Function<K,V> computeFunction, int capacity)
    Creates a lookup table based on the specified function.
    get(K key)
    Returns the value mapped to the specified key.
    int
     
    protected boolean
    isCached(K key)
    Returns true if the value for the specified key is currently cached, and false if the value still needs to be computed.
    void
    Precomputes the values for the specified keys, so the cached values are used when the key/value pairs are retrieved at a later time.
    void
    Precomputes the value for the specified key, so the cached value is used when the key/value pair is retrieved at a later time.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • get

      public V get(K key)
      Returns the value mapped to the specified key. If the key/value pair is currently present in the cache, the cached value will be returned. Otherwise, the underlying function is used to compute the value, cache it, then return it.
    • precompute

      public void precompute(K key)
      Precomputes the value for the specified key, so the cached value is used when the key/value pair is retrieved at a later time.
    • precompute

      public void precompute(Iterable<K> keys)
      Precomputes the values for the specified keys, so the cached values are used when the key/value pairs are retrieved at a later time.
    • forget

      public void forget(K key)
      Forgets a cached key/value pairs in this cache. If the key/value pair was not yet computed this method does nothing.
    • forgetAll

      public void forgetAll()
      Forgets all cached key/value pairs in this cache.
    • isCached

      protected boolean isCached(K key)
      Returns true if the value for the specified key is currently cached, and false if the value still needs to be computed.
    • getCapacity

      public int getCapacity()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • from

      public static <K, V> Cache<K,V> from(Function<K,V> computeFunction)
      Creates a lookup table based on the specified function. The lookup table will have unlimited capacity, cached values will never be removed.
    • from

      public static <K, V> Cache<K,V> from(Function<K,V> computeFunction, int capacity)
      Creates a lookup table based on the specified function. The lookup table will have limited capacity. If the number of key/value pairs exceeds the capacity, the oldest values will be removed.