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 540ec09 Sort members.
540ec09 is described below
commit 540ec09e25a3ec60fbe0a77dfc491475229caba7
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jun 12 11:36:13 2020 -0400
Sort members.
---
.../java/org/apache/commons/lang3/Functions.java | 120 +++---
.../org/apache/commons/lang3/FunctionsTest.java | 458 ++++++++++-----------
2 files changed, 289 insertions(+), 289 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/Functions.java
b/src/main/java/org/apache/commons/lang3/Functions.java
index ae03211..f854f02 100644
--- a/src/main/java/org/apache/commons/lang3/Functions.java
+++ b/src/main/java/org/apache/commons/lang3/Functions.java
@@ -136,6 +136,24 @@ public class Functions {
}
/**
+ * A functional interface like {@link BooleanSupplier} that declares a
Throwable.
+ *
+ * @param <T> Thrown exception.
+ * @since 3.11
+ */
+ @FunctionalInterface
+ public interface FailableBooleanSupplier<T extends Throwable> {
+
+ /**
+ * Supplies a boolean.
+ *
+ * @return a result
+ * @throws T if the supplier fails
+ */
+ boolean getAsBoolean() throws T;
+ }
+
+ /**
* A functional interface like {@link java.util.concurrent.Callable} that
declares a Throwable.
*
* @param <R> Return type.
@@ -173,148 +191,130 @@ public class Functions {
}
/**
- * A functional interface like {@link Function} that declares a Throwable.
- *
- * @param <I> Input type 1.
- * @param <R> Return type.
- * @param <T> Thrown exception.
- */
- @FunctionalInterface
- public interface FailableFunction<I, R, T extends Throwable> {
-
- /**
- * Applies this function.
- *
- * @param input the input for the function
- * @return the result of the function
- * @throws T if the function fails
- */
- R apply(I input) throws T;
- }
-
- /**
- * A functional interface like {@link Predicate} that declares a Throwable.
+ * A functional interface like {@link DoubleSupplier} that declares a
Throwable.
*
- * @param <I> Predicate type 1.
* @param <T> Thrown exception.
+ * @since 3.11
*/
@FunctionalInterface
- public interface FailablePredicate<I, T extends Throwable> {
+ public interface FailableDoubleSupplier<T extends Throwable> {
/**
- * Tests the predicate.
+ * Supplies a double.
*
- * @param object the object to test the predicate on
- * @return the predicate's evaluation
- * @throws T if the predicate fails
+ * @return a result
+ * @throws T if the supplier fails
*/
- boolean test(I object) throws T;
+ double getAsDouble() throws T;
}
/**
- * A functional interface like {@link Runnable} that declares a Throwable.
+ * A functional interface like {@link Function} that declares a Throwable.
*
+ * @param <I> Input type 1.
+ * @param <R> Return type.
* @param <T> Thrown exception.
*/
@FunctionalInterface
- public interface FailableRunnable<T extends Throwable> {
+ public interface FailableFunction<I, R, T extends Throwable> {
/**
- * Runs the function.
+ * Applies this function.
*
+ * @param input the input for the function
+ * @return the result of the function
* @throws T if the function fails
*/
- void run() throws T;
+ R apply(I input) throws T;
}
/**
- * A functional interface like {@link Supplier} that declares a Throwable.
+ * A functional interface like {@link IntSupplier} that declares a
Throwable.
*
- * @param <R> Return type.
* @param <T> Thrown exception.
+ * @since 3.11
*/
@FunctionalInterface
- public interface FailableSupplier<R, T extends Throwable> {
+ public interface FailableIntSupplier<T extends Throwable> {
/**
- * Supplies an object
+ * Supplies an int.
*
* @return a result
* @throws T if the supplier fails
*/
- R get() throws T;
+ int getAsInt() throws T;
}
/**
- * A functional interface like {@link BooleanSupplier} that declares a
Throwable.
+ * A functional interface like {@link LongSupplier} that declares a
Throwable.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
- public interface FailableBooleanSupplier<T extends Throwable> {
+ public interface FailableLongSupplier<T extends Throwable> {
/**
- * Supplies a boolean.
+ * Supplies a long.
*
* @return a result
* @throws T if the supplier fails
*/
- boolean getAsBoolean() throws T;
+ long getAsLong() throws T;
}
/**
- * A functional interface like {@link DoubleSupplier} that declares a
Throwable.
+ * A functional interface like {@link Predicate} that declares a Throwable.
*
+ * @param <I> Predicate type 1.
* @param <T> Thrown exception.
- * @since 3.11
*/
@FunctionalInterface
- public interface FailableDoubleSupplier<T extends Throwable> {
+ public interface FailablePredicate<I, T extends Throwable> {
/**
- * Supplies a double.
+ * Tests the predicate.
*
- * @return a result
- * @throws T if the supplier fails
+ * @param object the object to test the predicate on
+ * @return the predicate's evaluation
+ * @throws T if the predicate fails
*/
- double getAsDouble() throws T;
+ boolean test(I object) throws T;
}
/**
- * A functional interface like {@link IntSupplier} that declares a
Throwable.
+ * A functional interface like {@link Runnable} that declares a Throwable.
*
* @param <T> Thrown exception.
- * @since 3.11
*/
@FunctionalInterface
- public interface FailableIntSupplier<T extends Throwable> {
+ public interface FailableRunnable<T extends Throwable> {
/**
- * Supplies an int.
+ * Runs the function.
*
- * @return a result
- * @throws T if the supplier fails
+ * @throws T if the function fails
*/
- int getAsInt() throws T;
+ void run() throws T;
}
/**
- * A functional interface like {@link LongSupplier} that declares a
Throwable.
+ * A functional interface like {@link Supplier} that declares a Throwable.
*
+ * @param <R> Return type.
* @param <T> Thrown exception.
- * @since 3.11
*/
@FunctionalInterface
- public interface FailableLongSupplier<T extends Throwable> {
+ public interface FailableSupplier<R, T extends Throwable> {
/**
- * Supplies a long.
+ * Supplies an object
*
* @return a result
* @throws T if the supplier fails
*/
- long getAsLong() throws T;
+ R get() throws T;
}
/**
diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java
b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
index 63375cd..c9e6acf 100644
--- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java
+++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
@@ -46,6 +46,53 @@ import org.junit.jupiter.api.Test;
class FunctionsTest {
+ public static class CloseableObject {
+ private boolean closed;
+
+ public void close() {
+ closed = true;
+ }
+
+ public boolean isClosed() {
+ return closed;
+ }
+
+ public void reset() {
+ closed = false;
+ }
+
+ public void run(final Throwable pTh) throws Throwable {
+ if (pTh != null) {
+ throw pTh;
+ }
+ }
+ }
+
+ public static class FailureOnOddInvocations {
+ private static int invocations;
+
+ static boolean failingBool() throws SomeException {
+ throwOnOdd();
+ return true;
+ }
+
+ private static void throwOnOdd() throws SomeException {
+ final int i = ++invocations;
+ if (i % 2 == 1) {
+ throw new SomeException("Odd Invocation: " + i);
+ }
+ }
+
+ FailureOnOddInvocations() throws SomeException {
+ throwOnOdd();
+ }
+
+ boolean getAsBoolean() throws SomeException {
+ throwOnOdd();
+ return true;
+ }
+ }
+
public static class SomeException extends Exception {
private static final long serialVersionUID = -4965704778119283411L;
@@ -88,38 +135,19 @@ class FunctionsTest {
}
}
- public Integer testInteger() throws Throwable {
- return testInteger(t);
- }
-
- public int testIntPrimitive() throws Throwable {
- return testIntPrimitive(t);
- }
-
- public long testLongPrimitive() throws Throwable {
- return testLongPrimitive(t);
- }
-
public boolean testBooleanPrimitive() throws Throwable {
return testBooleanPrimitive(t);
}
- public double testDoublePrimitive() throws Throwable {
- return testDoublePrimitive(t);
- }
-
- public Integer testInteger(final Throwable throwable) throws Throwable
{
+ public boolean testBooleanPrimitive(final Throwable throwable) throws
Throwable {
if (throwable != null) {
throw throwable;
}
- return 0;
+ return false;
}
- public int testIntPrimitive(final Throwable throwable) throws
Throwable {
- if (throwable != null) {
- throw throwable;
- }
- return 0;
+ public double testDoublePrimitive() throws Throwable {
+ return testDoublePrimitive(t);
}
public double testDoublePrimitive(final Throwable throwable) throws
Throwable {
@@ -129,124 +157,60 @@ class FunctionsTest {
return 0;
}
- public long testLongPrimitive(final Throwable throwable) throws
Throwable {
- if (throwable != null) {
- throw throwable;
- }
- return 0;
+ public Integer testInteger() throws Throwable {
+ return testInteger(t);
}
- public boolean testBooleanPrimitive(final Throwable throwable) throws
Throwable {
+ public Integer testInteger(final Throwable throwable) throws Throwable
{
if (throwable != null) {
throw throwable;
}
- return false;
- }
- }
-
- public static class FailureOnOddInvocations {
- private static int invocations;
-
- private static void throwOnOdd() throws SomeException {
- final int i = ++invocations;
- if (i % 2 == 1) {
- throw new SomeException("Odd Invocation: " + i);
- }
- }
-
- static boolean failingBool() throws SomeException {
- throwOnOdd();
- return true;
- }
-
- FailureOnOddInvocations() throws SomeException {
- throwOnOdd();
+ return 0;
}
- boolean getAsBoolean() throws SomeException {
- throwOnOdd();
- return true;
+ public int testIntPrimitive() throws Throwable {
+ return testIntPrimitive(t);
}
- }
- public static class CloseableObject {
- private boolean closed;
-
- public void run(final Throwable pTh) throws Throwable {
- if (pTh != null) {
- throw pTh;
+ public int testIntPrimitive(final Throwable throwable) throws
Throwable {
+ if (throwable != null) {
+ throw throwable;
}
+ return 0;
}
- public void reset() {
- closed = false;
- }
-
- public void close() {
- closed = true;
+ public long testLongPrimitive() throws Throwable {
+ return testLongPrimitive(t);
}
- public boolean isClosed() {
- return closed;
+ public long testLongPrimitive(final Throwable throwable) throws
Throwable {
+ if (throwable != null) {
+ throw throwable;
+ }
+ return 0;
}
}
@Test
- void testRunnable() {
- FailureOnOddInvocations.invocations = 0;
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
- final Throwable cause = e.getCause();
- assertNotNull(cause);
- assertTrue(cause instanceof SomeException);
- assertEquals("Odd Invocation: 1", cause.getMessage());
-
- // Even invocations, should not throw an exception
- Functions.run(FailureOnOddInvocations::new);
- }
-
- @Test
- void testAsRunnable() {
- FailureOnOddInvocations.invocations = 0;
- final Runnable runnable =
Functions.asRunnable(FailureOnOddInvocations::new);
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
- final Throwable cause = e.getCause();
- assertNotNull(cause);
- assertTrue(cause instanceof SomeException);
- assertEquals("Odd Invocation: 1", cause.getMessage());
+ void testAcceptBiConsumer() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(null);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.accept(Testable::test, testable, ise));
+ assertSame(ise, e);
- // Even invocations, should not throw an exception
- runnable.run();
- }
+ final Error error = new OutOfMemoryError();
+ e = assertThrows(OutOfMemoryError.class, () ->
Functions.accept(Testable::test, testable, error));
+ assertSame(error, e);
- @Test
- void testCallable() {
- FailureOnOddInvocations.invocations = 0;
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
- final Throwable cause = e.getCause();
- assertNotNull(cause);
- assertTrue(cause instanceof SomeException);
- assertEquals("Odd Invocation: 1", cause.getMessage());
- final FailureOnOddInvocations instance =
Functions.call(FailureOnOddInvocations::new);
- assertNotNull(instance);
- }
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () ->
Functions.accept(Testable::test, testable, ioe));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
- @Test
- void testAsCallable() {
- FailureOnOddInvocations.invocations = 0;
- final FailableCallable<FailureOnOddInvocations, SomeException>
failableCallable = FailureOnOddInvocations::new;
- final Callable<FailureOnOddInvocations> callable =
Functions.asCallable(failableCallable);
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> callable.call());
- final Throwable cause = e.getCause();
- assertNotNull(cause);
- assertTrue(cause instanceof SomeException);
- assertEquals("Odd Invocation: 1", cause.getMessage());
- final FailureOnOddInvocations instance;
- try {
- instance = callable.call();
- } catch (final Exception ex) {
- throw Functions.rethrow(ex);
- }
- assertNotNull(instance);
+ testable.setThrowable(null);
+ Functions.accept(Testable::test, testable, (Throwable) null);
}
@Test
@@ -273,49 +237,50 @@ class FunctionsTest {
}
@Test
- void testAsConsumer() {
+ public void testApplyBiFunction() {
final IllegalStateException ise = new IllegalStateException();
- final Testable testable = new Testable(ise);
- final Consumer<Testable> consumer =
Functions.asConsumer(Testable::test);
- Throwable e = assertThrows(IllegalStateException.class, () ->
consumer.accept(testable));
+ final Testable testable = new Testable(null);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.apply(Testable::testInteger, testable, ise));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
- testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () ->
consumer.accept(testable));
+ e = assertThrows(OutOfMemoryError.class, () ->
Functions.apply(Testable::testInteger, testable, error));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
- testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
consumer.accept(testable));
+ e = assertThrows(UncheckedIOException.class, () ->
Functions.apply(Testable::testInteger, testable, ioe));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
- testable.setThrowable(null);
- Functions.accept(Testable::test, testable);
+ final Integer i = Functions.apply(Testable::testInteger, testable,
(Throwable) null);
+ assertNotNull(i);
+ assertEquals(0, i.intValue());
}
@Test
- void testAcceptBiConsumer() {
+ public void testApplyFunction() {
final IllegalStateException ise = new IllegalStateException();
- final Testable testable = new Testable(null);
- Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.accept(Testable::test, testable, ise));
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.apply(Testable::testInteger, testable));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
- e = assertThrows(OutOfMemoryError.class, () ->
Functions.accept(Testable::test, testable, error));
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () ->
Functions.apply(Testable::testInteger, testable));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
Functions.accept(Testable::test, testable, ioe));
+ e = assertThrows(UncheckedIOException.class, () ->
Functions.apply(Testable::testInteger, testable));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
- Functions.accept(Testable::test, testable, (Throwable) null);
+ final Integer i = Functions.apply(Testable::testInteger, testable);
+ assertNotNull(i);
+ assertEquals(0, i.intValue());
}
@Test
@@ -344,141 +309,169 @@ class FunctionsTest {
}
@Test
- public void testApplyFunction() {
+ public void testAsBiFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
- Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.apply(Testable::testInteger, testable));
+ final FailableBiFunction<Testable, Throwable, Integer, Throwable>
failableBiFunction = (t, th) -> {
+ t.setThrowable(th);
+ return Integer.valueOf(t.testInteger());
+ };
+ final BiFunction<Testable, Throwable, Integer> biFunction =
Functions.asBiFunction(failableBiFunction);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
biFunction.apply(testable, ise));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () ->
Functions.apply(Testable::testInteger, testable));
+ e = assertThrows(OutOfMemoryError.class, () ->
biFunction.apply(testable, error));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
Functions.apply(Testable::testInteger, testable));
+ e = assertThrows(UncheckedIOException.class, () ->
biFunction.apply(testable, ioe));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
- testable.setThrowable(null);
- final Integer i = Functions.apply(Testable::testInteger, testable);
- assertNotNull(i);
- assertEquals(0, i.intValue());
+ assertEquals(0, biFunction.apply(testable, null).intValue());
}
@Test
- public void testAsFunction() {
- final IllegalStateException ise = new IllegalStateException();
- final Testable testable = new Testable(ise);
- final FailableFunction<Throwable, Integer, Throwable> failableFunction
= th -> {
- testable.setThrowable(th);
- return Integer.valueOf(testable.testInteger());
- };
- final Function<Throwable, Integer> function =
Functions.asFunction(failableFunction);
- Throwable e = assertThrows(IllegalStateException.class, () ->
function.apply(ise));
- assertSame(ise, e);
-
- final Error error = new OutOfMemoryError();
- testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () -> function.apply(error));
- assertSame(error, e);
-
- final IOException ioe = new IOException("Unknown I/O error");
- testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
function.apply(ioe));
- final Throwable t = e.getCause();
- assertNotNull(t);
- assertSame(ioe, t);
+ @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to
-> BiPredicate ")
+ public void testAsBiPredicate() {
+ FailureOnOddInvocations.invocations = 0;
+ final Functions.FailableBiPredicate<Object, Object, Throwable>
failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
+ final BiPredicate<?, ?> predicate =
Functions.asBiPredicate(failableBiPredicate);
+ final 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);
+ }
- assertEquals(0, function.apply(null).intValue());
+ @Test
+ void testAsCallable() {
+ FailureOnOddInvocations.invocations = 0;
+ final FailableCallable<FailureOnOddInvocations, SomeException>
failableCallable = FailureOnOddInvocations::new;
+ final Callable<FailureOnOddInvocations> callable =
Functions.asCallable(failableCallable);
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> callable.call());
+ final Throwable cause = e.getCause();
+ assertNotNull(cause);
+ assertTrue(cause instanceof SomeException);
+ assertEquals("Odd Invocation: 1", cause.getMessage());
+ final FailureOnOddInvocations instance;
+ try {
+ instance = callable.call();
+ } catch (final Exception ex) {
+ throw Functions.rethrow(ex);
+ }
+ assertNotNull(instance);
}
@Test
- public void testApplyBiFunction() {
+ void testAsConsumer() {
final IllegalStateException ise = new IllegalStateException();
- final Testable testable = new Testable(null);
- Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.apply(Testable::testInteger, testable, ise));
+ final Testable testable = new Testable(ise);
+ final Consumer<Testable> consumer =
Functions.asConsumer(Testable::test);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
consumer.accept(testable));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
- e = assertThrows(OutOfMemoryError.class, () ->
Functions.apply(Testable::testInteger, testable, error));
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () ->
consumer.accept(testable));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
- e = assertThrows(UncheckedIOException.class, () ->
Functions.apply(Testable::testInteger, testable, ioe));
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () ->
consumer.accept(testable));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
- final Integer i = Functions.apply(Testable::testInteger, testable,
(Throwable) null);
- assertNotNull(i);
- assertEquals(0, i.intValue());
+ testable.setThrowable(null);
+ Functions.accept(Testable::test, testable);
}
@Test
- public void testAsBiFunction() {
+ public void testAsFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
- final FailableBiFunction<Testable, Throwable, Integer, Throwable>
failableBiFunction = (t, th) -> {
- t.setThrowable(th);
- return Integer.valueOf(t.testInteger());
+ final FailableFunction<Throwable, Integer, Throwable> failableFunction
= th -> {
+ testable.setThrowable(th);
+ return Integer.valueOf(testable.testInteger());
};
- final BiFunction<Testable, Throwable, Integer> biFunction =
Functions.asBiFunction(failableBiFunction);
- Throwable e = assertThrows(IllegalStateException.class, () ->
biFunction.apply(testable, ise));
+ final Function<Throwable, Integer> function =
Functions.asFunction(failableFunction);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
function.apply(ise));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () ->
biFunction.apply(testable, error));
+ e = assertThrows(OutOfMemoryError.class, () -> function.apply(error));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
biFunction.apply(testable, ioe));
+ e = assertThrows(UncheckedIOException.class, () ->
function.apply(ioe));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
- assertEquals(0, biFunction.apply(testable, null).intValue());
+ assertEquals(0, function.apply(null).intValue());
}
@Test
- public void testGetFromSupplier() {
+ @DisplayName("Test that asPredicate(FailablePredicate) is converted to ->
Predicate ")
+ public void testAsPredicate() {
FailureOnOddInvocations.invocations = 0;
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
+ final Functions.FailablePredicate<Object, Throwable> failablePredicate
= t -> FailureOnOddInvocations.failingBool();
+ final Predicate<?> predicate =
Functions.asPredicate(failablePredicate);
+ final 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 FailureOnOddInvocations instance =
Functions.call(FailureOnOddInvocations::new);
+ final boolean instance = predicate.test(null);
assertNotNull(instance);
}
@Test
- public void testGetSupplier() {
- final IllegalStateException ise = new IllegalStateException();
- final Testable testable = new Testable(ise);
- Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.get(testable::testInteger));
- assertSame(ise, e);
+ void testAsRunnable() {
+ FailureOnOddInvocations.invocations = 0;
+ final Runnable runnable =
Functions.asRunnable(FailureOnOddInvocations::new);
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
+ final Throwable cause = e.getCause();
+ assertNotNull(cause);
+ assertTrue(cause instanceof SomeException);
+ assertEquals("Odd Invocation: 1", cause.getMessage());
- final Error error = new OutOfMemoryError();
- testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () ->
Functions.get(testable::testInteger));
- assertSame(error, e);
+ // Even invocations, should not throw an exception
+ runnable.run();
+ }
- final IOException ioe = new IOException("Unknown I/O error");
- testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () ->
Functions.get(testable::testInteger));
- final Throwable t = e.getCause();
- assertNotNull(t);
- assertSame(ioe, t);
+ @Test
+ public void testAsSupplier() {
+ FailureOnOddInvocations.invocations = 0;
+ final FailableSupplier<FailureOnOddInvocations, Throwable>
failableSupplier = FailureOnOddInvocations::new;
+ final Supplier<FailureOnOddInvocations> supplier =
Functions.asSupplier(failableSupplier);
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
+ final Throwable cause = e.getCause();
+ assertNotNull(cause);
+ assertTrue(cause instanceof SomeException);
+ assertEquals("Odd Invocation: 1", cause.getMessage());
+ assertNotNull(supplier.get());
+ }
- testable.setThrowable(null);
- final Integer i = Functions.apply(Testable::testInteger, testable);
- assertNotNull(i);
- assertEquals(0, i.intValue());
+ @Test
+ void testCallable() {
+ FailureOnOddInvocations.invocations = 0;
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
+ final Throwable cause = e.getCause();
+ assertNotNull(cause);
+ assertTrue(cause instanceof SomeException);
+ assertEquals("Odd Invocation: 1", cause.getMessage());
+ final FailureOnOddInvocations instance =
Functions.call(FailureOnOddInvocations::new);
+ assertNotNull(instance);
}
@Test
@@ -576,46 +569,53 @@ class FunctionsTest {
}
@Test
- @DisplayName("Test that asPredicate(FailablePredicate) is converted to ->
Predicate ")
- public void testAsPredicate() {
+ public void testGetFromSupplier() {
FailureOnOddInvocations.invocations = 0;
- final Functions.FailablePredicate<Object, Throwable> failablePredicate
= t -> FailureOnOddInvocations.failingBool();
- final Predicate<?> predicate =
Functions.asPredicate(failablePredicate);
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
- final boolean instance = predicate.test(null);
+ final FailureOnOddInvocations instance =
Functions.call(FailureOnOddInvocations::new);
assertNotNull(instance);
}
@Test
- @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to
-> BiPredicate ")
- public void testAsBiPredicate() {
- FailureOnOddInvocations.invocations = 0;
- final Functions.FailableBiPredicate<Object, Object, Throwable>
failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
- final BiPredicate<?, ?> predicate =
Functions.asBiPredicate(failableBiPredicate);
- final 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);
+ public void testGetSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () ->
Functions.get(testable::testInteger));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () ->
Functions.get(testable::testInteger));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () ->
Functions.get(testable::testInteger));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ final Integer i = Functions.apply(Testable::testInteger, testable);
+ assertNotNull(i);
+ assertEquals(0, i.intValue());
}
@Test
- public void testAsSupplier() {
+ void testRunnable() {
FailureOnOddInvocations.invocations = 0;
- final FailableSupplier<FailureOnOddInvocations, Throwable>
failableSupplier = FailureOnOddInvocations::new;
- final Supplier<FailureOnOddInvocations> supplier =
Functions.asSupplier(failableSupplier);
- final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
+ final UndeclaredThrowableException e =
assertThrows(UndeclaredThrowableException.class, () ->
Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
- assertNotNull(supplier.get());
+
+ // Even invocations, should not throw an exception
+ Functions.run(FailureOnOddInvocations::new);
}
@Test