Class CachedSupplier<T>
- java.lang.Object
-
- com.android.tools.build.apkzlib.utils.CachedSupplier<T>
-
public class CachedSupplier<T> extends java.lang.ObjectSupplier that will cache a computed value and always supply the same value. It can be used to lazily compute data. For example:CachedSupplier<Integer> value = new CachedSupplier<>(() -> { Integer result; // Do some expensive computation. return result; }); if (a) { // We need the result of the expensive computation. Integer r = value.get(); } if (b) { // We also need the result of the expensive computation. Integer r = value.get(); } // If neither a nor b are true, we avoid doing the computation at all.
-
-
Constructor Summary
Constructors Constructor Description CachedSupplier(com.google.common.base.Supplier<T> supplier)Creates a new supplier.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Obtains the value.booleanisValid()Checks if the contents of the cache are valid.voidprecomputed(T t)In some cases, we may be able to precompute the cache value (or load it from somewhere we had previously stored it).voidreset()Resets the cache forcing aget()on the supplier next timeget()is invoked.
-
-
-
Constructor Detail
-
CachedSupplier
public CachedSupplier(com.google.common.base.Supplier<T> supplier)
Creates a new supplier.
-
-
Method Detail
-
get
public T get()
Obtains the value.- Returns:
- the value, either cached (if one exists) or computed
-
reset
public void reset()
Resets the cache forcing aget()on the supplier next timeget()is invoked.
-
precomputed
public void precomputed(T t)
In some cases, we may be able to precompute the cache value (or load it from somewhere we had previously stored it). This method allows the cache value to be loaded.If this method is invoked, then an invocation of
get()will not trigger an invocation of the supplier provided in the constructor.- Parameters:
t- the new cache contents; will replace any currently cache content, if one exists
-
isValid
public boolean isValid()
Checks if the contents of the cache are valid.- Returns:
- are there valid contents in the cache?
-
-