public class ThrowingCloseableMemoizedSupplier<T,E extends Exception> extends AbstractCloseableMemoizedSupplier<T,E>
Example:
class Main {
public static void main() {
try (ThrowingCloseableMemoizedSupplier<Resource, Exception> closeableSupplier =
ThrowingCloseableMemoizedSupplier.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,
ThrowingConsumer<T,E> closer)
Wrap a supplier with
AutoCloseable interface and close only if the supplier has been
used. |
close, get
public static <T,E extends Exception> ThrowingCloseableMemoizedSupplier<T,E> of(java.util.function.Supplier<T> supplier, ThrowingConsumer<T,E> 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