[LANG-1568] Predicate add.

This commit is contained in:
Gary Gregory 2020-06-25 10:42:13 -04:00
parent c7d709b220
commit 180d948d7f
6 changed files with 113 additions and 15 deletions

View File

@ -70,9 +70,9 @@ static <T, U, E extends Throwable> FailableBiPredicate<T, U, E> truePredicate()
* @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableBiPredicate<T, U, E> and(FailableBiPredicate<? super T, ? super U, E> other) {
default FailableBiPredicate<T, U, E> and(final FailableBiPredicate<? super T, ? super U, E> other) {
Objects.requireNonNull(other);
return (T t, U u) -> test(t, u) && other.test(t, u);
return (final T t, final U u) -> test(t, u) && other.test(t, u);
}
/**

View File

@ -18,7 +18,6 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.DoublePredicate;
/**
@ -30,18 +29,6 @@
@FunctionalInterface
public interface FailableDoublePredicate<E extends Throwable> {
/**
* Returns a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableBiPredicate} like {@link BiPredicate#and(BiPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableDoublePredicate<E> and(FailableDoublePredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/** FALSE singleton */
@SuppressWarnings("rawtypes")
FailableDoublePredicate FALSE = t -> false;
@ -70,6 +57,18 @@ static <E extends Throwable> FailableDoublePredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableDoublePredicate} like {@link DoublePredicate#and(DoublePredicate)}.
* @throws NullPointerException if other is null
*/
default FailableDoublePredicate<E> and(final FailableDoublePredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.IntPredicate;
/**
@ -56,6 +57,18 @@ static <E extends Throwable> FailableIntPredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableIntPredicate} like {@link IntPredicate#and(IntPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableIntPredicate<E> and(final FailableIntPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.LongPredicate;
/**
@ -56,6 +57,18 @@ static <E extends Throwable> FailableLongPredicate<E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailableLongPredicate} like {@link LongPredicate#and(LongPredicate)}.
* @throws NullPointerException if other is null
*/
default FailableLongPredicate<E> and(final FailableLongPredicate<E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang3.function;
import java.util.Objects;
import java.util.function.Predicate;
/**
@ -59,6 +60,18 @@ static <T, E extends Throwable> FailablePredicate<T, E> truePredicate() {
return TRUE;
}
/**
* Returns a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
*
* @param other a predicate that will be logically-ANDed with this predicate.
* @return a composed {@code FailablePredicate} like {@link Predicate#and(Predicate)}.
* @throws NullPointerException if other is null
*/
default FailablePredicate<T, E> and(final FailablePredicate<? super T, E> other) {
Objects.requireNonNull(other);
return t -> test(t) && other.test(t);
}
/**
* Returns a predicate that negates this predicate.
*

View File

@ -712,6 +712,18 @@ public void testBiPredicate() {
assertTrue(predicate.test(null, null));
}
@Test
public void testBiPredicateAnd() throws Throwable {
assertTrue(FailableBiPredicate.TRUE.and(FailableBiPredicate.TRUE).test(null, null));
assertFalse(FailableBiPredicate.TRUE.and(FailableBiPredicate.FALSE).test(null, null));
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)));
}
@Test
public void testBiPredicateNegate() throws Throwable {
assertFalse(FailableBiPredicate.TRUE.negate().test(null, null));
assertFalse(FailableBiPredicate.truePredicate().negate().test(null, null));
@ -774,6 +786,18 @@ public void testDoublePredicate() throws Throwable {
failablePredicate.test(1d);
}
@Test
public void testDoublePredicateAnd() throws Throwable {
assertTrue(FailableDoublePredicate.TRUE.and(FailableDoublePredicate.TRUE).test(0));
assertFalse(FailableDoublePredicate.TRUE.and(FailableDoublePredicate.FALSE).test(0));
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)));
}
@Test
public void testDoublePredicateNegate() throws Throwable {
assertFalse(FailableDoublePredicate.TRUE.negate().test(0d));
assertFalse(FailableDoublePredicate.truePredicate().negate().test(0d));
@ -1052,6 +1076,18 @@ public void testIntPredicate() throws Throwable {
failablePredicate.test(1);
}
@Test
public void testIntPredicateAnd() throws Throwable {
assertTrue(FailableIntPredicate.TRUE.and(FailableIntPredicate.TRUE).test(0));
assertFalse(FailableIntPredicate.TRUE.and(FailableIntPredicate.FALSE).test(0));
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)));
}
@Test
public void testIntPredicateNegate() throws Throwable {
assertFalse(FailableIntPredicate.TRUE.negate().test(0));
assertFalse(FailableIntPredicate.truePredicate().negate().test(0));
@ -1132,6 +1168,18 @@ public void testLongPredicate() throws Throwable {
failablePredicate.test(1L);
}
@Test
public void testLongPredicateAnd() throws Throwable {
assertTrue(FailableLongPredicate.TRUE.and(FailableLongPredicate.TRUE).test(0));
assertFalse(FailableLongPredicate.TRUE.and(FailableLongPredicate.FALSE).test(0));
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)));
}
@Test
public void testLongPredicateNegate() throws Throwable {
assertFalse(FailableLongPredicate.TRUE.negate().test(0L));
assertFalse(FailableLongPredicate.truePredicate().negate().test(0L));
@ -1202,6 +1250,18 @@ public void testPredicate() {
assertNotNull(instance);
}
@Test
public void testPredicateAnd() throws Throwable {
assertTrue(FailablePredicate.TRUE.and(FailablePredicate.TRUE).test(null));
assertFalse(FailablePredicate.TRUE.and(FailablePredicate.FALSE).test(null));
assertFalse(FailablePredicate.FALSE.and(FailablePredicate.TRUE).test(null));
assertFalse(FailablePredicate.FALSE.and(FailablePredicate.FALSE).test(null));
// null tests
assertThrows(NullPointerException.class, () -> assertFalse(FailablePredicate.FALSE.and(null).test(null)));
assertThrows(NullPointerException.class, () -> assertTrue(FailablePredicate.TRUE.and(null).test(null)));
}
@Test
public void testPredicateNegate() throws Throwable {
assertFalse(FailablePredicate.TRUE.negate().test(null));
assertFalse(FailablePredicate.truePredicate().negate().test(null));