As of Java 8, method references exist. I have a simple interface which I would like to use existing (preferably in a java.something package) method references to implement.
public class Calculator {
private interface BinaryFunction<A, B, R> {
R apply(A a, B b);
}
private <A, B, R> R apply(BinaryFunction<A, B, R> binaryFunction, A a, B b) {
return binaryFunction.apply(a, b);
}
public static void main(String [] args) {
Calculator calculator = new Calculator();
calculator.apply((a, b) -> a + b, 1, 2); // 3
calculator.apply(Math::addExact, 1, 2); // 3
calculator.apply((a, b) -> a || b, true, false); // true
// calculator.apply(?::?, true, false); // true
}
}
Math::addExact isn't the same as +, but it's close enough for me--I actually prefer getting ArithmeticException("integer overflow") thrown for my purposes.
If this were Python, I'd use the operator module, but I don't know if there is such a thing in Java.
So is there any static method in a java.something package that can go in the ?::? part which is equivalent to the || operator?
||on a pair of inputs?