- 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 <X extends Throwable>
voidcloseAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable)Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
voidcloseAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
voidcloseAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable)Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
voidcloseAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, Iterable<? extends AutoCloseable> closeable)Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.static <X extends Throwable>
XcloseAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable)Closes the givenAutoCloseable, catching allThrowable.static <X extends Throwable>
XcloseAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable)Closes all of the givenAutoCloseablein order, catching allThrowable.static <X extends Throwable>
XcloseAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, 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.- 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.- 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.- 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.- 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.- 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.- Parameters:
closeable- The set of all closeables, which will be closed in order- Returns:
- A new throwable or
null
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable) throws Error, RuntimeException, X extends Throwable
Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable) throws Error, RuntimeException, X extends Throwable
Closes the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
- Parameters:
xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable) throws Error, RuntimeException, X extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable) throws Error, RuntimeException, X extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
- Parameters:
xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, Iterable<? extends AutoCloseable> closeable) throws Error, RuntimeException, X extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when
t0isnulland no new throwables.- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndThrow
public static <X extends Throwable> void closeAndThrow(Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, Iterable<? extends AutoCloseable> closeable) throws Error, RuntimeException, X extends Throwable
Closes all of the givenAutoCloseablein order, throwing allThrowable, wrapping when needed.Only returns when no throwables.
- Parameters:
xClass- Throwables of this class, as well asErrorandRuntimeException, are thrown directly.xSupplier- 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 aRuntimeExceptionX- When resolved throwable is an instance ofxClass, otherwise wrapped viaxSupplierX extends Throwable
-
closeAndWrap
public static <X extends Throwable> X closeAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable closeable)
Closes the givenAutoCloseable, catching allThrowable. Wraps any resulting throwable, unless is an instance ofxClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
xClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
xSupplier.
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); }- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class are returned directly.xSupplier- 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 ofxClass, otherwise wrapped viaxSupplier.- 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 <X extends Throwable> X closeAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, AutoCloseable... closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable. Wraps any resulting throwable, unless is an instance ofxClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
xClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
xSupplier.
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); }- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class are returned directly.xSupplier- 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 ofxClass, otherwise wrapped viaxSupplier.- 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 <X extends Throwable> X closeAndWrap(Throwable t0, Class<? extends X> xClass, Function<? super Throwable,? extends X> xSupplier, Iterable<? extends AutoCloseable> closeable)
Closes all of the givenAutoCloseablein order, catching allThrowable. Wraps any resulting throwable, unless is an instance ofxClass,Error, orRuntimeException.- When
null, returnsnull. - When is an instance of
xClass, returns the exception. - When is
ErrororRuntimeException, throws the exception directly. - Otherwise, returns the exception wrapped via
xSupplier.
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); }- Parameters:
t0- If notnull, any new throwables will be combined viaThrowables.addSuppressed(java.lang.Throwable, java.lang.Throwable)xClass- Throwables of this class are returned directly.xSupplier- 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 ofxClass, otherwise wrapped viaxSupplier.- 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
-
-