Sort members.
This commit is contained in:
parent
d88f70e8ff
commit
540ec09e25
|
@ -135,6 +135,24 @@ public class Functions {
|
||||||
boolean test(I1 object1, I2 object2) throws T;
|
boolean test(I1 object1, I2 object2) throws T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* A functional interface like {@link java.util.concurrent.Callable} that declares a Throwable.
|
||||||
*
|
*
|
||||||
|
@ -172,6 +190,24 @@ public class Functions {
|
||||||
void accept(O object) throws T;
|
void accept(O object) throws T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A functional interface like {@link DoubleSupplier} that declares a Throwable.
|
||||||
|
*
|
||||||
|
* @param <T> Thrown exception.
|
||||||
|
* @since 3.11
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface FailableDoubleSupplier<T extends Throwable> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supplies a double.
|
||||||
|
*
|
||||||
|
* @return a result
|
||||||
|
* @throws T if the supplier fails
|
||||||
|
*/
|
||||||
|
double getAsDouble() throws T;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A functional interface like {@link Function} that declares a Throwable.
|
* A functional interface like {@link Function} that declares a Throwable.
|
||||||
*
|
*
|
||||||
|
@ -192,6 +228,42 @@ public class Functions {
|
||||||
R apply(I input) throws T;
|
R apply(I input) throws T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A functional interface like {@link IntSupplier} that declares a Throwable.
|
||||||
|
*
|
||||||
|
* @param <T> Thrown exception.
|
||||||
|
* @since 3.11
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface FailableIntSupplier<T extends Throwable> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supplies an int.
|
||||||
|
*
|
||||||
|
* @return a result
|
||||||
|
* @throws T if the supplier fails
|
||||||
|
*/
|
||||||
|
int getAsInt() throws T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A functional interface like {@link LongSupplier} that declares a Throwable.
|
||||||
|
*
|
||||||
|
* @param <T> Thrown exception.
|
||||||
|
* @since 3.11
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface FailableLongSupplier<T extends Throwable> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supplies a long.
|
||||||
|
*
|
||||||
|
* @return a result
|
||||||
|
* @throws T if the supplier fails
|
||||||
|
*/
|
||||||
|
long getAsLong() throws T;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A functional interface like {@link Predicate} that declares a Throwable.
|
* A functional interface like {@link Predicate} that declares a Throwable.
|
||||||
*
|
*
|
||||||
|
@ -245,78 +317,6 @@ public class Functions {
|
||||||
R get() throws T;
|
R get() throws T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 DoubleSupplier} that declares a Throwable.
|
|
||||||
*
|
|
||||||
* @param <T> Thrown exception.
|
|
||||||
* @since 3.11
|
|
||||||
*/
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface FailableDoubleSupplier<T extends Throwable> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supplies a double.
|
|
||||||
*
|
|
||||||
* @return a result
|
|
||||||
* @throws T if the supplier fails
|
|
||||||
*/
|
|
||||||
double getAsDouble() throws T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A functional interface like {@link IntSupplier} that declares a Throwable.
|
|
||||||
*
|
|
||||||
* @param <T> Thrown exception.
|
|
||||||
* @since 3.11
|
|
||||||
*/
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface FailableIntSupplier<T extends Throwable> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supplies an int.
|
|
||||||
*
|
|
||||||
* @return a result
|
|
||||||
* @throws T if the supplier fails
|
|
||||||
*/
|
|
||||||
int getAsInt() throws T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A functional interface like {@link LongSupplier} that declares a Throwable.
|
|
||||||
*
|
|
||||||
* @param <T> Thrown exception.
|
|
||||||
* @since 3.11
|
|
||||||
*/
|
|
||||||
@FunctionalInterface
|
|
||||||
public interface FailableLongSupplier<T extends Throwable> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Supplies a long.
|
|
||||||
*
|
|
||||||
* @return a result
|
|
||||||
* @throws T if the supplier fails
|
|
||||||
*/
|
|
||||||
long getAsLong() throws T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
|
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
|
||||||
*
|
*
|
||||||
|
|
|
@ -46,6 +46,53 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class FunctionsTest {
|
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 {
|
public static class SomeException extends Exception {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4965704778119283411L;
|
private static final long serialVersionUID = -4965704778119283411L;
|
||||||
|
@ -88,40 +135,21 @@ 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 {
|
public boolean testBooleanPrimitive() throws Throwable {
|
||||||
return testBooleanPrimitive(t);
|
return testBooleanPrimitive(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable {
|
||||||
|
if (throwable != null) {
|
||||||
|
throw throwable;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public double testDoublePrimitive() throws Throwable {
|
public double testDoublePrimitive() throws Throwable {
|
||||||
return testDoublePrimitive(t);
|
return testDoublePrimitive(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer testInteger(final Throwable throwable) throws Throwable {
|
|
||||||
if (throwable != null) {
|
|
||||||
throw throwable;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int testIntPrimitive(final Throwable throwable) throws Throwable {
|
|
||||||
if (throwable != null) {
|
|
||||||
throw throwable;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double testDoublePrimitive(final Throwable throwable) throws Throwable {
|
public double testDoublePrimitive(final Throwable throwable) throws Throwable {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throw throwable;
|
throw throwable;
|
||||||
|
@ -129,124 +157,60 @@ class FunctionsTest {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long testLongPrimitive(final Throwable throwable) throws Throwable {
|
public Integer testInteger() throws Throwable {
|
||||||
|
return testInteger(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer testInteger(final Throwable throwable) throws Throwable {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throw throwable;
|
throw throwable;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable {
|
public int testIntPrimitive() throws Throwable {
|
||||||
|
return testIntPrimitive(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int testIntPrimitive(final Throwable throwable) throws Throwable {
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
throw throwable;
|
throw throwable;
|
||||||
}
|
}
|
||||||
return false;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static class FailureOnOddInvocations {
|
public long testLongPrimitive() throws Throwable {
|
||||||
private static int invocations;
|
return testLongPrimitive(t);
|
||||||
|
}
|
||||||
|
|
||||||
private static void throwOnOdd() throws SomeException {
|
public long testLongPrimitive(final Throwable throwable) throws Throwable {
|
||||||
final int i = ++invocations;
|
if (throwable != null) {
|
||||||
if (i % 2 == 1) {
|
throw throwable;
|
||||||
throw new SomeException("Odd Invocation: " + i);
|
|
||||||
}
|
}
|
||||||
}
|
return 0;
|
||||||
|
|
||||||
static boolean failingBool() throws SomeException {
|
|
||||||
throwOnOdd();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
FailureOnOddInvocations() throws SomeException {
|
|
||||||
throwOnOdd();
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean getAsBoolean() throws SomeException {
|
|
||||||
throwOnOdd();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class CloseableObject {
|
|
||||||
private boolean closed;
|
|
||||||
|
|
||||||
public void run(final Throwable pTh) throws Throwable {
|
|
||||||
if (pTh != null) {
|
|
||||||
throw pTh;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
closed = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
closed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isClosed() {
|
|
||||||
return closed;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRunnable() {
|
void testAcceptBiConsumer() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
final Testable testable = new Testable(null);
|
||||||
final Throwable cause = e.getCause();
|
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable, ise));
|
||||||
assertNotNull(cause);
|
assertSame(ise, e);
|
||||||
assertTrue(cause instanceof SomeException);
|
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
|
||||||
|
|
||||||
// Even invocations, should not throw an exception
|
final Error error = new OutOfMemoryError();
|
||||||
Functions.run(FailureOnOddInvocations::new);
|
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(Testable::test, testable, error));
|
||||||
}
|
assertSame(error, e);
|
||||||
|
|
||||||
@Test
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
void testAsRunnable() {
|
testable.setThrowable(ioe);
|
||||||
FailureOnOddInvocations.invocations = 0;
|
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(Testable::test, testable, ioe));
|
||||||
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
|
final Throwable t = e.getCause();
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
|
assertNotNull(t);
|
||||||
final Throwable cause = e.getCause();
|
assertSame(ioe, t);
|
||||||
assertNotNull(cause);
|
|
||||||
assertTrue(cause instanceof SomeException);
|
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
|
||||||
|
|
||||||
// Even invocations, should not throw an exception
|
testable.setThrowable(null);
|
||||||
runnable.run();
|
Functions.accept(Testable::test, testable, (Throwable) null);
|
||||||
}
|
|
||||||
|
|
||||||
@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
|
|
||||||
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
|
@Test
|
||||||
|
@ -273,74 +237,25 @@ class FunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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));
|
|
||||||
assertSame(ise, e);
|
|
||||||
|
|
||||||
final Error error = new OutOfMemoryError();
|
|
||||||
testable.setThrowable(error);
|
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable));
|
|
||||||
assertSame(error, e);
|
|
||||||
|
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
|
||||||
testable.setThrowable(ioe);
|
|
||||||
e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable));
|
|
||||||
final Throwable t = e.getCause();
|
|
||||||
assertNotNull(t);
|
|
||||||
assertSame(ioe, t);
|
|
||||||
|
|
||||||
testable.setThrowable(null);
|
|
||||||
Functions.accept(Testable::test, testable);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testAcceptBiConsumer() {
|
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(null);
|
final Testable testable = new Testable(null);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable, ise));
|
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable, ise));
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
|
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(Testable::test, testable, error));
|
e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error));
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
|
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
testable.setThrowable(ioe);
|
e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe));
|
||||||
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(Testable::test, testable, ioe));
|
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
|
|
||||||
testable.setThrowable(null);
|
final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null);
|
||||||
Functions.accept(Testable::test, testable, (Throwable) null);
|
assertNotNull(i);
|
||||||
}
|
assertEquals(0, i.intValue());
|
||||||
|
|
||||||
@Test
|
|
||||||
void testAsBiConsumer() {
|
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
|
||||||
final Testable testable = new Testable(null);
|
|
||||||
final FailableBiConsumer<Testable, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
|
||||||
t.setThrowable(th); t.test();
|
|
||||||
};
|
|
||||||
final BiConsumer<Testable, Throwable> consumer = Functions.asBiConsumer(failableBiConsumer);
|
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable, ise));
|
|
||||||
assertSame(ise, e);
|
|
||||||
|
|
||||||
final Error error = new OutOfMemoryError();
|
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable, error));
|
|
||||||
assertSame(error, e);
|
|
||||||
|
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
|
||||||
testable.setThrowable(ioe);
|
|
||||||
e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable, ioe));
|
|
||||||
final Throwable t = e.getCause();
|
|
||||||
assertNotNull(t);
|
|
||||||
assertSame(ioe, t);
|
|
||||||
|
|
||||||
consumer.accept(testable, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -369,52 +284,28 @@ class FunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAsFunction() {
|
void testAsBiConsumer() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(null);
|
||||||
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
|
final FailableBiConsumer<Testable, Throwable, Throwable> failableBiConsumer = (t, th) -> {
|
||||||
testable.setThrowable(th);
|
t.setThrowable(th); t.test();
|
||||||
return Integer.valueOf(testable.testInteger());
|
|
||||||
};
|
};
|
||||||
final Function<Throwable, Integer> function = Functions.asFunction(failableFunction);
|
final BiConsumer<Testable, Throwable> consumer = Functions.asBiConsumer(failableBiConsumer);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise));
|
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable, ise));
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
|
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
testable.setThrowable(error);
|
e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable, error));
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> function.apply(error));
|
|
||||||
assertSame(error, e);
|
assertSame(error, e);
|
||||||
|
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
testable.setThrowable(ioe);
|
testable.setThrowable(ioe);
|
||||||
e = assertThrows(UncheckedIOException.class, () -> function.apply(ioe));
|
e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable, ioe));
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
|
|
||||||
assertEquals(0, function.apply(null).intValue());
|
consumer.accept(testable, null);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testApplyBiFunction() {
|
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
|
||||||
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();
|
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error));
|
|
||||||
assertSame(error, e);
|
|
||||||
|
|
||||||
final IOException ioe = new IOException("Unknown I/O error");
|
|
||||||
e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe));
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -445,7 +336,134 @@ class FunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFromSupplier() {
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
void testAsConsumer() {
|
||||||
|
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));
|
||||||
|
assertSame(ise, e);
|
||||||
|
|
||||||
|
final Error error = new OutOfMemoryError();
|
||||||
|
testable.setThrowable(error);
|
||||||
|
e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable));
|
||||||
|
assertSame(error, e);
|
||||||
|
|
||||||
|
final IOException ioe = new IOException("Unknown I/O error");
|
||||||
|
testable.setThrowable(ioe);
|
||||||
|
e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable));
|
||||||
|
final Throwable t = e.getCause();
|
||||||
|
assertNotNull(t);
|
||||||
|
assertSame(ioe, t);
|
||||||
|
|
||||||
|
testable.setThrowable(null);
|
||||||
|
Functions.accept(Testable::test, testable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
|
||||||
|
assertEquals(0, function.apply(null).intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
||||||
|
public void testAsPredicate() {
|
||||||
|
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 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
|
||||||
|
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());
|
||||||
|
|
||||||
|
// Even invocations, should not throw an exception
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCallable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
|
@ -456,31 +474,6 @@ class FunctionsTest {
|
||||||
assertNotNull(instance);
|
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);
|
|
||||||
|
|
||||||
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
|
@Test
|
||||||
public void testGetAsBooleanSupplier() {
|
public void testGetAsBooleanSupplier() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
|
@ -576,46 +569,53 @@ class FunctionsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
public void testGetFromSupplier() {
|
||||||
public void testAsPredicate() {
|
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool();
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
||||||
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
|
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
|
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
final boolean instance = predicate.test(null);
|
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
|
||||||
assertNotNull(instance);
|
assertNotNull(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
|
public void testGetSupplier() {
|
||||||
public void testAsBiPredicate() {
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
FailureOnOddInvocations.invocations = 0;
|
final Testable testable = new Testable(ise);
|
||||||
final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
|
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.get(testable::testInteger));
|
||||||
final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
|
assertSame(ise, e);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
|
|
||||||
final Throwable cause = e.getCause();
|
final Error error = new OutOfMemoryError();
|
||||||
assertNotNull(cause);
|
testable.setThrowable(error);
|
||||||
assertTrue(cause instanceof SomeException);
|
e = assertThrows(OutOfMemoryError.class, () -> Functions.get(testable::testInteger));
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertSame(error, e);
|
||||||
final boolean instance = predicate.test(null, null);
|
|
||||||
assertNotNull(instance);
|
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
|
@Test
|
||||||
public void testAsSupplier() {
|
void testRunnable() {
|
||||||
FailureOnOddInvocations.invocations = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new;
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
||||||
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
|
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
|
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
assertNotNull(supplier.get());
|
|
||||||
|
// Even invocations, should not throw an exception
|
||||||
|
Functions.run(FailureOnOddInvocations::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue