Παράδειγμα χρήσης τύπου συνάρτησης


@FunctionalInterface
interface Combiner {
    int combine(int a, char operator, int b);
}

public class CombinerExample {
    public static void main(String[] args) {
        Combiner arithmeticCombiner = (a, operator, b) -> {
            switch (operator) {
                case '+': return a + b;
                case '*': return a * b;
                default: throw new IllegalArgumentException("Invalid operator for arithmetic: " + operator);
            }
        };

        Combiner bitwiseCombiner = (a, operator, b) -> {
            switch (operator) {
                case '+': return a | b;
                case '*': return a & b;
                default: throw new IllegalArgumentException("Invalid operator for bitwise: " + operator);
            }
        };

        // Verify Arithmetic Combiner
        assert arithmeticCombiner.combine(5, '+', 3) == 8;
        assert arithmeticCombiner.combine(5, '*', 3) == 15;

        // Verify Bitwise Combiner
        assert bitwiseCombiner.combine(5, '+', 3) == 7;
        assert bitwiseCombiner.combine(5, '*', 3) == 1;
    }
}