Class 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's Closer, that supports AutoCloseables. 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>
      O
      applyAndCloseOnError​(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 all AutoCloseable instances that have been added to this AutoCloser.
      static void close​(java.lang.AutoCloseable... theCloseables)  
      static <E extends java.lang.Exception>
      void
      close​(java.lang.Iterable<? extends java.lang.AutoCloseable> theCloseables)  
      static <T extends java.lang.Throwable>
      void
      close​(java.util.function.Consumer<T> errConsumer, java.lang.AutoCloseable... theCloseables)  
      static <T extends java.lang.Throwable>
      void
      close​(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 new AutoCloser.
      <C extends java.lang.AutoCloseable>
      C
      register​(C closeable)
      Registers the given closeable to be closed when this AutoCloser is closed.
      java.lang.RuntimeException rethrow​(java.lang.Throwable e)
      Stores the given throwable and rethrows it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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)
      • register

        public <C extends java.lang.AutoCloseable> C register​(@Nullable
                                                              C closeable)
        Registers the given closeable to be closed when this AutoCloser is closed.
        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 all AutoCloseable instances that have been added to this AutoCloser. If an exception was thrown in the try block and passed to one of the exceptionThrown 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 interface java.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