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
A
Supplier
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
Modifier and TypeMethodDescriptionvoid
close()
If the lazily-cached value has been initialized and isAutoCloseable
then it is closed.get()
Supplies a value and caches it, ensuring the same value is returned on subsequent calls.static <T,
E extends Exception>
LazySupplier<T,E> Constructs a supplier that will lazily compute and cache a value from the given supplier.
-
Method Details
-
get
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
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
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.
-