- java.lang.Object
-
- com.aoapps.lang.AutoCloseables
-
public final class AutoCloseables extends Object
Utilities for working withAutoCloseable.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ThrowablecloseAndCatch(AutoCloseable closeable)Closes the givenAutoCloseable, catching allThrowable.static ThrowablecloseAndCatch(AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static ThrowablecloseAndCatch(Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static ThrowablecloseAndCatch(Throwable t0, AutoCloseable closeable)Closes the givenAutoCloseable, catching allThrowable.static ThrowablecloseAndCatch(Throwable t0, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static ThrowablecloseAndCatch(Throwable t0, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static <Ex extends Throwable>
voidcloseAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable)Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
voidcloseAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
voidcloseAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable)Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <Ex extends Throwable>
ExcloseAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable)Closes the givenAutoCloseable, catching allThrowable.static <Ex extends Throwable>
ExcloseAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static <Ex extends Throwable>
ExcloseAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.
-
-
-
Method Detail
-
closeAndCatch
public static Throwable closeAndCatch(Throwable t0, AutoCloseable closeable)
Closes the givenAutoCloseable, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)closeable- The closeable to be closed- Returns:
t0, a new throwable, ornullwhen none given and none new
-
closeAndCatch
public static Throwable closeAndCatch(AutoCloseable closeable)
Closes the givenAutoCloseable, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
closeable- The closeable to be closed- Returns:
- A new throwable or
null
-
closeAndCatch
public static Throwable closeAndCatch(Throwable t0, AutoCloseable... closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)closeable- The set of all closeables, which will be closed in order- Returns:
t0, a new throwable, ornullwhen none given and none new
-
closeAndCatch
public static Throwable closeAndCatch(AutoCloseable... closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
closeable- The set of all closeables, which will be closed in order- Returns:
- A new throwable or
null
-
closeAndCatch
public static Throwable closeAndCatch(Throwable t0, Iterable<? extends AutoCloseable> closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)closeable- The set of all closeables, which will be closed in order- Returns:
t0, a new throwable, ornullwhen none given and none new
-
closeAndCatch
public static Throwable closeAndCatch(Iterable<? extends AutoCloseable> closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable.See
Throwables.addSuppressed(java.lang.Throwable, java.lang.Throwable)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
closeable- The set of all closeables, which will be closed in order- Returns:
- A new throwable or
null
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable) throws Error, RuntimeException, Ex extends Throwable
Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The closeable to be closed- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable) throws Error, RuntimeException, Ex extends Throwable
Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The closeable to be closed- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable) throws Error, RuntimeException, Ex extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The set of all closeables, which will be closed in order- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable) throws Error, RuntimeException, Ex extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The set of all closeables, which will be closed in order- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable) throws Error, RuntimeException, Ex extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The set of all closeables, which will be closed in order- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndThrow
public static <Ex extends Throwable> void closeAndThrow(Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable) throws Error, RuntimeException, Ex extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
When the exception is an
InterruptedExceptionand is wrapped viaexSupplier, and the resulting wrapper is not itself anInterruptedException, the current thread will be re-interrupted.- Parameters:
exClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.exSupplier- Other throwables are wrapped via this function, then throwncloseable- The set of all closeables, which will be closed in order- Throws:
Error- When resolved throwable is anErrorRuntimeException- When resolved throwable is aRuntimeExceptionEx- When resolved throwable is an instance ofexClass, otherwise wrapped viaexSupplierEx extends Throwable
-
closeAndWrap
public static <Ex extends Throwable> Ex closeAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable closeable)
Closes the givenAutoCloseable, catching allThrowable. Wraps any resulting throwable, unless is an instance ofexClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
exClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
exSupplier.
This is expected to typically used within a catch block, to throw a narrower scope:
try { … } catch (Throwable t) { throw AutoCloseables.closeAndWrap(t, SQLException.class, SQLException::new, closeable); }See
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class are returned directly.exSupplier- Throwables that a not returned directly, and are notErrororRuntimeException, are wrapped via this function, then returned.closeable- The closeable to be closed- Returns:
nullwhent0isnulland no new throwables, resulting throwable when is an instance ofexClass, otherwise wrapped viaexSupplier.- Throws:
Error- When resulting throwable is anErrorRuntimeException- When resulting throwable is aRuntimeException- See Also:
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)
- When
-
closeAndWrap
public static <Ex extends Throwable> Ex closeAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, AutoCloseable... closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable. Wraps any resulting throwable, unless is an instance ofexClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
exClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
exSupplier.
This is expected to typically used within a catch block, to throw a narrower scope:
try { … } catch (Throwable t) { throw AutoCloseables.closeAndWrap(t, SQLException.class, SQLException::new, closeable); }See
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class are returned directly.exSupplier- Throwables that a not returned directly, and are notErrororRuntimeException, are wrapped via this function, then returned.closeable- The set of all closeables, which will be closed in order- Returns:
nullwhent0isnulland no new throwables, resulting throwable when is an instance ofexClass, otherwise wrapped viaexSupplier.- Throws:
Error- When resulting throwable is anErrorRuntimeException- When resulting throwable is aRuntimeException- See Also:
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)
- When
-
closeAndWrap
public static <Ex extends Throwable> Ex closeAndWrap(Throwable t0, Class<? extends Ex> exClass, Function<? super Throwable,? extends Ex> exSupplier, Iterable<? extends AutoCloseable> closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable. Wraps any resulting throwable, unless is an instance ofexClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
exClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
exSupplier.
This is expected to typically used within a catch block, to throw a narrower scope:
try { … } catch (Throwable t) { throw AutoCloseables.closeAndWrap(t, SQLException.class, SQLException::new, closeable); }See
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)for details on howThreadDeathandInterruptedExceptionare managed.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)exClass- Throwables of this class are returned directly.exSupplier- Throwables that a not returned directly, and are notErrororRuntimeException, are wrapped via this function, then returned.closeable- The set of all closeables, which will be closed in order- Returns:
nullwhent0isnulland no new throwables, resulting throwable when is an instance ofexClass, otherwise wrapped viaexSupplier.- Throws:
Error- When resulting throwable is anErrorRuntimeException- When resulting throwable is aRuntimeException- See Also:
Throwables.wrap(java.lang.Throwable, java.lang.Class, java.util.function.Function)
- When
-
-