public class CloseableWrapper<T> extends AbstractCloseableWrapper<T,RuntimeException>
Example:
class Main {
private static void finalizeMyClass(MyClass obj) {
obj.shutdown();
}
public static void main() {
try (CloseableWrapper<MyClass> myClassWrapper =
CloseableWrapper.of(new MyClass(), Main::finalizeMyClass)) {
myClassWrapper.get().doSomething();
}
}
}
Modifier and Type | Method and Description |
---|---|
static <T> CloseableWrapper<T> |
of(T obj,
java.util.function.Consumer<T> closer)
Wrap an object with
AutoCloseable interface and provide a function to replace a close method. |
close, get
public static <T> CloseableWrapper<T> of(T obj, java.util.function.Consumer<T> 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