Consumer. The Java Consumer interface is a functional interface that represents an function that consumes a value without returning any value.
You can use this type of interfaces. I would like to start with the conditions where you need this type of construct. Default empty methods can be used to make implementation of any method optional. However, you can make a abstract adaptor class of such interface with a default implementation.
Consumer. A consumer is an operation that accepts a single input argument and returns no result; it just execute some operations on the argument.
For example, the Runnable interface can be viewed as the signature of a function that accepts nothing and returns nothing ( void ) because it has only one abstract method called run , which accepts nothing and returns nothing ( void ).
If I understand correctly you want a functional interface with a method void m()
. In which case you can simply use a Runnable
.
Just make your own
@FunctionalInterface
public interface Procedure {
void run();
default Procedure andThen(Procedure after){
return () -> {
this.run();
after.run();
};
}
default Procedure compose(Procedure before){
return () -> {
before.run();
this.run();
};
}
}
and use it like this
public static void main(String[] args){
Procedure procedure1 = () -> System.out.print("Hello");
Procedure procedure2 = () -> System.out.print("World");
procedure1.andThen(procedure2).run();
System.out.println();
procedure1.compose(procedure2).run();
}
and the output
HelloWorld
WorldHello
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