|
| 1 | +package com.jnape.palatable.lambda.functions.specialized; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 4 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 5 | + |
| 6 | +/** |
| 7 | + * A specialized {@link Fn2} that returns a Boolean when fully applied, |
| 8 | + * or a {@link Predicate} when partially applied. |
| 9 | + * |
| 10 | + * @param <A> the first argument type |
| 11 | + * @param <B> the second argument type |
| 12 | + */ |
| 13 | +@FunctionalInterface |
| 14 | +public interface BiPredicate<A, B> extends Fn2<A, B, Boolean>, java.util.function.BiPredicate<A, B> { |
| 15 | + |
| 16 | + /** |
| 17 | + * {@inheritDoc} |
| 18 | + */ |
| 19 | + @Override |
| 20 | + default boolean test(A a, B b) { |
| 21 | + return apply(a, b); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritDoc} |
| 26 | + */ |
| 27 | + @Override |
| 28 | + default Predicate<B> apply(A a) { |
| 29 | + return Fn2.super.apply(a)::apply; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * {@inheritDoc} |
| 34 | + */ |
| 35 | + @Override |
| 36 | + default BiPredicate<B, A> flip() { |
| 37 | + return (BiPredicate<B, A>) Fn2.super.flip(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritDoc} |
| 42 | + */ |
| 43 | + @Override |
| 44 | + default Predicate<Tuple2<A, B>> uncurry() { |
| 45 | + return Fn2.super.uncurry()::apply; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Override of {@link java.util.function.BiPredicate#and(java.util.function.BiPredicate)}, returning an instance of |
| 50 | + * <code>BiPredicate</code> for compatibility. Left-to-right composition. |
| 51 | + * |
| 52 | + * @param other the biPredicate to test if this one succeeds |
| 53 | + * @return a biPredicate representing the conjunction of this biPredicate and other |
| 54 | + */ |
| 55 | + @Override |
| 56 | + default BiPredicate<A, B> and(java.util.function.BiPredicate<? super A, ? super B> other) { |
| 57 | + return (a, b) -> apply(a, b) && other.test(a, b); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Override of {@link java.util.function.BiPredicate#or(java.util.function.BiPredicate)}, returning an instance of |
| 62 | + * <code>BiPredicate</code> for compatibility. Left-to-right composition. |
| 63 | + * |
| 64 | + * @param other the biPredicate to test if this one fails |
| 65 | + * @return a biPredicate representing the disjunction of this biPredicate and other |
| 66 | + */ |
| 67 | + @Override |
| 68 | + default BiPredicate<A, B> or(java.util.function.BiPredicate<? super A, ? super B> other) { |
| 69 | + return (a, b) -> apply(a, b) || other.test(a, b); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Override of {@link java.util.function.BiPredicate#negate()}, returning an instance of <code>BiPredicate</code> |
| 74 | + * for compatibility. |
| 75 | + * |
| 76 | + * @return the negation of this biPredicate |
| 77 | + */ |
| 78 | + @Override |
| 79 | + default BiPredicate<A, B> negate() { |
| 80 | + return (a, b) -> !apply(a, b); |
| 81 | + } |
| 82 | +} |
0 commit comments