Package com.complexible.common.base
Class AutoCloser
java.lang.Object
com.complexible.common.base.AutoCloser
- All Implemented Interfaces:
AutoCloseable
This class is an equivalent of Guava's
Closer, that supports AutoCloseables.
See https://github.com/google/guava/issues/1020
Also, this class contains static utility methods for closing multiple AutoCloseables at once.-
Method Summary
Modifier and TypeMethodDescriptionstatic <O,I extends AutoCloseable, E extends Exception>
OapplyAndCloseOnError(I theClosable, Function<I, O> theAction, Function<Throwable, E> theErrMapper) Executes the action on the closeable and closes it immediately upon failure.voidclose()Closes allAutoCloseableinstances that have been added to thisAutoCloser.static voidclose(AutoCloseable... theCloseables) static <E extends Exception>
voidclose(Iterable<? extends AutoCloseable> theCloseables) static <T extends Throwable>
voidclose(Consumer<T> errConsumer, AutoCloseable... theCloseables) static <T extends Throwable>
voidclose(Consumer<T> errConsumer, Iterable<? extends AutoCloseable> theCloseables) static voidcloseSuppressedBy(Exception theE, AutoCloseable... theCloseables) static AutoClosercreate()Creates a newAutoCloser.<C extends AutoCloseable>
Cregister(C closeable) Stores the given throwable and rethrows it.
-
Method Details
-
close
public static <E extends Exception> void close(Iterable<? extends AutoCloseable> theCloseables) throws E - Throws:
E
-
close
-
close
public static <T extends Throwable> void close(Consumer<T> errConsumer, Iterable<? extends AutoCloseable> theCloseables) -
close
public static <T extends Throwable> void close(Consumer<T> errConsumer, AutoCloseable... theCloseables) -
closeSuppressedBy
-
create
Creates a newAutoCloser. -
register
- Returns:
- the given
closeable
-
rethrow
Stores the given throwable and rethrows it. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.This method always throws, and as such should be called as
throw closer.rethrow(e);to ensure the compiler knows that it will throw.- Returns:
- this method does not return; it always throws
- Throws:
Exception- when the given throwable is an Exception
-
close
public void close()Closes allAutoCloseableinstances that have been added to thisAutoCloser. If an exception was thrown in the try block and passed to one of theexceptionThrownmethods, any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the first exception to be thrown from an attempt to close a closeable will be thrown and any additional exceptions that are thrown after that will be suppressed.- Specified by:
closein interfaceAutoCloseable
-
applyAndCloseOnError
public static <O,I extends AutoCloseable, O applyAndCloseOnErrorE extends Exception> (I theClosable, Function<I, O> theAction, Function<Throwable, throws EE> theErrMapper) Executes the action on the closeable and closes it immediately upon failure. Leaves the closeable unclosed otherwise because it can be used while the result is processed (e.g. if the result is a query result set).- Throws:
E
-