From 268d4936d4136ce35af688617bbd6874b0854f4c Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 25 Jun 2020 10:52:37 -0400 Subject: [PATCH] [LANG-1568] Predicate or. --- .../lang3/function/FailableBiPredicate.java | 12 ++++ .../function/FailableDoublePredicate.java | 12 ++++ .../lang3/function/FailableIntPredicate.java | 12 ++++ .../lang3/function/FailableLongPredicate.java | 12 ++++ .../lang3/function/FailablePredicate.java | 12 ++++ .../lang3/function/FailableFunctionsTest.java | 72 ++++++++++++++++--- 6 files changed, 124 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/function/FailableBiPredicate.java b/src/main/java/org/apache/commons/lang3/function/FailableBiPredicate.java index f5894b32b..5258daaca 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableBiPredicate.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableBiPredicate.java @@ -84,6 +84,18 @@ public interface FailableBiPredicate { 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 or(final FailableBiPredicate other) { + Objects.requireNonNull(other); + return (final T t, final U u) -> test(t, u) || other.test(t, u); + } + /** * Tests the predicate. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoublePredicate.java b/src/main/java/org/apache/commons/lang3/function/FailableDoublePredicate.java index badc2a589..ec6b2cc6b 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableDoublePredicate.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoublePredicate.java @@ -78,6 +78,18 @@ public interface FailableDoublePredicate { 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 or(final FailableDoublePredicate other) { + Objects.requireNonNull(other); + return t -> test(t) || other.test(t); + } + /** * Tests the predicate. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntPredicate.java b/src/main/java/org/apache/commons/lang3/function/FailableIntPredicate.java index 57e0cb1d1..724d6bf4e 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableIntPredicate.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntPredicate.java @@ -78,6 +78,18 @@ public interface FailableIntPredicate { 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 or(final FailableIntPredicate other) { + Objects.requireNonNull(other); + return t -> test(t) || other.test(t); + } + /** * Tests the predicate. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongPredicate.java b/src/main/java/org/apache/commons/lang3/function/FailableLongPredicate.java index 154b1dfce..be97ca9b4 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableLongPredicate.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongPredicate.java @@ -78,6 +78,18 @@ public interface FailableLongPredicate { 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 or(final FailableLongPredicate other) { + Objects.requireNonNull(other); + return t -> test(t) || other.test(t); + } + /** * Tests the predicate. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailablePredicate.java b/src/main/java/org/apache/commons/lang3/function/FailablePredicate.java index 081464a69..d2ecbc565 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailablePredicate.java +++ b/src/main/java/org/apache/commons/lang3/function/FailablePredicate.java @@ -81,6 +81,18 @@ public interface FailablePredicate { 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 or(final FailablePredicate other) { + Objects.requireNonNull(other); + return t -> test(t) || other.test(t); + } + /** * Tests the predicate. * diff --git a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java index 085d83995..cac514d9a 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -719,8 +719,10 @@ public class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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 class FailableFunctionsTest { 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);