public final class Closer<E extends Exception> extends Object implements AutoCloseable
suppressing the others as necessary.
This class is not thread safe.
This helper is mainly useful when implementing AutoCloseable.close()
or a similar closing method in your own class, to make sure that all resources are
at least given the chance to close, even if closing one of them fails.
When creating then closing resources in the scope of a single method call,
try-with-resource blocks should be favored.
Warning: In order not to ignore exceptions,
you should always call close()
once you closed all of your resources. The most straightforward way
to do this is to use a try-with-resource block:
public void myCloseFunction() throws MyException {
try ( Closer<MyException> closer = new Closer<>() ) {
closer.push( this.myCloseable::close );
closer.pushAll( this.myListOfCloseables, MyCloseableType::close );
}
}
Note that the closer has a generic type parameter, allowing it to
re-throw a given checked exception.
If you don't want to use this feature, you can simply use a
Closer<RuntimeException>.
If you need to close multiple resources throwing different checked exceptions, and those exceptions don't have a practical common superclass, you can "split" the closer:
public void myCloseFunction() throws IOException, MyException, MyOtherException {
try ( Closer<MyException> closer1 = new Closer<>();
Closer<IOException> closer2 = closer1.split();
Closer<MyOtherException> closer3 = closer1.split() ) {
closer2.push( this.someJavaIOCloseable::close );
closer1.pushAll( this.myListOfCloseables1, MyCloseableTypeThrowingMyException::close );
closer3.pushAll( this.myListOfCloseables2, MyCloseableTypeThrowingMyOtherException::close );
}
}
The multiple closers will share the same state, which means the first exception to be caught by any of the closers will be the one to be re-thrown, and all subsequent exceptions caught by any closer will be added as suppressed to this first exception. Be careful though, you will have to close every single closer. Closing just the original one will not be enough.
| Constructor and Description |
|---|
Closer() |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
<T> void |
push(ClosingOperator<T,? extends E> operator,
T objectToClose)
Execute the given close
operator immediately on
the given objectToClose, swallowing any throwable in order to
throw it later. |
void |
push(GenericCloseable<? extends E> closeable)
Close the given
closeable immediately,
swallowing any throwable in order to throw it later. |
<T> void |
pushAll(ClosingOperator<T,? extends E> operator,
Iterable<T> objectsToClose)
Execute the given close
operator immediately on
each element of the given iterable, swallowing any throwable in order to
throw them later. |
<T> void |
pushAll(ClosingOperator<T,? extends E> operator,
T... objectsToClose)
Execute the given close
operator immediately on
each element of the given array, swallowing any throwable in order to
throw them later. |
<E2 extends Exception> |
split() |
public <T> void push(ClosingOperator<T,? extends E> operator, T objectToClose)
operator immediately on
the given objectToClose, swallowing any throwable in order to
throw it later.operator - An operator to close objectToClose. Accepts lambdas
such as MyType::close.objectToClose - An object to closepublic void push(GenericCloseable<? extends E> closeable)
closeable immediately,
swallowing any throwable in order to throw it later.closeable - A GenericCloseable to close. Accepts lambdas
such as myObject::close.public <T> void pushAll(ClosingOperator<T,? extends E> operator, Iterable<T> objectsToClose)
operator immediately on
each element of the given iterable, swallowing any throwable in order to
throw them later.operator - An operator to close each element in objectsToClose. Accepts lambdas
such as MyType::close.objectsToClose - An iterable of objects to close@SafeVarargs public final <T> void pushAll(ClosingOperator<T,? extends E> operator, T... objectsToClose)
operator immediately on
each element of the given array, swallowing any throwable in order to
throw them later.operator - An operator to close each element in objectsToClose. Accepts lambdas
such as MyType::close.objectsToClose - An array of objects to closepublic <E2 extends Exception> Closer<E2> split()
this, allowing to handle
multiple exception types.public void close()
throws E extends Exception
close in interface AutoCloseableE - The first throwable caught when executing the push methods, if any.
Any throwable caught after the first will have been
suppressed.E extends ExceptionCopyright © 2006–2017 Hibernate. All rights reserved.