public class CachedSupplier<T>
extends java.lang.Object
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 and Description |
|---|
CachedSupplier(com.google.common.base.Supplier<T> supplier)
Creates a new supplier.
|
| Modifier and Type | Method and Description |
|---|---|
T |
get()
Obtains the value.
|
boolean |
isValid()
Checks if the contents of the cache are valid.
|
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).
|
void |
reset()
Resets the cache forcing a
get() on the supplier next time get() is invoked. |
public CachedSupplier(com.google.common.base.Supplier<T> supplier)
public T get()
public void reset()
get() on the supplier next time get() is invoked.public void precomputed(T t)
If this method is invoked, then an invocation of get() will not trigger an
invocation of the supplier provided in the constructor.
t - the new cache contents; will replace any currently cache content, if one existspublic boolean isValid()