public class CreatingCache<K,V>
extends java.lang.Object
get(Object)
returns the value, calling into CreatingCache.ValueFactory
if it
was not created. If the creation takes a long time, other threads can still query the cache
for the same or different keys. Calls for the same key will block until the value has been
created. Calls for different keys will return right away if the key is available.
This is very similar to Guava's LoadingCache, without the automated clean-up based on size
or time.
This is extracted from the PreDexCache of the Gradle plugin which has different requirements
(reloading cached info from disk)
This class is thread-safe.
TODO Move PreDexCache to be based on this.Modifier and Type | Class and Description |
---|---|
static interface |
CreatingCache.ValueFactory<K,V>
A factory creating values based on keys.
|
Constructor and Description |
---|
CreatingCache(CreatingCache.ValueFactory<K,V> valueFactory) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the cache of all values.
|
V |
get(K key)
Queries the cache for a given key.
|
java.util.List<V> |
values() |
public CreatingCache(@NonNull CreatingCache.ValueFactory<K,V> valueFactory)
@Nullable public V get(@NonNull K key)
CreatingCache.ValueFactory
.key
- the given key.public java.util.List<V> values()
public void clear()
java.lang.IllegalStateException
- if values are currently being created.