public class ThrowingCloseableWrapper<T,E extends Exception> extends AbstractCloseableWrapper<T,E>
Example:
class Main {
private static void finalizeMyClass(MyClass obj) throws IOException {
obj.shutdown();
}
public static void main() {
try (ThrowingCloseableWrapper<MyClass, IOException> myClassWrapper =
ThrowingCloseableWrapper.of(new MyClass(), Main::finalizeMyClass)) {
myClassWrapper.get().doSomething();
}
}
}
Modifier and Type | Method and Description |
---|---|
static <T,E extends Exception> |
of(T obj,
ThrowingConsumer<T,E> closer)
Wrap an object with
AutoCloseable interface and provide a function to replace a close method The wrapper is idempotent, i.e. |
close, get
public static <T,E extends Exception> ThrowingCloseableWrapper<T,E> of(T obj, ThrowingConsumer<T,E> closer)
AutoCloseable
interface and provide a function to replace a close
method The wrapper is idempotent, i.e. it will call closer function exactly once, even
if user calls close
multiple times.obj
- Any class that does not implement AutoCloseable interface which is hard to extendcloser
- A function to call on close