java.lang.Object
org.glassfish.jersey.internal.util.collection.Cache<K,V>
Type Parameters:
K - The type of the key of the cache
V - The type of the values in the cache
All Implemented Interfaces:
Function<K,V>

public class Cache<K,V> extends Object implements Function<K,V>
Cache implementation that relies on FutureTask. Desired value will only be computed once and computed value stored in the cache. The implementation is based on an example from the "Java Concurrency in Practice" book authored by Brian Goetz and company.
Author:
Jakub Podlesak
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Should a cycle be detected during computation of a value for given key, this interface allows client code to register a callback that would get invoked in such a case.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cache(Function<K,V> computable)
    Create new cache with given computable to compute values.
    Cache(Function<K,V> computable, Cache.CycleHandler<K> cycleHandler)
    Create new cache with given computable and cycle handler.
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(K key)
     
    void
    Empty cache.
    boolean
    Returns true if the key has already been cached.
    Get the cache keys
    void
    remove(K key)
    Remove item from the cache.
    int
    Returns the size of the cache

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface java.util.function.Function

    andThen, compose
  • Constructor Details

    • Cache

      public Cache(Function<K,V> computable)
      Create new cache with given computable to compute values. Detected cycles will be ignored as there is a no-op cycle handler registered by default.
      Parameters:
      computable - function generated the new value.
    • Cache

      public Cache(Function<K,V> computable, Cache.CycleHandler<K> cycleHandler)
      Create new cache with given computable and cycle handler.
      Parameters:
      computable - function generated the new value.
      cycleHandler - handler used if the thread cycle is met.
  • Method Details

    • apply

      public V apply(K key)
      Specified by:
      apply in interface Function<K,V>
    • clear

      public void clear()
      Empty cache.
    • keys

      public Enumeration<K> keys()
      Get the cache keys
      Returns:
    • containsKey

      public boolean containsKey(K key)
      Returns true if the key has already been cached.
      Parameters:
      key - item key.
      Returns:
      true if given key is present in the cache.
    • remove

      public void remove(K key)
      Remove item from the cache.
      Parameters:
      key - item key.
    • size

      public int size()
      Returns the size of the cache
      Returns:
      The number of elements in the cache