Class Cache<K,V>
- java.lang.Object
-
- org.glassfish.jersey.internal.util.collection.Cache<K,V>
-
- Type Parameters:
K
- The type of the key of the cacheV
- The type of the values in the cache
- All Implemented Interfaces:
java.util.function.Function<K,V>
public class Cache<K,V> extends java.lang.Object implements java.util.function.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Cache.CycleHandler<K>
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.private class
Cache.OriginThreadAwareFuture
Helper class, that remembers the future task origin thread, so that cycles could be detected.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentHashMap<K,Cache.OriginThreadAwareFuture>
cache
private java.util.function.Function<K,V>
computable
private Cache.CycleHandler<K>
cycleHandler
private static Cache.CycleHandler<java.lang.Object>
EMPTY_HANDLER
-
Constructor Summary
Constructors Constructor Description Cache(java.util.function.Function<K,V> computable)
Create new cache with given computable to compute values.Cache(java.util.function.Function<K,V> computable, Cache.CycleHandler<K> cycleHandler)
Create new cache with given computable and cycle handler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description V
apply(K key)
void
clear()
Empty cache.boolean
containsKey(K key)
Returns true if the key has already been cached.void
remove(K key)
Remove item from the cache.int
size()
Returns the size of the cache
-
-
-
Field Detail
-
EMPTY_HANDLER
private static final Cache.CycleHandler<java.lang.Object> EMPTY_HANDLER
-
cycleHandler
private final Cache.CycleHandler<K> cycleHandler
-
cache
private final java.util.concurrent.ConcurrentHashMap<K,Cache.OriginThreadAwareFuture> cache
-
-
Constructor Detail
-
Cache
public Cache(java.util.function.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(java.util.function.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 Detail
-
clear
public void clear()
Empty cache.
-
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
-
-