From e12eb5d4d79398f4d3dab7a593efd767c59b74c6 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 25 Jun 2020 09:01:13 -0400 Subject: [PATCH] [LANG-1568] FailableDoubleUnaryOperator, FailableIntUnaryOperator, FailableLongUnaryOperator. --- .../function/FailableDoubleUnaryOperator.java | 94 +++++++++++ .../function/FailableIntUnaryOperator.java | 90 ++++++++++ .../function/FailableLongUnaryOperator.java | 90 ++++++++++ .../lang3/function/FailableFunctionsTest.java | 154 ++++++++++++++++-- 4 files changed, 417 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/apache/commons/lang3/function/FailableDoubleUnaryOperator.java create mode 100644 src/main/java/org/apache/commons/lang3/function/FailableIntUnaryOperator.java create mode 100644 src/main/java/org/apache/commons/lang3/function/FailableLongUnaryOperator.java diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoubleUnaryOperator.java b/src/main/java/org/apache/commons/lang3/function/FailableDoubleUnaryOperator.java new file mode 100644 index 000000000..13978d947 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoubleUnaryOperator.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import java.util.Objects; +import java.util.function.DoubleUnaryOperator; + +/** + * A functional interface like {@link DoubleUnaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ +public interface FailableDoubleUnaryOperator { + + /** NOP singleton */ + @SuppressWarnings("rawtypes") + FailableDoubleUnaryOperator NOP = t -> 0d; + + /** + * Returns a unary operator that always returns its input argument. + * + * @return a unary operator that always returns its input argument + */ + static FailableDoubleUnaryOperator identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableDoubleUnaryOperator nop() { + return NOP; + } + + /** + * Returns a composed {@code FailableDoubleUnaryOperator} like + * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}. + * + * @param after the operator to apply after this one. + * @return a composed {@code FailableDoubleUnaryOperator} like + * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}. + * @throws NullPointerException if after is null. + * @throws E Thrown when a consumer fails. + * @see #compose(FailableDoubleUnaryOperator) + */ + default FailableDoubleUnaryOperator andThen(FailableDoubleUnaryOperator after) throws E { + Objects.requireNonNull(after); + return (double t) -> after.applyAsDouble(applyAsDouble(t)); + } + + /** + * Applies this operator to the given operand. + * + * @param operand the operand + * @return the operator result + * @throws E Thrown when a consumer fails. + */ + double applyAsDouble(double operand) throws E; + + /** + * Returns a composed {@code FailableDoubleUnaryOperator} like + * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}. + * + * @param before the operator to apply before this one. + * @return a composed {@code FailableDoubleUnaryOperator} like + * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}. + * @throws NullPointerException if before is null. + * @throws E Thrown when a consumer fails. + * @see #andThen(FailableDoubleUnaryOperator) + */ + default FailableDoubleUnaryOperator compose(FailableDoubleUnaryOperator before) throws E { + Objects.requireNonNull(before); + return (double v) -> applyAsDouble(before.applyAsDouble(v)); + } +} diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntUnaryOperator.java b/src/main/java/org/apache/commons/lang3/function/FailableIntUnaryOperator.java new file mode 100644 index 000000000..2fe71f119 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntUnaryOperator.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import java.util.Objects; +import java.util.function.IntUnaryOperator; + +/** + * A functional interface like {@link IntUnaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ +public interface FailableIntUnaryOperator { + + /** NOP singleton */ + @SuppressWarnings("rawtypes") + FailableIntUnaryOperator NOP = t -> 0; + + /** + * Returns a unary operator that always returns its input argument. + * + * @return a unary operator that always returns its input argument + */ + static FailableIntUnaryOperator identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableIntUnaryOperator nop() { + return NOP; + } + + /** + * Returns a composed {@code FailableDoubleUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}. + * + * @param after the operator to apply after this one. + * @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}. + * @throws NullPointerException if after is null. + * @throws E Thrown when a consumer fails. + * @see #compose(FailableIntUnaryOperator) + */ + default FailableIntUnaryOperator andThen(FailableIntUnaryOperator after) throws E { + Objects.requireNonNull(after); + return (int t) -> after.applyAsInt(applyAsInt(t)); + } + + /** + * Applies this operator to the given operand. + * + * @param operand the operand + * @return the operator result + * @throws E Thrown when a consumer fails. + */ + int applyAsInt(int operand) throws E; + + /** + * Returns a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}. + * + * @param before the operator to apply before this one. + * @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}. + * @throws NullPointerException if before is null. + * @throws E Thrown when a consumer fails. + * @see #andThen(FailableIntUnaryOperator) + */ + default FailableIntUnaryOperator compose(FailableIntUnaryOperator before) throws E { + Objects.requireNonNull(before); + return (int v) -> applyAsInt(before.applyAsInt(v)); + } +} diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongUnaryOperator.java b/src/main/java/org/apache/commons/lang3/function/FailableLongUnaryOperator.java new file mode 100644 index 000000000..3248b02d3 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongUnaryOperator.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import java.util.Objects; +import java.util.function.LongUnaryOperator; + +/** + * A functional interface like {@link LongUnaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ +public interface FailableLongUnaryOperator { + + /** NOP singleton */ + @SuppressWarnings("rawtypes") + FailableLongUnaryOperator NOP = t -> 0L; + + /** + * Returns a unary operator that always returns its input argument. + * + * @return a unary operator that always returns its input argument + */ + static FailableLongUnaryOperator identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableLongUnaryOperator nop() { + return NOP; + } + + /** + * Returns a composed {@code FailableDoubleUnaryOperator} like {@link LongUnaryOperator#andThen(LongUnaryOperator)}. + * + * @param after the operator to apply after this one. + * @return a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#andThen(LongUnaryOperator)}. + * @throws NullPointerException if after is null. + * @throws E Thrown when a consumer fails. + * @see #compose(FailableLongUnaryOperator) + */ + default FailableLongUnaryOperator andThen(FailableLongUnaryOperator after) throws E { + Objects.requireNonNull(after); + return (long t) -> after.applyAsLong(applyAsLong(t)); + } + + /** + * Applies this operator to the given operand. + * + * @param operand the operand + * @return the operator result + * @throws E Thrown when a consumer fails. + */ + long applyAsLong(long operand) throws E; + + /** + * Returns a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}. + * + * @param before the operator to apply before this one. + * @return a composed {@code FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}. + * @throws NullPointerException if before is null. + * @throws E Thrown when a consumer fails. + * @see #andThen(FailableLongUnaryOperator) + */ + default FailableLongUnaryOperator compose(FailableLongUnaryOperator before) throws E { + Objects.requireNonNull(before); + return (long v) -> applyAsLong(before.applyAsLong(v)); + } +} 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 3a123cf90..9232762e4 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -36,7 +36,6 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -import org.apache.commons.lang3.Functions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -708,8 +707,7 @@ public class FailableFunctionsTest { assertNotNull(cause); assertTrue(cause instanceof SomeException); assertEquals("Odd Invocation: 1", cause.getMessage()); - final boolean instance = predicate.test(null, null); - assertNotNull(instance); + assertTrue(predicate.test(null, null)); } @Test @@ -725,13 +723,6 @@ public class FailableFunctionsTest { assertNotNull(instance); } - @Test - public void testConstructor() { - // We allow this, which must have been an omission to make the ctor private. - // We could make the ctor private in 4.0. - new Functions(); - } - @Test public void testConsumerAndThen() throws Throwable { final Testable testable = new Testable<>(null); @@ -774,12 +765,59 @@ public class FailableFunctionsTest { failablePredicate.test(1d); } + @Test + public void testDoubleUnaryOperatorAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableDoubleUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0d; + }; + final FailableDoubleUnaryOperator nop = FailableDoubleUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).applyAsDouble(0d)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).applyAsDouble(0d)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test + public void testDoubleUnaryOperatorCompose() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableDoubleUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0d; + }; + final FailableDoubleUnaryOperator nop = FailableDoubleUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.compose(failing).applyAsDouble(0d)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.compose(nop).applyAsDouble(0d)); + assertSame(ERROR, e); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.compose(null)); + } + + @Test + public void testDoubleUnaryOperatorIdentity() throws Throwable { + final FailableDoubleUnaryOperator nop = FailableDoubleUnaryOperator.identity(); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> nop.compose(null)); + } + @Test public void testFunction() { final Testable testable = new Testable<>(ILLEGAL_STATE_EXCEPTION); final FailableFunction failableFunction = th -> { testable.setThrowable(th); - return Integer.valueOf(testable.testAsInteger()); + return testable.testAsInteger(); }; final Function function = Failable.asFunction(failableFunction); Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ILLEGAL_STATE_EXCEPTION)); @@ -968,6 +1006,53 @@ public class FailableFunctionsTest { failablePredicate.test(1); } + @Test + public void testIntUnaryOperatorAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableIntUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0; + }; + final FailableIntUnaryOperator nop = FailableIntUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).applyAsInt(0)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).applyAsInt(0)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test + public void testIntUnaryOperatorCompose() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableIntUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0; + }; + final FailableIntUnaryOperator nop = FailableIntUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.compose(failing).applyAsInt(0)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.compose(nop).applyAsInt(0)); + assertSame(ERROR, e); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.compose(null)); + } + + @Test + public void testIntUnaryOperatorIdentity() throws Throwable { + final FailableIntUnaryOperator nop = FailableIntUnaryOperator.identity(); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> nop.compose(null)); + } + @Test public void testLongConsumerAndThen() throws Throwable { final Testable testable = new Testable<>(null); @@ -994,6 +1079,53 @@ public class FailableFunctionsTest { failablePredicate.test(1L); } + @Test + public void testLongUnaryOperatorAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableLongUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0L; + }; + final FailableLongUnaryOperator nop = FailableLongUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).applyAsLong(0L)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).applyAsLong(0L)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test + public void testLongUnaryOperatorCompose() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableLongUnaryOperator failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0L; + }; + final FailableLongUnaryOperator nop = FailableLongUnaryOperator.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.compose(failing).applyAsLong(0L)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.compose(nop).applyAsLong(0L)); + assertSame(ERROR, e); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.compose(null)); + } + + @Test + public void testLongUnaryOperatorIdentity() throws Throwable { + final FailableLongUnaryOperator nop = FailableLongUnaryOperator.identity(); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> nop.compose(null)); + } + @Test @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") public void testPredicate() {