Package com.complexible.common.base
Class AutoCloser
- java.lang.Object
-
- com.complexible.common.base.AutoCloser
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public final class AutoCloser extends java.lang.Object implements java.lang.AutoCloseable
This class is an equivalent of Guava'sCloser
, that supportsAutoCloseable
s. See https://github.com/google/guava/issues/1020 Also, this class contains static utility methods for closing multiple AutoCloseables at once.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <O,I extends java.lang.AutoCloseable,E extends java.lang.Exception>
OapplyAndCloseOnError(I theClosable, java.util.function.Function<I,O> theAction, java.util.function.Function<java.lang.Throwable,E> theErrMapper)
Executes the action on the closeable and closes it immediately upon failure.void
close()
Closes allAutoCloseable
instances that have been added to thisAutoCloser
.static void
close(java.lang.AutoCloseable... theCloseables)
static <E extends java.lang.Exception>
voidclose(java.lang.Iterable<? extends java.lang.AutoCloseable> theCloseables)
static <T extends java.lang.Throwable>
voidclose(java.util.function.Consumer<T> errConsumer, java.lang.AutoCloseable... theCloseables)
static <T extends java.lang.Throwable>
voidclose(java.util.function.Consumer<T> errConsumer, java.lang.Iterable<? extends java.lang.AutoCloseable> theCloseables)
static void
closeSuppressedBy(java.lang.Exception theE, java.lang.AutoCloseable... theCloseables)
static AutoCloser
create()
Creates a newAutoCloser
.<C extends java.lang.AutoCloseable>
Cregister(C closeable)
java.lang.RuntimeException
rethrow(java.lang.Throwable e)
Stores the given throwable and rethrows it.
-
-
-
Method Detail
-
close
public static <E extends java.lang.Exception> void close(java.lang.Iterable<? extends java.lang.AutoCloseable> theCloseables) throws E extends java.lang.Exception
- Throws:
E extends java.lang.Exception
-
close
public static void close(java.lang.AutoCloseable... theCloseables)
-
close
public static <T extends java.lang.Throwable> void close(java.util.function.Consumer<T> errConsumer, java.lang.Iterable<? extends java.lang.AutoCloseable> theCloseables)
-
close
public static <T extends java.lang.Throwable> void close(java.util.function.Consumer<T> errConsumer, java.lang.AutoCloseable... theCloseables)
-
closeSuppressedBy
public static void closeSuppressedBy(java.lang.Exception theE, java.lang.AutoCloseable... theCloseables)
-
create
public static AutoCloser create()
Creates a newAutoCloser
.
-
register
public <C extends java.lang.AutoCloseable> C register(@Nullable C closeable)
- Returns:
- the given
closeable
-
rethrow
public java.lang.RuntimeException rethrow(java.lang.Throwable e) throws java.lang.Exception
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:
java.lang.Exception
- when the given throwable is an Exception
-
close
public void close()
Closes allAutoCloseable
instances that have been added to thisAutoCloser
. If an exception was thrown in the try block and passed to one of theexceptionThrown
methods, 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:
close
in interfacejava.lang.AutoCloseable
-
applyAndCloseOnError
public static <O,I extends java.lang.AutoCloseable,E extends java.lang.Exception> O applyAndCloseOnError(I theClosable, java.util.function.Function<I,O> theAction, java.util.function.Function<java.lang.Throwable,E> theErrMapper) throws E extends java.lang.Exception
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 extends java.lang.Exception
-
-