public class CloseableMemoizedSupplier<T> extends AbstractCloseableMemoizedSupplier<T,RuntimeException>
Example:
class Main {
public static void main() {
try (CloseableMemoizedSupplier<Resource> closeableSupplier =
CloseableMemoizedSupplier.of(Resource::new, resource::shutDown)) {
if (condition) {
useResource(closeableSupplier.get())
}
}
}
}
The above will only construct the Resource if condition is true, and close the constructed resource appropriately.
Modifier and Type | Method and Description |
---|---|
static <T,E extends Exception> |
of(java.util.function.Supplier<T> supplier,
java.util.function.Consumer<T> closer)
Wrap a supplier with
AutoCloseable interface and close only if the supplier has been
used. |
close, get
public static <T,E extends Exception> CloseableMemoizedSupplier<T> of(java.util.function.Supplier<T> supplier, java.util.function.Consumer<T> closer)
AutoCloseable
interface and close only if the supplier has been
used. Close is idempotent.supplier
- the Supplier of a resource to be closedcloser
- the method to close the resource