Class Closer
- 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 ofCloseable
s in reverse order, suppressing all but the first exception to occur.Inspired by and replacing Guava's Closer.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the set ofCloseable
s in reverse order.static Closer
create()
Create instance of Closer.<C extends Closeable>
Cregister(C closeable)
Add aCloseable
to the list.RuntimeException
rethrow(@NotNull Throwable throwable)
Sets a flag indicating that this method was called, then rethrows the given exception (potentially wrapped intoError
orRuntimeException
).
-
-
-
Method Detail
-
create
public static Closer create()
Create instance of Closer.
-
register
@Nullable public <C extends Closeable> C register(@Nullable C closeable)
Add aCloseable
to the list.- Parameters:
closeable
-Closeable
object to be added- Returns:
- the closeable param
-
close
public void close() throws IOException
Closes the set ofCloseable
s 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- 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 intoError
orRuntimeException
).close()
will not throw when this method was called before.- Returns:
- never returns
- Throws:
IOException
- wrapping the input, when needed
-
-