diff options
author | Gary Gregory <garydgregory@gmail.com> | 2020-06-25 09:01:13 -0400 |
---|---|---|
committer | Gary Gregory <garydgregory@gmail.com> | 2020-06-25 09:01:13 -0400 |
commit | e12eb5d4d79398f4d3dab7a593efd767c59b74c6 (patch) | |
tree | 40fa2c98dd438619424352d153aa22b5d0659804 /src/main/java/org/apache/commons/lang3/function | |
parent | 5f2fa64137db3c492b97271e6aa42ffd5598c475 (diff) | |
download | apache-commons-lang-e12eb5d4d79398f4d3dab7a593efd767c59b74c6.tar.gz |
[LANG-1568] FailableDoubleUnaryOperator, FailableIntUnaryOperator,
FailableLongUnaryOperator.
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/function')
3 files changed, 274 insertions, 0 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 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 <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 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 <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 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 <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)); + } +} |