Package org.forgerock.util
Class LazySupplier<T,E extends Exception>
- java.lang.Object
-
- org.forgerock.util.LazySupplier<T,E>
-
- Type Parameters:
T
- the type of value returned by the supplier.E
- the type of exceptions thrown by the supplier.
- All Implemented Interfaces:
AutoCloseable
,Supplier<T,E>
public final class LazySupplier<T,E extends Exception> extends Object implements Supplier<T,E>, AutoCloseable
ASupplier
that lazily computes a value the first time it is accessed and then caches the result to return on subsequent requests. This class is thread safe.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
If the lazily-cached value has been initialized and isAutoCloseable
then it is closed.T
get()
Supplies a value and caches it, ensuring the same value is returned on subsequent calls.static <T,E extends Exception>
LazySupplier<T,E>lazy(Supplier<T,E> supplier)
Constructs a supplier that will lazily compute and cache a value from the given supplier.
-
-
-
Method Detail
-
get
public T get() throws E extends Exception
Supplies a value and caches it, ensuring the same value is returned on subsequent calls. If an exception is thrown by the underlying supplier then this is thrown from this method and not cached.
-
close
public void close() throws Exception
If the lazily-cached value has been initialized and isAutoCloseable
then it is closed. If the value has not been initialized, or is not closeable, then this method does nothing.- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
- if theAutoCloseable.close()
method on the cached value throws an exception.
-
lazy
public static <T,E extends Exception> LazySupplier<T,E> lazy(Supplier<T,E> supplier)
Constructs a supplier that will lazily compute and cache a value from the given supplier.- Type Parameters:
T
- the type of values returned.E
- the type of exceptions thrown by the supplier.- Parameters:
supplier
- the supplier to use to compute the value.- Returns:
- a lazy version of the supplier.
-
-