This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new e12eb5d [LANG-1568] FailableDoubleUnaryOperator, FailableIntUnaryOperator, FailableLongUnaryOperator. e12eb5d is described below commit e12eb5d4d79398f4d3dab7a593efd767c59b74c6 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Jun 25 09:01:13 2020 -0400 [LANG-1568] FailableDoubleUnaryOperator, FailableIntUnaryOperator, FailableLongUnaryOperator. --- .../function/FailableDoubleUnaryOperator.java | 94 +++++++++++++ .../lang3/function/FailableIntUnaryOperator.java | 90 ++++++++++++ .../lang3/function/FailableLongUnaryOperator.java | 90 ++++++++++++ .../lang3/function/FailableFunctionsTest.java | 154 +++++++++++++++++++-- 4 files changed, 417 insertions(+), 11 deletions(-) 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 0000000..13978d9 --- /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 <E> Thrown exception. + * @since 3.11 + */ +public interface FailableDoubleUnaryOperator<E extends Throwable> { + + /** 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 <E extends Throwable> FailableDoubleUnaryOperator<E> identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableDoubleUnaryOperator<E> 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<E> andThen(FailableDoubleUnaryOperator<E> 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<E> compose(FailableDoubleUnaryOperator<E> 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 0000000..2fe71f1 --- /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 <E> Thrown exception. + * @since 3.11 + */ +public interface FailableIntUnaryOperator<E extends Throwable> { + + /** 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 <E extends Throwable> FailableIntUnaryOperator<E> identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableIntUnaryOperator<E> 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<E> andThen(FailableIntUnaryOperator<E> 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<E> compose(FailableIntUnaryOperator<E> 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 0000000..3248b02 --- /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 <E> Thrown exception. + * @since 3.11 + */ +public interface FailableLongUnaryOperator<E extends Throwable> { + + /** 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 <E extends Throwable> FailableLongUnaryOperator<E> identity() { + return t -> t; + } + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableLongUnaryOperator<E> 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<E> andThen(FailableLongUnaryOperator<E> 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<E> compose(FailableLongUnaryOperator<E> 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 3a123cf..9232762 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 @@ -726,13 +724,6 @@ public class FailableFunctionsTest { } @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); final FailableConsumer<Throwable, Throwable> failableConsumer = th -> { @@ -775,11 +766,58 @@ public class FailableFunctionsTest { } @Test + public void testDoubleUnaryOperatorAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableDoubleUnaryOperator<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0d; + }; + final FailableDoubleUnaryOperator<Throwable> 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<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0d; + }; + final FailableDoubleUnaryOperator<Throwable> 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<Throwable> 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<Throwable, Integer, Throwable> failableFunction = th -> { testable.setThrowable(th); - return Integer.valueOf(testable.testAsInteger()); + return testable.testAsInteger(); }; final Function<Throwable, Integer> function = Failable.asFunction(failableFunction); Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ILLEGAL_STATE_EXCEPTION)); @@ -969,6 +1007,53 @@ public class FailableFunctionsTest { } @Test + public void testIntUnaryOperatorAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableIntUnaryOperator<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0; + }; + final FailableIntUnaryOperator<Throwable> 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<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0; + }; + final FailableIntUnaryOperator<Throwable> 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<Throwable> 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); final FailableLongConsumer<Throwable> failing = t -> { @@ -995,6 +1080,53 @@ public class FailableFunctionsTest { } @Test + public void testLongUnaryOperatorAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableLongUnaryOperator<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0L; + }; + final FailableLongUnaryOperator<Throwable> 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<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0L; + }; + final FailableLongUnaryOperator<Throwable> 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<Throwable> 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() { FailureOnOddInvocations.invocations = 0;