com.android.ide.common.caching
Class CreatingCache<K,V>

java.lang.Object
  extended by com.android.ide.common.caching.CreatingCache<K,V>

public class CreatingCache<K,V>
extends java.lang.Object

A cache that handles creating the values when they are not present in the map. Calls to 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.


Nested Class Summary
static interface CreatingCache.ValueFactory<K,V>
          A factory creating values based on keys.
 
Constructor Summary
CreatingCache(CreatingCache.ValueFactory<K,V> valueFactory)
           
 
Method Summary
 void clear()
          Clears the cache of all values.
 V get(K key)
          Queries the cache for a given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CreatingCache

public CreatingCache(@NonNull
                     CreatingCache.ValueFactory<K,V> valueFactory)
Method Detail

get

@Nullable
public V get(@NonNull
                      K key)
Queries the cache for a given key. If the value is not present, this blocks until it is. If this is the first thread requesting the value, then this trigger creation of the value through the CreatingCache.ValueFactory.

Parameters:
key - the given key.
Returns:
the value, or null if the thread was interrupted while waiting for the value to be created.

clear

public void clear()
Clears the cache of all values.

Throws:
java.lang.IllegalStateException - if values are currently being created.