[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 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 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 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>
|
||||||
|
|
||||||
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">
|
<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.BiConsumer;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.BiPredicate;
|
import java.util.function.BiPredicate;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.DoubleSupplier;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.IntSupplier;
|
||||||
|
import java.util.function.LongSupplier;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -235,12 +239,84 @@ public interface FailableSupplier<R, T extends Throwable> {
|
||||||
/**
|
/**
|
||||||
* Supplies an object
|
* Supplies an object
|
||||||
*
|
*
|
||||||
* @return the suppliers result
|
* @return a result
|
||||||
* @throws T if the supplier fails
|
* @throws T if the supplier fails
|
||||||
*/
|
*/
|
||||||
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}.
|
||||||
*
|
*
|
||||||
|
@ -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 supplier The supplier to invoke.
|
||||||
* @param <O> The suppliers output type.
|
* @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>
|
* <p>
|
||||||
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
|
* 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;
|
package org.apache.commons.lang3;
|
||||||
|
|
||||||
import org.apache.commons.lang3.Functions.FailableBiConsumer;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import org.apache.commons.lang3.Functions.FailableBiFunction;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import org.apache.commons.lang3.Functions.FailableCallable;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import org.apache.commons.lang3.Functions.FailableConsumer;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import org.apache.commons.lang3.Functions.FailableFunction;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import org.apache.commons.lang3.Functions.FailableSupplier;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
@ -37,24 +35,29 @@
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import org.apache.commons.lang3.Functions.FailableBiConsumer;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import org.apache.commons.lang3.Functions.FailableBiFunction;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import org.apache.commons.lang3.Functions.FailableCallable;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import org.apache.commons.lang3.Functions.FailableConsumer;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
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 {
|
class FunctionsTest {
|
||||||
|
|
||||||
public static class SomeException extends Exception {
|
public static class SomeException extends Exception {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4965704778119283411L;
|
private static final long serialVersionUID = -4965704778119283411L;
|
||||||
|
|
||||||
private Throwable t;
|
private Throwable t;
|
||||||
|
|
||||||
SomeException(final String pMsg) {
|
SomeException(final String message) {
|
||||||
super(pMsg);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThrowable(final Throwable pThrowable) {
|
public void setThrowable(final Throwable throwable) {
|
||||||
t = pThrowable;
|
t = throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() throws Throwable {
|
public void test() throws Throwable {
|
||||||
|
@ -63,6 +66,7 @@ public void test() throws Throwable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Testable {
|
public static class Testable {
|
||||||
private Throwable t;
|
private Throwable t;
|
||||||
|
|
||||||
|
@ -70,48 +74,99 @@ public static class Testable {
|
||||||
t = pTh;
|
t = pTh;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setThrowable(final Throwable pThrowable) {
|
public void setThrowable(final Throwable throwable) {
|
||||||
t = pThrowable;
|
t = throwable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() throws Throwable {
|
public void test() throws Throwable {
|
||||||
test(t);
|
test(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test(final Throwable pThrowable) throws Throwable {
|
public void test(final Throwable throwable) throws Throwable {
|
||||||
if (pThrowable != null) {
|
if (throwable != null) {
|
||||||
throw pThrowable;
|
throw throwable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer testInt() throws Throwable {
|
public Integer testInteger() throws Throwable {
|
||||||
return testInt(t);
|
return testInteger(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer testInt(final Throwable pThrowable) throws Throwable {
|
public int testIntPrimitive() throws Throwable {
|
||||||
if (pThrowable != null) {
|
return testIntPrimitive(t);
|
||||||
throw pThrowable;
|
}
|
||||||
|
|
||||||
|
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;
|
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 {
|
public static class FailureOnOddInvocations {
|
||||||
private static int invocation;
|
private static int invocations;
|
||||||
|
|
||||||
private static void throwOnOdd() throws SomeException {
|
private static void throwOnOdd() throws SomeException {
|
||||||
final int i = ++invocation;
|
final int i = ++invocations;
|
||||||
if (i % 2 == 1) {
|
if (i % 2 == 1) {
|
||||||
throw new SomeException("Odd Invocation: " + i);
|
throw new SomeException("Odd Invocation: " + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean failingBool() throws SomeException {
|
static boolean failingBool() throws SomeException {
|
||||||
throwOnOdd();
|
throwOnOdd();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
FailureOnOddInvocations() throws SomeException {
|
FailureOnOddInvocations() throws SomeException {
|
||||||
throwOnOdd();
|
throwOnOdd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean getAsBoolean() throws SomeException {
|
||||||
|
throwOnOdd();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CloseableObject {
|
public static class CloseableObject {
|
||||||
|
@ -138,20 +193,20 @@ public boolean isClosed() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRunnable() {
|
void testRunnable() {
|
||||||
FailureOnOddInvocations.invocation = 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();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
|
|
||||||
// Even invocation, should not throw an exception
|
// Even invocations, should not throw an exception
|
||||||
Functions.run(FailureOnOddInvocations::new);
|
Functions.run(FailureOnOddInvocations::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAsRunnable() {
|
void testAsRunnable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
|
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
|
@ -159,13 +214,13 @@ void testAsRunnable() {
|
||||||
assertTrue(cause instanceof SomeException);
|
assertTrue(cause instanceof SomeException);
|
||||||
assertEquals("Odd Invocation: 1", cause.getMessage());
|
assertEquals("Odd Invocation: 1", cause.getMessage());
|
||||||
|
|
||||||
// Even invocation, should not throw an exception
|
// Even invocations, should not throw an exception
|
||||||
runnable.run();
|
runnable.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCallable() {
|
void testCallable() {
|
||||||
FailureOnOddInvocations.invocation = 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();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
|
@ -177,7 +232,7 @@ void testCallable() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAsCallable() {
|
void testAsCallable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
|
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
|
||||||
final Callable<FailureOnOddInvocations> callable = Functions.asCallable(failableCallable);
|
final Callable<FailureOnOddInvocations> callable = Functions.asCallable(failableCallable);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> callable.call());
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> callable.call());
|
||||||
|
@ -292,23 +347,23 @@ void testAsBiConsumer() {
|
||||||
public void testApplyFunction() {
|
public void testApplyFunction() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
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);
|
assertSame(ise, e);
|
||||||
|
|
||||||
final Error error = new OutOfMemoryError();
|
final Error error = new OutOfMemoryError();
|
||||||
testable.setThrowable(error);
|
testable.setThrowable(error);
|
||||||
e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInt, testable));
|
e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable));
|
||||||
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, () -> Functions.apply(Testable::testInt, testable));
|
e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable));
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertSame(ioe, t);
|
assertSame(ioe, t);
|
||||||
|
|
||||||
testable.setThrowable(null);
|
testable.setThrowable(null);
|
||||||
final Integer i = Functions.apply(Testable::testInt, testable);
|
final Integer i = Functions.apply(Testable::testInteger, testable);
|
||||||
assertNotNull(i);
|
assertNotNull(i);
|
||||||
assertEquals(0, i.intValue());
|
assertEquals(0, i.intValue());
|
||||||
}
|
}
|
||||||
|
@ -319,7 +374,7 @@ public void testAsFunction() {
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
|
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
|
||||||
testable.setThrowable(th);
|
testable.setThrowable(th);
|
||||||
return Integer.valueOf(testable.testInt());
|
return Integer.valueOf(testable.testInteger());
|
||||||
};
|
};
|
||||||
final Function<Throwable, Integer> function = Functions.asFunction(failableFunction);
|
final Function<Throwable, Integer> function = Functions.asFunction(failableFunction);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise));
|
Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise));
|
||||||
|
@ -344,20 +399,20 @@ public void testAsFunction() {
|
||||||
public void testApplyBiFunction() {
|
public void testApplyBiFunction() {
|
||||||
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.apply(Testable::testInt, 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.apply(Testable::testInt, 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");
|
||||||
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();
|
final Throwable t = e.getCause();
|
||||||
assertNotNull(t);
|
assertNotNull(t);
|
||||||
assertSame(ioe, 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);
|
assertNotNull(i);
|
||||||
assertEquals(0, i.intValue());
|
assertEquals(0, i.intValue());
|
||||||
}
|
}
|
||||||
|
@ -368,7 +423,7 @@ public void testAsBiFunction() {
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
|
final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
|
||||||
t.setThrowable(th);
|
t.setThrowable(th);
|
||||||
return Integer.valueOf(t.testInt());
|
return Integer.valueOf(t.testInteger());
|
||||||
};
|
};
|
||||||
final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction);
|
final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction);
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise));
|
Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise));
|
||||||
|
@ -391,7 +446,7 @@ public void testAsBiFunction() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFromSupplier() {
|
public void testGetFromSupplier() {
|
||||||
FailureOnOddInvocations.invocation = 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();
|
||||||
assertNotNull(cause);
|
assertNotNull(cause);
|
||||||
|
@ -401,10 +456,129 @@ public void testGetFromSupplier() {
|
||||||
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
|
||||||
|
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
|
@Test
|
||||||
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
||||||
public void testAsPredicate() {
|
public void testAsPredicate() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool();
|
final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool();
|
||||||
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
|
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
|
||||||
|
@ -419,7 +593,7 @@ public void testAsPredicate() {
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
|
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
|
||||||
public void testAsBiPredicate() {
|
public void testAsBiPredicate() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
|
final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
|
||||||
final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
|
final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
|
||||||
|
@ -433,7 +607,7 @@ public void testAsBiPredicate() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAsSupplier() {
|
public void testAsSupplier() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocations = 0;
|
||||||
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new;
|
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new;
|
||||||
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
|
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
|
||||||
|
@ -441,8 +615,7 @@ public void testAsSupplier() {
|
||||||
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 FailureOnOddInvocations instance = supplier.get();
|
assertNotNull(supplier.get());
|
||||||
assertNotNull(instance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue