java.lang.Object
org.apache.jackrabbit.oak.commons.pio.Closer
All Implemented Interfaces:
Closeable, AutoCloseable

public class Closer extends Object implements Closeable
Convenience utility to close a list of Closeables in reverse order, suppressing all but the first exception to occur.

Inspired by and replacing Guava's Closer.

  • Method Details

    • create

      public static Closer create()
      Create instance of Closer.
    • register

      @Nullable public <C extends Closeable> C register(@Nullable C closeable)
      Add a Closeable to the list.
      Parameters:
      closeable - Closeable object to be added
      Returns:
      the closeable param
    • close

      public void close() throws IOException
      Closes the set of Closeables in reverse order.

      Swallows all exceptions except the first that was thrown.

      If rethrow(java.lang.Throwable) was called before, even the first exception will be suppressed.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • rethrow

      public RuntimeException rethrow(@NotNull @NotNull Throwable throwable) throws IOException
      Sets a flag indicating that this method was called, then rethrows the given exception (potentially wrapped into Error or RuntimeException).

      close() will not throw when this method was called before.

      Returns:
      never returns
      Throws:
      IOException - wrapping the input, when needed