A few years ago I created this set of generic functional interfaces with zero to three arguments, with and without return value. It covered 99% of my use cases since Java 8.
/**
* Functional Interfaces
*/
public class Functions {
/** 0 args, with return value */
@FunctionalInterface
public static interface Func0R<RETURN> {
public RETURN run() throws Exception;
default public RETURN runNoEx() {
try {
return run();
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 1 args, with return value */
@FunctionalInterface
public static interface Func1R<ARG1, RETURN> {
public RETURN run(ARG1 arg1) throws Exception;
default public RETURN runNoEx(ARG1 arg1) {
try {
return run(arg1);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 2 args, with return value */
@FunctionalInterface
public static interface Func2R<ARG1, ARG2, RETURN> {
public RETURN run(ARG1 arg1, ARG2 arg2) throws Exception;
default public RETURN runNoEx(ARG1 arg1, ARG2 arg2) {
try {
return run(arg1, arg2);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 3 args, with return value */
@FunctionalInterface
public static interface Func3R<ARG1, ARG2, ARG3, RETURN> {
public RETURN run(ARG1 arg1, ARG2 arg2, ARG3 arg3) throws Exception;
default public RETURN runNoEx(ARG1 arg1, ARG2 arg2, ARG3 arg3) {
try {
return run(arg1, arg2, arg3);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 0 args, no return value (void) */
@FunctionalInterface
public interface Func0V {
public void run() throws Exception;
default public void runNoEx() {
try {
run();
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 1 args, no return value (void) */
@FunctionalInterface
public static interface Func1V<ARG1> {
public void run(ARG1 arg1) throws Exception;
default public void runNoEx(ARG1 arg1) {
try {
run(arg1);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 2 args, no return value (void) */
@FunctionalInterface
public static interface Func2V<ARG1, ARG2> {
public void run(ARG1 arg1, ARG2 arg2) throws Exception;
default public void runNoEx(ARG1 arg1, ARG2 arg2) {
try {
run(arg1, arg2);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
/** 3 args, no return value (void) */
@FunctionalInterface
public static interface Func3V<ARG1, ARG2, ARG3> {
public void run(ARG1 arg1, ARG2 arg2, ARG3 arg3) throws Exception;
default public void runNoEx(ARG1 arg1, ARG2 arg2, ARG3 arg3) {
try {
run(arg1, arg2, arg3);
}
catch(Exception e) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
}
}
}
}
Sample usage:
public class Component {
private Func1R<Component, Integer> strategy;
public void setStrategy(Func1R<Component, Integer> strategy) {
this.strategy = strategy;
}
public void calc() {
int result = this.strategy.runNoEx(this);
// ...
}
}