org.hibernate.search.util.impl

## Class Closer<E extends Exception>

- java.lang.Object
- - org.hibernate.search.util.impl.Closer<E>

- All Implemented Interfaces:
    AutoCloseable

      public final class Closer<E extends Exception>
      extends Object
      implements AutoCloseable

  A helper for closing multiple resources and re-throwing only one exception, `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 );
         }
       }

  ### Exception type

  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>`.

  ### Splitting

  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.

  Author:
    Yoann Rodiere

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Closer()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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> Closer<E2>` | `split()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Closer

          public Closer()

  - ### Method Detail

    - #### push

          public <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`.
      Parameters:
        `operator` - An operator to close `objectToClose`. Accepts lambdas such as `MyType::close`.
        `objectToClose` - An object to close

    - #### push

          public void push(GenericCloseable<? extends E> closeable)

      Close the given `closeable` immediately, swallowing any throwable in order to throw it `later`.
      Parameters:
        `closeable` - A `GenericCloseable` to close. Accepts lambdas such as `myObject::close`.

    - #### pushAll

          public <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`.
      Parameters:
        `operator` - An operator to close each element in `objectsToClose`. Accepts lambdas such as `MyType::close`.
        `objectsToClose` - An iterable of objects to close

    - #### pushAll

          @SafeVarargs
          public final <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`.
      Parameters:
        `operator` - An operator to close each element in `objectsToClose`. Accepts lambdas such as `MyType::close`.
        `objectsToClose` - An array of objects to close

    - #### split

          public <E2 extends Exception> Closer<E2> split()

      Returns:
        A closer sharing the same state as `this`, allowing to handle multiple exception types.
      See Also:
        splitting

    - #### close

          public void close()
                     throws E extends Exception

      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `E` - The first throwable caught when executing the `push` methods, if any. Any throwable caught after the first will have been `suppressed`.
        `E extends Exception`

Copyright © 2006-2017 Red Hat, Inc. All Rights Reserved