K
- The type of the key of the cacheV
- The type of the values in the cachepublic class Cache<K,V>
extends java.lang.Object
implements java.util.function.Function<K,V>
Modifier and Type | Class and 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.
|
Modifier and Type | Field and 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 and 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.
|
Modifier and Type | Method and 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
|
private static final Cache.CycleHandler<java.lang.Object> EMPTY_HANDLER
private final Cache.CycleHandler<K> cycleHandler
private final java.util.concurrent.ConcurrentHashMap<K,Cache.OriginThreadAwareFuture> cache
public Cache(java.util.function.Function<K,V> computable)
computable
- function generated the new value.public Cache(java.util.function.Function<K,V> computable, Cache.CycleHandler<K> cycleHandler)
computable
- function generated the new value.cycleHandler
- handler used if the thread cycle is met.public void clear()
public boolean containsKey(K key)
key
- item key.public void remove(K key)
key
- item key.public int size()