I have quite a few places where I need to get an object, perform some side effects based on the state of that object, then finally return the object. For a specific class Foo, I can write a method
public Foo perform(Consumer<Foo> sideEffect) {
sideEffect.accept(this);
return this;
}
Simple enough, but I'd like to not have to rewrite it for every class, and I'd also like it to work for classes that aren't mine. I can't add this method to Object, and I'd rather not make a subclass of Consumer that returns the argument. Is there a better way to do this?