What is the right paradigm or utility class (can’t seem to find a preexisting class) to implement a lazy supplier in Java?
I want to have something that handles the compute-once/cache-later behavior and allows me to specify the computation behavior independently. I know this probably has an error but it has the right semantics:
abstract public class LazySupplier<T> implements Supplier<T>
{
private volatile T t;
final private Object lock = new Object();
final public T get() {
if (t == null)
{
synchronized(lock)
{
if (t == null)
t = compute();
}
}
return t;
}
abstract protected T compute();
}
This is already implemented in
Suppliers.memoizemethod.