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?
Option 1
I would overload the constructor with a no-arg version.
public Whatever() {
this(() -> {});
}
() -> {} looks consice and self-explanatory to me.
Option 2
As an alternative, you could go with a utility class that defines an empty Runnable method
public final class EmptyUtils {
public static Runnable emptyRunnable() { return () -> {}; }
}
which you could statically import
new Whatever(emptyRunnable());
Option 3
I find this option particularly interesting (and you asked for a method reference)
new Whatever(EmptyRunnable::get);
even though it requires to write a (completely) dummy class
class EmptyRunnable {
public static void get() {}
}
How about a second constructor that takes no argument?
public Whatever() {
this(() -> {});
}
Then just do new Whatever(). It's not a direct answer to your question (Java does not really have such a no-op), but its a useful alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With