[LANG-1568] FailableBooleanSupplier, FailableIntSupplier,
FailableLongSupplier, FailableDoubleSupplier.
This commit is contained in:
parent
53aa7a2e9a
commit
d88f70e8ff
|
@ -60,6 +60,7 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<action type="add" dev="ggregory" due-to="XenoAmess, Gary Gregory">Add ArrayUtils.isSameLength() to compare more array types #430.</action>
|
||||
<action issue="LANG-1545" type="update" dev="ggregory" due-to="XenoAmess, Gary Gregory">CharSequenceUtils.regionMatches is wrong dealing with Georgian.</action>
|
||||
<action type="add" dev="jochen">Added the Locks class as a convenient possibility to deal with locked objects.</action>
|
||||
<action issue="LANG-1568" type="add" dev="ggregory">Add to Functions: FailableBooleanSupplier, FailableIntSupplier, FailableLongSupplier, FailableDoubleSupplier.</action>
|
||||
</release>
|
||||
|
||||
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">
|
||||
|
|
|
@ -25,8 +25,12 @@
|
|||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.DoubleSupplier;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.LongSupplier;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -235,12 +239,84 @@ public interface FailableSupplier<R, T extends Throwable> {
|
|||
/**
|
||||
* Supplies an object
|
||||
*
|
||||
* @return the suppliers result
|
||||
* @return a result
|
||||
* @throws T if the supplier fails
|
||||
*/
|
||||
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}.
|
||||
*
|
||||
|
@ -424,7 +500,7 @@ public static <O, T extends Throwable> O call(final FailableCallable<O, T> calla
|
|||
}
|
||||
|
||||
/**
|
||||
* Invokes the supplier, and returns the result.
|
||||
* Invokes a supplier, and returns the result.
|
||||
*
|
||||
* @param supplier The supplier to invoke.
|
||||
* @param <O> The suppliers output type.
|
||||
|
@ -440,6 +516,70 @@ public static <O, T extends Throwable> O get(final FailableSupplier<O, T> suppli
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a boolean supplier, and returns the result.
|
||||
*
|
||||
* @param supplier The boolean supplier to invoke.
|
||||
* @param <T> The type of checked exception, which the supplier can throw.
|
||||
* @return The boolean, which has been created by the supplier
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <T extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<T> supplier) {
|
||||
try {
|
||||
return supplier.getAsBoolean();
|
||||
} catch (final Throwable t) {
|
||||
throw rethrow(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a double supplier, and returns the result.
|
||||
*
|
||||
* @param supplier The double supplier to invoke.
|
||||
* @param <T> The type of checked exception, which the supplier can throw.
|
||||
* @return The boolean, which has been created by the supplier
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <T extends Throwable> double getAsDouble(final FailableDoubleSupplier<T> supplier) {
|
||||
try {
|
||||
return supplier.getAsDouble();
|
||||
} catch (final Throwable t) {
|
||||
throw rethrow(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes an int supplier, and returns the result.
|
||||
*
|
||||
* @param supplier The int supplier to invoke.
|
||||
* @param <T> The type of checked exception, which the supplier can throw.
|
||||
* @return The boolean, which has been created by the supplier
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <T extends Throwable> int getAsInt(final FailableIntSupplier<T> supplier) {
|
||||
try {
|
||||
return supplier.getAsInt();
|
||||
} catch (final Throwable t) {
|
||||
throw rethrow(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a long supplier, and returns the result.
|
||||
*
|
||||
* @param supplier The long supplier to invoke.
|
||||
* @param <T> The type of checked exception, which the supplier can throw.
|
||||
* @return The boolean, which has been created by the supplier
|
||||
* @since 3.11
|
||||
*/
|
||||
public static <T extends Throwable> long getAsLong(final FailableLongSupplier<T> supplier) {
|
||||
try {
|
||||
return supplier.getAsLong();
|
||||
} catch (final Throwable t) {
|
||||
throw rethrow(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
|
||||
|
|
|
@ -16,14 +16,12 @@
|
|||
*/
|
||||
package org.apache.commons.lang3;
|
||||
|
||||
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 static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
@ -37,24 +35,29 @@
|
|||
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;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
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;
|
||||
|
||||
class FunctionsTest {
|
||||
|
||||
public static class SomeException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -4965704778119283411L;
|
||||
|
||||
private Throwable t;
|
||||
|
||||
SomeException(final String pMsg) {
|
||||
super(pMsg);
|
||||
SomeException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public void setThrowable(final Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
public void setThrowable(final Throwable throwable) {
|
||||
t = throwable;
|
||||
}
|
||||
|
||||
public void test() throws Throwable {
|
||||
|
@ -63,6 +66,7 @@ public void test() throws Throwable {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Testable {
|
||||
private Throwable t;
|
||||
|
||||
|
@ -70,48 +74,99 @@ public static class Testable {
|
|||
t = pTh;
|
||||
}
|
||||
|
||||
public void setThrowable(final Throwable pThrowable) {
|
||||
t = pThrowable;
|
||||
public void setThrowable(final Throwable throwable) {
|
||||
t = throwable;
|
||||
}
|
||||
|
||||
public void test() throws Throwable {
|
||||
test(t);
|
||||
}
|
||||
|
||||
public void test(final Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
public void test(final Throwable throwable) throws Throwable {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
|
||||
public Integer testInt() throws Throwable {
|
||||
return testInt(t);
|
||||
public Integer testInteger() throws Throwable {
|
||||
return testInteger(t);
|
||||
}
|
||||
|
||||
public Integer testInt(final Throwable pThrowable) throws Throwable {
|
||||
if (pThrowable != null) {
|
||||
throw pThrowable;
|
||||
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 {
|
||||
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 {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long testLongPrimitive(final Throwable throwable) throws Throwable {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable {
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FailureOnOddInvocations {
|
||||
private static int invocation;
|
||||
private static int invocations;
|
||||
|
||||
private static void throwOnOdd() throws SomeException {
|
||||
final int i = ++invocation;
|
||||
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();
|
||||
}
|
||||
|
||||
boolean getAsBoolean() throws SomeException {
|
||||
throwOnOdd();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class CloseableObject {
|
||||
|
@ -138,20 +193,20 @@ public boolean isClosed() {
|
|||
|
||||
@Test
|
||||
void testRunnable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
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 invocation, should not throw an exception
|
||||
// Even invocations, should not throw an exception
|
||||
Functions.run(FailureOnOddInvocations::new);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAsRunnable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
FailureOnOddInvocations.invocations = 0;
|
||||
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
|
||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
|
||||
final Throwable cause = e.getCause();
|
||||
|
@ -159,13 +214,13 @@ void testAsRunnable() {
|
|||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
|
||||
// Even invocation, should not throw an exception
|
||||
// Even invocations, should not throw an exception
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCallable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
FailureOnOddInvocations.invocations = 0;
|
||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
|
@ -177,7 +232,7 @@ void testCallable() {
|
|||
|
||||
@Test
|
||||
void testAsCallable() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
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());
|
||||
|
@ -292,23 +347,23 @@ void testAsBiConsumer() {
|
|||
public void testApplyFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInt, testable));
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable));
|
||||
assertSame(ise, e);
|
||||
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInt, testable));
|
||||
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.apply(Testable::testInt, testable));
|
||||
e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable));
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertSame(ioe, t);
|
||||
|
||||
testable.setThrowable(null);
|
||||
final Integer i = Functions.apply(Testable::testInt, testable);
|
||||
final Integer i = Functions.apply(Testable::testInteger, testable);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
|
@ -319,7 +374,7 @@ public void testAsFunction() {
|
|||
final Testable testable = new Testable(ise);
|
||||
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
|
||||
testable.setThrowable(th);
|
||||
return Integer.valueOf(testable.testInt());
|
||||
return Integer.valueOf(testable.testInteger());
|
||||
};
|
||||
final Function<Throwable, Integer> function = Functions.asFunction(failableFunction);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise));
|
||||
|
@ -344,20 +399,20 @@ public void testAsFunction() {
|
|||
public void testApplyBiFunction() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(null);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInt, testable, ise));
|
||||
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::testInt, testable, error));
|
||||
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::testInt, testable, ioe));
|
||||
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::testInt, testable, (Throwable) null);
|
||||
final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null);
|
||||
assertNotNull(i);
|
||||
assertEquals(0, i.intValue());
|
||||
}
|
||||
|
@ -368,7 +423,7 @@ public void testAsBiFunction() {
|
|||
final Testable testable = new Testable(ise);
|
||||
final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
|
||||
t.setThrowable(th);
|
||||
return Integer.valueOf(t.testInt());
|
||||
return Integer.valueOf(t.testInteger());
|
||||
};
|
||||
final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise));
|
||||
|
@ -391,7 +446,7 @@ public void testAsBiFunction() {
|
|||
|
||||
@Test
|
||||
public void testGetFromSupplier() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
FailureOnOddInvocations.invocations = 0;
|
||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
|
||||
final Throwable cause = e.getCause();
|
||||
assertNotNull(cause);
|
||||
|
@ -401,10 +456,129 @@ public void testGetFromSupplier() {
|
|||
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
|
||||
public void testGetAsBooleanSupplier() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
|
||||
assertSame(ise, e);
|
||||
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
|
||||
assertSame(error, e);
|
||||
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertSame(ioe, t);
|
||||
|
||||
testable.setThrowable(null);
|
||||
assertFalse(Functions.getAsBoolean(testable::testBooleanPrimitive));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAsDoubleSupplier() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
|
||||
assertSame(ise, e);
|
||||
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
|
||||
assertSame(error, e);
|
||||
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertSame(ioe, t);
|
||||
|
||||
testable.setThrowable(null);
|
||||
assertEquals(0, Functions.getAsDouble(testable::testDoublePrimitive));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAsIntSupplier() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testIntPrimitive));
|
||||
assertSame(ise, e);
|
||||
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsInt(testable::testIntPrimitive));
|
||||
assertSame(error, e);
|
||||
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsInt(testable::testIntPrimitive));
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertSame(ioe, t);
|
||||
|
||||
testable.setThrowable(null);
|
||||
final int i = Functions.getAsInt(testable::testInteger);
|
||||
assertEquals(0, i);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAsLongSupplier() {
|
||||
final IllegalStateException ise = new IllegalStateException();
|
||||
final Testable testable = new Testable(ise);
|
||||
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsLong(testable::testLongPrimitive));
|
||||
assertSame(ise, e);
|
||||
|
||||
final Error error = new OutOfMemoryError();
|
||||
testable.setThrowable(error);
|
||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsLong(testable::testLongPrimitive));
|
||||
assertSame(error, e);
|
||||
|
||||
final IOException ioe = new IOException("Unknown I/O error");
|
||||
testable.setThrowable(ioe);
|
||||
e = assertThrows(UncheckedIOException.class, () -> Functions.getAsLong(testable::testLongPrimitive));
|
||||
final Throwable t = e.getCause();
|
||||
assertNotNull(t);
|
||||
assertSame(ioe, t);
|
||||
|
||||
testable.setThrowable(null);
|
||||
final long i = Functions.getAsLong(testable::testLongPrimitive);
|
||||
assertEquals(0, i);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
||||
public void testAsPredicate() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
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));
|
||||
|
@ -419,7 +593,7 @@ public void testAsPredicate() {
|
|||
@Test
|
||||
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
|
||||
public void testAsBiPredicate() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
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));
|
||||
|
@ -433,7 +607,7 @@ public void testAsBiPredicate() {
|
|||
|
||||
@Test
|
||||
public void testAsSupplier() {
|
||||
FailureOnOddInvocations.invocation = 0;
|
||||
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());
|
||||
|
@ -441,8 +615,7 @@ public void testAsSupplier() {
|
|||
assertNotNull(cause);
|
||||
assertTrue(cause instanceof SomeException);
|
||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||
final FailureOnOddInvocations instance = supplier.get();
|
||||
assertNotNull(instance);
|
||||
assertNotNull(supplier.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue