Skip to content

Commit d62119d

Browse files
committed
adding BiPredicate
1 parent f9b10be commit d62119d

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.jnape.palatable.lambda.functions.specialized;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class BiPredicateTest {
9+
10+
@Test
11+
public void jufBiPredicateTest() {
12+
BiPredicate<String, String> equals = String::equals;
13+
14+
assertTrue(equals.test("abc", "abc"));
15+
assertFalse(equals.test("abc", ""));
16+
assertFalse(equals.test("", "abc"));
17+
}
18+
19+
@Test
20+
public void jufBiPredicateAnd() {
21+
BiPredicate<Integer, Integer> bothOdd = (x, y) -> x % 2 == 1 && y % 2 == 1;
22+
BiPredicate<Integer, Integer> greaterThan = (x, y) -> x.compareTo(y) > 0;
23+
24+
BiPredicate<Integer, Integer> conjunction = bothOdd.and(greaterThan);
25+
26+
assertTrue(conjunction.test(3, 1));
27+
assertFalse(conjunction.test(3, 2));
28+
assertFalse(conjunction.test(3, 5));
29+
assertFalse(conjunction.test(4, 1));
30+
}
31+
32+
@Test
33+
public void jufBiPredicateOr() {
34+
BiPredicate<Integer, Integer> bothOdd = (x, y) -> x % 2 == 1 && y % 2 == 1;
35+
BiPredicate<Integer, Integer> greaterThan = (x, y) -> x.compareTo(y) > 0;
36+
37+
BiPredicate<Integer, Integer> disjunction = bothOdd.or(greaterThan);
38+
39+
assertTrue(disjunction.test(3, 2));
40+
assertTrue(disjunction.test(1, 3));
41+
assertFalse(disjunction.test(1, 2));
42+
}
43+
44+
@Test
45+
public void jufBiPredicateNegate() {
46+
BiPredicate<String, String> equals = String::equals;
47+
48+
assertTrue(equals.test("a", "a"));
49+
assertFalse(equals.test("b", "a"));
50+
assertFalse(equals.negate().test("a", "a"));
51+
assertTrue(equals.negate().test("b", "a"));
52+
}
53+
}

0 commit comments

Comments
 (0)