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

View File

@ -16,24 +16,27 @@
*/ */
package org.apache.commons.lang3; 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.FailableBiConsumer;
import org.apache.commons.lang3.Functions.FailableBiFunction; import org.apache.commons.lang3.Functions.FailableBiFunction;
import org.apache.commons.lang3.Functions.FailableCallable; import org.apache.commons.lang3.Functions.FailableCallable;
import org.apache.commons.lang3.Functions.FailableConsumer; import org.apache.commons.lang3.Functions.FailableConsumer;
import org.apache.commons.lang3.Functions.FailableFunction; import org.apache.commons.lang3.Functions.FailableFunction;
import org.apache.commons.lang3.Functions.FailableSupplier; import org.apache.commons.lang3.Functions.FailableSupplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
@ -95,12 +98,20 @@ class FunctionsTest {
public static class FailureOnOddInvocations { public static class FailureOnOddInvocations {
private static int invocation; private static int invocation;
FailureOnOddInvocations() throws SomeException {
private static void throwOnOdd() throws SomeException {
final int i = ++invocation; final int i = ++invocation;
if (i % 2 == 1) { if (i % 2 == 1) {
throw new SomeException("Odd Invocation: " + i); throw new SomeException("Odd Invocation: " + i);
} }
} }
static boolean failingBool() throws SomeException {
throwOnOdd();
return true;
}
FailureOnOddInvocations() throws SomeException {
throwOnOdd();
}
} }
public static class CloseableObject { public static class CloseableObject {
@ -392,12 +403,40 @@ class FunctionsTest {
assertNotNull(instance); 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 @Test
public void testAsSupplier() { public void testAsSupplier() {
FailureOnOddInvocations.invocation = 0; FailureOnOddInvocations.invocation = 0;
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> { final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> new FailureOnOddInvocations();
return new FailureOnOddInvocations();
};
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier); final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get()); UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();