Code refactor to simplify Functions and new tests (#463)

* more tests

* formatting change as per requested by checkstyle

* DisplayName was added to the tests

* throw on odd refactored to eliminate copy paste
This commit is contained in:
Peter Verhas 2019-10-12 21:12:17 +02:00 committed by Gary Gregory
parent c42806cfa0
commit a32c188c32
2 changed files with 62 additions and 77 deletions

View File

@ -158,13 +158,7 @@ public class Functions {
* @return a standard {@code Runnable}
*/
public static Runnable asRunnable(FailableRunnable<?> pRunnable) {
return () -> {
try {
pRunnable.run();
} catch (Throwable t) {
throw rethrow(t);
}
};
return () -> run(pRunnable);
}
/**
@ -175,13 +169,7 @@ public class Functions {
* @return a standard {@code Consumer}
*/
public static <I> Consumer<I> asConsumer(FailableConsumer<I, ?> pConsumer) {
return (pInput) -> {
try {
pConsumer.accept(pInput);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput) -> accept(pConsumer, pInput);
}
/**
@ -192,13 +180,7 @@ public class Functions {
* @return a standard {@code Callable}
*/
public static <O> Callable<O> asCallable(FailableCallable<O, ?> pCallable) {
return () -> {
try {
return pCallable.call();
} catch (Throwable t) {
throw rethrow(t);
}
};
return () -> call(pCallable);
}
/**
@ -210,13 +192,7 @@ public class Functions {
* @return a standard {@code BiConsumer}
*/
public static <I1, I2> BiConsumer<I1, I2> asBiConsumer(FailableBiConsumer<I1, I2, ?> pConsumer) {
return (pInput1, pInput2) -> {
try {
pConsumer.accept(pInput1, pInput2);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput1, pInput2) -> accept(pConsumer, pInput1, pInput2);
}
/**
@ -228,13 +204,7 @@ public class Functions {
* @return a standard {@code Function}
*/
public static <I, O> Function<I, O> asFunction(FailableFunction<I, O, ?> pFunction) {
return (pInput) -> {
try {
return pFunction.apply(pInput);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput) -> apply(pFunction, pInput);
}
/**
@ -247,13 +217,7 @@ public class Functions {
* @return a standard {@code BiFunction}
*/
public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(FailableBiFunction<I1, I2, O, ?> pFunction) {
return (pInput1, pInput2) -> {
try {
return pFunction.apply(pInput1, pInput2);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput1, pInput2) -> apply(pFunction, pInput1, pInput2);
}
/**
@ -264,13 +228,7 @@ public class Functions {
* @return a standard {@code Predicate}
*/
public static <I> Predicate<I> asPredicate(FailablePredicate<I, ?> pPredicate) {
return (pInput) -> {
try {
return pPredicate.test(pInput);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput) -> test(pPredicate, pInput);
}
/**
@ -282,13 +240,7 @@ public class Functions {
* @return a standard {@code BiPredicate}
*/
public static <I1, I2> BiPredicate<I1, I2> asBiPredicate(FailableBiPredicate<I1, I2, ?> pPredicate) {
return (pInput1, pInput2) -> {
try {
return pPredicate.test(pInput1, pInput2);
} catch (Throwable t) {
throw rethrow(t);
}
};
return (pInput1, pInput2) -> test(pPredicate, pInput1, pInput2);
}
/**
@ -299,13 +251,7 @@ public class Functions {
* @return a standard {@code Supplier}
*/
public static <O> Supplier<O> asSupplier(FailableSupplier<O, ?> pSupplier) {
return () -> {
try {
return pSupplier.get();
} catch (Throwable t) {
throw rethrow(t);
}
};
return () -> get(pSupplier);
}
/**

View File

@ -16,24 +16,27 @@
*/
package org.apache.commons.lang3;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.lang3.Functions.FailableBiConsumer;
import org.apache.commons.lang3.Functions.FailableBiFunction;
import org.apache.commons.lang3.Functions.FailableCallable;
import org.apache.commons.lang3.Functions.FailableConsumer;
import org.apache.commons.lang3.Functions.FailableFunction;
import org.apache.commons.lang3.Functions.FailableSupplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
@ -95,12 +98,20 @@ class FunctionsTest {
public static class FailureOnOddInvocations {
private static int invocation;
FailureOnOddInvocations() throws SomeException {
private static void throwOnOdd() throws SomeException {
final int i = ++invocation;
if (i % 2 == 1) {
throw new SomeException("Odd Invocation: " + i);
}
}
static boolean failingBool() throws SomeException {
throwOnOdd();
return true;
}
FailureOnOddInvocations() throws SomeException {
throwOnOdd();
}
}
public static class CloseableObject {
@ -392,12 +403,40 @@ class FunctionsTest {
assertNotNull(instance);
}
@Test
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
public void testAsPredicate() {
FailureOnOddInvocations.invocation = 0;
final Functions.FailablePredicate<Object, Throwable> failablePredicate = (t) -> FailureOnOddInvocations.failingBool();
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
final boolean instance = predicate.test(null);
assertNotNull(instance);
}
@Test
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
public void testAsBiPredicate() {
FailureOnOddInvocations.invocation = 0;
final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
final boolean instance = predicate.test(null, null);
assertNotNull(instance);
}
@Test
public void testAsSupplier() {
FailureOnOddInvocations.invocation = 0;
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> {
return new FailureOnOddInvocations();
};
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> new FailureOnOddInvocations();
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
final Throwable cause = e.getCause();