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 ofCloseables 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 voidclose()Closes the set ofCloseables in reverse order.static Closercreate()Create instance of Closer.<C extends Closeable>
Cregister(C closeable)Add aCloseableto the list.RuntimeExceptionrethrow(@NotNull Throwable throwable)Sets a flag indicating that this method was called, then rethrows the given exception (potentially wrapped intoErrororRuntimeException).
-
-
-
Method Detail
-
create
public static Closer create()
Create instance of Closer.
-
register
@Nullable public <C extends Closeable> C register(@Nullable C closeable)
Add aCloseableto the list.- Parameters:
closeable-Closeableobject to be added- Returns:
- the closeable param
-
close
public void close() throws IOExceptionCloses the set ofCloseables 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:
closein interfaceAutoCloseable- Specified by:
closein 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 intoErrororRuntimeException).close()will not throw when this method was called before.- Returns:
- never returns
- Throws:
IOException- wrapping the input, when needed
-
-