
net.pincette.function.SideEffect Maven / Gradle / Ivy
package net.pincette.function;
import java.util.function.Supplier;
/**
* Allows to interleave statements in expressions. You can run it like this:
*
* {@code final int i = SideEffect.run(() -> doSomething()).andThenGet(() -> 3); }
*
* @author Werner Donn\u00e9
*/
@FunctionalInterface
public interface SideEffect extends Runnable {
/**
* Runs runnable
and then returns a SideEffect
on which the
* andThenGet
method can be called.
*
* @param sideEffect the side effect to be run.
* @param the type of the value that will eventually be returned.
* @return The SideEffect
object.
*/
static SideEffect run(Runnable sideEffect) {
return sideEffect::run;
}
/**
* This method should be called in order for the side effect to execute.
*
* @param supplier the function that produces the final result.
* @return The result of supplier
.
*/
default T andThenGet(Supplier supplier) {
run();
return supplier.get();
}
}