-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Closeables.closeable() to allow for wrapping non-Closeables for use with the try-with-resources statement #279
Comments
So something like this? try (Closeable<ExecutorService> closeableService = new Closeable<>(Executors.newSingleThreadExecutor(), ExecutorService::shutdown)) {
ExecutorService service = closeableService.get();
// ...
} public class Closeable<Wrapped> extends AutoCloseable {
private final Wrapped wrapped;
private final CheckedRunnable cleanup;
public Closeable(Wrapped wrapped, CheckedRunnable cleanup) {
this.wrapped = wrapped;
this.cleanup = cleanup;
}
public Wrapped get() { return wrapped; }
@Override
public void close() {
try {
cleanup.run();
} catch (Throwable t) { // Some configurable error handling instead?
throw new RuntimeException(t);
}
}
} |
Yes, in that direction, although, I wonder if we can create an intersection type |
Ignoring the fact that we'd need to extract the interfaces to create the proxy, intersections seem to need both types to be concrete.
|
Indeed, also, they are not "denotable" types, i.e. the caller couldn't assign |
+1. Just needed this for Jersey responses:
Usage:
(this is using Lombok) |
Inspired by:
http://stackoverflow.com/q/41393417/521799
The text was updated successfully, but these errors were encountered: