|
| 1 | +package com.jnape.palatable.lambda.functions.builtin.fn4; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.functions.Fn1; |
| 4 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 5 | +import com.jnape.palatable.lambda.functions.Fn3; |
| 6 | +import com.jnape.palatable.lambda.functions.Fn4; |
| 7 | + |
| 8 | +import java.util.function.Function; |
| 9 | + |
| 10 | +public final class IfThenElse<A, B> implements Fn4<Function<? super A, Boolean>, Function<? super A, ? extends B>, Function<? super A, ? extends B>, A, B> { |
| 11 | + |
| 12 | + private static final IfThenElse INSTANCE = new IfThenElse(); |
| 13 | + |
| 14 | + private IfThenElse() { |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public B apply(Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase, |
| 19 | + Function<? super A, ? extends B> elseCase, A a) { |
| 20 | + return predicate.apply(a) ? thenCase.apply(a) : elseCase.apply(a); |
| 21 | + } |
| 22 | + |
| 23 | + @SuppressWarnings("unchecked") |
| 24 | + public static <A, B> IfThenElse<A, B> ifThenElse() { |
| 25 | + return INSTANCE; |
| 26 | + } |
| 27 | + |
| 28 | + public static <A, B> Fn3<Function<? super A, ? extends B>, Function<? super A, ? extends B>, A, B> ifThenElse( |
| 29 | + Function<? super A, Boolean> predicate) { |
| 30 | + return IfThenElse.<A, B>ifThenElse().apply(predicate); |
| 31 | + } |
| 32 | + |
| 33 | + public static <A, B> Fn2<Function<? super A, ? extends B>, A, B> ifThenElse( |
| 34 | + Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase) { |
| 35 | + return IfThenElse.<A, B>ifThenElse(predicate).apply(thenCase); |
| 36 | + } |
| 37 | + |
| 38 | + public static <A, B> Fn1<A, B> ifThenElse( |
| 39 | + Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase, |
| 40 | + Function<? super A, ? extends B> elseCase) { |
| 41 | + return IfThenElse.<A, B>ifThenElse(predicate, thenCase).apply(elseCase); |
| 42 | + } |
| 43 | + |
| 44 | + public static <A, B> B ifThenElse( |
| 45 | + Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase, |
| 46 | + Function<? super A, ? extends B> elseCase, |
| 47 | + A a) { |
| 48 | + return ifThenElse(predicate, thenCase, elseCase).apply(a); |
| 49 | + } |
| 50 | +} |
0 commit comments