I have an AutoCloseable class that executes a Runnable within close()
, like this:
static class Whatever implements AutoCloseable {
Runnable r;
public Whatever(Runnable r) {
this.r = r;
}
@Override
public void close() throws Exception {
r.run();
}
}
@Test
public void testAutoClose() throws Exception {
List<Boolean> updateMe = Arrays.asList(false);
AutoCloseable ac = new Whatever(() -> updateMe.set(0, true));
ac.close();
assertThat("close() failed to update list", updateMe, is(Collections.singletonList(true)));
}
The above works nicely. And enables me to have code like
new Whatever( () -> foo() );
to do "something".
But: there is one case, where, well nothing should happen for close()
. This works:
new Whatever( () -> {} );
As said, that does the job, yet I am wondering: is there a way to express that "empty Runnable" in any other way, for example using some sort of method reference?