[LANG-1568] Predicate or.

This commit is contained in:
Gary Gregory 2020-06-25 10:52:37 -04:00
parent 180d948d7f
commit 268d4936d4
6 changed files with 124 additions and 8 deletions

View File

@ -84,6 +84,18 @@ default FailableBiPredicate<T, U, E> negate() {
return (final T t, final U u) -> !test(t, u);
}
/**
* Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableBiPredicate<T, U, E> or(final FailableBiPredicate<? super T, ? super U, E> other) {
Objects.requireNonNull(other);
return (final T t, final U u) -> test(t, u) || other.test(t, u);
}
/**
* Tests the predicate.
*

View File

@ -78,6 +78,18 @@ default FailableDoublePredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
* @throws NullPointerException if other is null
*/
default FailableDoublePredicate<E> or(final FailableDoublePredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/**
* Tests the predicate.
*

View File

@ -78,6 +78,18 @@ default FailableIntPredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableIntPredicate<E> or(final FailableIntPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/**
* Tests the predicate.
*

View File

@ -78,6 +78,18 @@ default FailableLongPredicate<E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableLongPredicate<E> or(final FailableLongPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/**
* Tests the predicate.
*

View File

@ -81,6 +81,18 @@ default FailablePredicate<T, E> negate() {
return t -> !test(t);
}
/**
* Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
*
* @param other a predicate that will be logically-ORed with this predicate.
* @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
* @throws NullPointerException if other is null
*/
default FailablePredicate<T, E> or(final FailablePredicate<? super T, E> other) {
Objects.requireNonNull(other);
return t -> test(t) || other.test(t);
}
/**
* Tests the predicate.
*

View File

@ -719,8 +719,10 @@ public void testBiPredicateAnd() throws Throwable {
assertFalse(FailableBiPredicate.FALSE.and(FailableBiPredicate.TRUE).test(null, null));
assertFalse(FailableBiPredicate.FALSE.and(FailableBiPredicate.FALSE).test(null, null));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailableBiPredicate.FALSE.and(null).test(null, null)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableBiPredicate.TRUE.and(null).test(null, null)));
assertThrows(NullPointerException.class,
() -> assertFalse(FailableBiPredicate.falsePredicate().and(null).test(null, null)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableBiPredicate.truePredicate().and(null).test(null, null)));
}
@Test
@ -731,6 +733,19 @@ public void testBiPredicateNegate() throws Throwable {
assertTrue(FailableBiPredicate.falsePredicate().negate().test(null, null));
}
@Test
public void testBiPredicateOr() throws Throwable {
assertTrue(FailableBiPredicate.TRUE.or(FailableBiPredicate.TRUE).test(null, null));
assertTrue(FailableBiPredicate.TRUE.or(FailableBiPredicate.FALSE).test(null, null));
assertTrue(FailableBiPredicate.FALSE.or(FailableBiPredicate.TRUE).test(null, null));
assertFalse(FailableBiPredicate.FALSE.or(FailableBiPredicate.FALSE).test(null, null));
// null tests
assertThrows(NullPointerException.class,
() -> assertFalse(FailableBiPredicate.falsePredicate().or(null).test(null, null)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableBiPredicate.truePredicate().or(null).test(null, null)));
}
@Test
public void testCallable() {
FailureOnOddInvocations.invocations = 0;
@ -793,8 +808,10 @@ public void testDoublePredicateAnd() throws Throwable {
assertFalse(FailableDoublePredicate.FALSE.and(FailableDoublePredicate.TRUE).test(0));
assertFalse(FailableDoublePredicate.FALSE.and(FailableDoublePredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailableDoublePredicate.FALSE.and(null).test(0)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableDoublePredicate.TRUE.and(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertFalse(FailableDoublePredicate.falsePredicate().and(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableDoublePredicate.truePredicate().and(null).test(0)));
}
@Test
@ -805,6 +822,19 @@ public void testDoublePredicateNegate() throws Throwable {
assertTrue(FailableDoublePredicate.falsePredicate().negate().test(0d));
}
@Test
public void testDoublePredicateOr() throws Throwable {
assertTrue(FailableDoublePredicate.TRUE.or(FailableDoublePredicate.TRUE).test(0));
assertTrue(FailableDoublePredicate.TRUE.or(FailableDoublePredicate.FALSE).test(0));
assertTrue(FailableDoublePredicate.FALSE.or(FailableDoublePredicate.TRUE).test(0));
assertFalse(FailableDoublePredicate.FALSE.or(FailableDoublePredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class,
() -> assertFalse(FailableDoublePredicate.falsePredicate().or(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableDoublePredicate.truePredicate().or(null).test(0)));
}
@Test
public void testDoubleUnaryOperatorAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
@ -1083,8 +1113,10 @@ public void testIntPredicateAnd() throws Throwable {
assertFalse(FailableIntPredicate.FALSE.and(FailableIntPredicate.TRUE).test(0));
assertFalse(FailableIntPredicate.FALSE.and(FailableIntPredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailableIntPredicate.FALSE.and(null).test(0)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableIntPredicate.TRUE.and(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertFalse(FailableIntPredicate.falsePredicate().and(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableIntPredicate.truePredicate().and(null).test(0)));
}
@Test
@ -1095,6 +1127,19 @@ public void testIntPredicateNegate() throws Throwable {
assertTrue(FailableIntPredicate.falsePredicate().negate().test(0));
}
@Test
public void testIntPredicateOr() throws Throwable {
assertTrue(FailableIntPredicate.TRUE.or(FailableIntPredicate.TRUE).test(0));
assertTrue(FailableIntPredicate.TRUE.or(FailableIntPredicate.FALSE).test(0));
assertTrue(FailableIntPredicate.FALSE.or(FailableIntPredicate.TRUE).test(0));
assertFalse(FailableIntPredicate.FALSE.or(FailableIntPredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class,
() -> assertFalse(FailableIntPredicate.falsePredicate().or(null).test(0)));
assertThrows(NullPointerException.class,
() -> assertTrue(FailableIntPredicate.truePredicate().or(null).test(0)));
}
@Test
public void testIntUnaryOperatorAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);
@ -1175,8 +1220,8 @@ public void testLongPredicateAnd() throws Throwable {
assertFalse(FailableLongPredicate.FALSE.and(FailableLongPredicate.TRUE).test(0));
assertFalse(FailableLongPredicate.FALSE.and(FailableLongPredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailableLongPredicate.FALSE.and(null).test(0)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableLongPredicate.TRUE.and(null).test(0)));
assertThrows(NullPointerException.class, () -> assertFalse(FailableLongPredicate.falsePredicate().and(null).test(0)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableLongPredicate.truePredicate().and(null).test(0)));
}
@Test
@ -1187,6 +1232,17 @@ public void testLongPredicateNegate() throws Throwable {
assertTrue(FailableLongPredicate.falsePredicate().negate().test(0L));
}
@Test
public void testLongPredicateOr() throws Throwable {
assertTrue(FailableLongPredicate.TRUE.or(FailableLongPredicate.TRUE).test(0));
assertTrue(FailableLongPredicate.TRUE.or(FailableLongPredicate.FALSE).test(0));
assertTrue(FailableLongPredicate.FALSE.or(FailableLongPredicate.TRUE).test(0));
assertFalse(FailableLongPredicate.FALSE.or(FailableLongPredicate.FALSE).test(0));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailableLongPredicate.falsePredicate().or(null).test(0)));
assertThrows(NullPointerException.class, () -> assertTrue(FailableLongPredicate.truePredicate().or(null).test(0)));
}
@Test
public void testLongUnaryOperatorAndThen() throws Throwable {
final Testable<?, ?> testable = new Testable<>(null);