[LANG-1568] FailableDoubleUnaryOperator, FailableIntUnaryOperator,
FailableLongUnaryOperator.
This commit is contained in:
parent
5f2fa64137
commit
e12eb5d4d7
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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<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));
|
||||
|
@ -968,6 +1006,53 @@ public class FailableFunctionsTest {
|
|||
failablePredicate.test(1);
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@ -994,6 +1079,53 @@ public class FailableFunctionsTest {
|
|||
failablePredicate.test(1L);
|
||||
}
|
||||
|
||||
@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() {
|
||||
|
|
Loading…
Reference in New Issue