[LANG-1568] Add more failable Consumers to match JRE stock Consumers.

This commit is contained in:
Gary Gregory 2020-06-12 15:25:14 -04:00
parent 62393bd601
commit f5e9f55283
2 changed files with 398 additions and 25 deletions

View File

@ -27,10 +27,16 @@
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.IntSupplier;
import java.util.function.LongConsumer;
import java.util.function.LongSupplier;
import java.util.function.ObjDoubleConsumer;
import java.util.function.ObjIntConsumer;
import java.util.function.ObjLongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
@ -180,7 +186,6 @@ public interface FailableCallable<R, T extends Throwable> {
@FunctionalInterface
public interface FailableConsumer<O, T extends Throwable> {
/**
* Accepts the consumer.
*
@ -190,6 +195,24 @@ public interface FailableConsumer<O, T extends Throwable> {
void accept(O object) throws T;
}
/**
* A functional interface like {@link DoubleConsumer} that declares a Throwable.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableDoubleConsumer<T extends Throwable> {
/**
* Accepts the consumer.
*
* @param value the parameter for the consumable to accept
* @throws T if the consumer fails
*/
void accept(double value) throws T;
}
/**
* A functional interface like {@link DoubleSupplier} that declares a Throwable.
*
@ -228,6 +251,24 @@ public interface FailableFunction<I, R, T extends Throwable> {
R apply(I input) throws T;
}
/**
* A functional interface like {@link IntConsumer} that declares a Throwable.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableIntConsumer<T extends Throwable> {
/**
* Accepts the consumer.
*
* @param value the parameter for the consumable to accept
* @throws T if the consumer fails
*/
void accept(int value) throws T;
}
/**
* A functional interface like {@link IntSupplier} that declares a Throwable.
*
@ -246,6 +287,24 @@ public interface FailableIntSupplier<T extends Throwable> {
int getAsInt() throws T;
}
/**
* A functional interface like {@link LongConsumer} that declares a Throwable.
*
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableLongConsumer<T extends Throwable> {
/**
* Accepts the consumer.
*
* @param object the parameter for the consumable to accept
* @throws T if the consumer fails
*/
void accept(long object) throws T;
}
/**
* A functional interface like {@link LongSupplier} that declares a Throwable.
*
@ -264,6 +323,66 @@ public interface FailableLongSupplier<T extends Throwable> {
long getAsLong() throws T;
}
/**
* A functional interface like {@link ObjDoubleConsumer} that declares a Throwable.
*
* @param <O> the type of the object argument to the operation.
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableObjDoubleConsumer<O, T extends Throwable> {
/**
* Accepts the consumer.
*
* @param object the object parameter for the consumable to accept.
* @param value the double parameter for the consumable to accept.
* @throws T if the consumer fails
*/
void accept(O object, double value) throws T;
}
/**
* A functional interface like {@link ObjIntConsumer} that declares a Throwable.
*
* @param <O> the type of the object argument to the operation.
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableObjIntConsumer<O, T extends Throwable> {
/**
* Accepts the consumer.
*
* @param object the object parameter for the consumable to accept.
* @param value the int parameter for the consumable to accept.
* @throws T if the consumer fails
*/
void accept(O object, int value) throws T;
}
/**
* A functional interface like {@link ObjLongConsumer} that declares a Throwable.
*
* @param <O> the type of the object argument to the operation.
* @param <T> Thrown exception.
* @since 3.11
*/
@FunctionalInterface
public interface FailableObjLongConsumer<O, T extends Throwable> {
/**
* Accepts the consumer.
*
* @param object the object parameter for the consumable to accept.
* @param value the long parameter for the consumable to accept.
* @throws T if the consumer fails
*/
void accept(O object, long value) throws T;
}
/**
* A functional interface like {@link Predicate} that declares a Throwable.
*
@ -344,6 +463,42 @@ public static <O, T extends Throwable> void accept(final FailableConsumer<O, T>
run(() -> consumer.accept(object));
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableDoubleConsumer<T> consumer, final double value) {
run(() -> consumer.accept(value));
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableIntConsumer<T> consumer, final int value) {
run(() -> consumer.accept(value));
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
*
* @param consumer the consumer to consume
* @param value the value to consume by {@code consumer}
* @param <T> the type of checked exception the consumer may throw
* @since 3.11
*/
public static <T extends Throwable> void accept(final FailableLongConsumer<T> consumer, final long value) {
run(() -> consumer.accept(value));
}
/**
* Applies a function and rethrows any exception as a {@link RuntimeException}.
*

View File

@ -19,6 +19,7 @@
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.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -114,19 +115,29 @@ public void test() throws Throwable {
}
}
public static class Testable {
private Throwable t;
public static class Testable<T, P> {
private Throwable throwable;
private P acceptedPrimitiveObject;
private T acceptedObject;
Testable(final Throwable pTh) {
t = pTh;
Testable(final Throwable throwable) {
this.throwable = throwable;
}
public T getAcceptedObject() {
return acceptedObject;
}
public P getAcceptedPrimitiveObject() {
return acceptedPrimitiveObject;
}
public void setThrowable(final Throwable throwable) {
t = throwable;
this.throwable = throwable;
}
public void test() throws Throwable {
test(t);
test(throwable);
}
public void test(final Throwable throwable) throws Throwable {
@ -136,7 +147,7 @@ public void test(final Throwable throwable) throws Throwable {
}
public boolean testBooleanPrimitive() throws Throwable {
return testBooleanPrimitive(t);
return testBooleanPrimitive(throwable);
}
public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable {
@ -146,8 +157,13 @@ public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable
return false;
}
public void testDouble(double i) throws Throwable {
test(throwable);
acceptedPrimitiveObject = (P) ((Double) i);
}
public double testDoublePrimitive() throws Throwable {
return testDoublePrimitive(t);
return testDoublePrimitive(throwable);
}
public double testDoublePrimitive(final Throwable throwable) throws Throwable {
@ -157,8 +173,13 @@ public double testDoublePrimitive(final Throwable throwable) throws Throwable {
return 0;
}
public void testInt(int i) throws Throwable {
test(throwable);
acceptedPrimitiveObject = (P) ((Integer) i);
}
public Integer testInteger() throws Throwable {
return testInteger(t);
return testInteger(throwable);
}
public Integer testInteger(final Throwable throwable) throws Throwable {
@ -169,7 +190,7 @@ public Integer testInteger(final Throwable throwable) throws Throwable {
}
public int testIntPrimitive() throws Throwable {
return testIntPrimitive(t);
return testIntPrimitive(throwable);
}
public int testIntPrimitive(final Throwable throwable) throws Throwable {
@ -179,8 +200,13 @@ public int testIntPrimitive(final Throwable throwable) throws Throwable {
return 0;
}
public void testLong(long i) throws Throwable {
test(throwable);
acceptedPrimitiveObject = (P) ((Long) i);
}
public long testLongPrimitive() throws Throwable {
return testLongPrimitive(t);
return testLongPrimitive(throwable);
}
public long testLongPrimitive(final Throwable throwable) throws Throwable {
@ -189,12 +215,30 @@ public long testLongPrimitive(final Throwable throwable) throws Throwable {
}
return 0;
}
public void testObjDouble(T object, double i) throws Throwable {
test(throwable);
acceptedObject = object;
acceptedPrimitiveObject = (P) ((Double) i);
}
public void testObjInt(T object, int i) throws Throwable {
test(throwable);
acceptedObject = object;
acceptedPrimitiveObject = (P) ((Integer) i);
}
public void testObjLong(T object, long i) throws Throwable {
test(throwable);
acceptedObject = object;
acceptedPrimitiveObject = (P) ((Long) i);
}
}
@Test
void testAcceptBiConsumer() {
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));
assertSame(ise, e);
@ -216,7 +260,7 @@ void testAcceptBiConsumer() {
@Test
void testAcceptConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable));
assertSame(ise, e);
@ -236,10 +280,184 @@ void testAcceptConsumer() {
Functions.accept(Testable::test, testable);
}
@Test
void testAcceptDoubleConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, Double> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testDouble, 1d));
assertSame(ise, e);
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testDouble, 1d));
assertSame(error, e);
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testDouble, 1d));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testDouble, 1d);
assertEquals(1, testable.getAcceptedPrimitiveObject());
}
@Test
void testAcceptIntConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, Integer> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testInt, 1));
assertSame(ise, e);
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testInt, 1));
assertSame(error, e);
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testInt, 1));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testInt, 1);
assertEquals(1, testable.getAcceptedPrimitiveObject());
}
@Test
void testAcceptLongConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<?, Long> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testLong, 1L));
assertSame(ise, e);
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testLong, 1L));
assertSame(error, e);
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testLong, 1L));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testLong, 1L);
assertEquals(1, testable.getAcceptedPrimitiveObject());
}
@Test
void testAcceptObjDoubleConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<String, Double> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjDouble, "X", 1d));
assertSame(ise, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjDouble, "X", 1d));
assertSame(error, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testObjDouble, "X", 1d));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testObjDouble, "X", 1d);
assertEquals("X", testable.getAcceptedObject());
assertEquals(1d, testable.getAcceptedPrimitiveObject());
}
@Test
void testAcceptObjIntConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<String, Integer> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjInt, "X", 1));
assertSame(ise, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjInt, "X", 1));
assertSame(error, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testObjInt, "X", 1));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testObjInt, "X", 1);
assertEquals("X", testable.getAcceptedObject());
assertEquals(1, testable.getAcceptedPrimitiveObject());
}
@Test
void testAcceptObjLongConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable<String, Long> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjLong, "X", 1L));
assertSame(ise, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjLong, "X", 1L));
assertSame(error, e);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
e = assertThrows(UncheckedIOException.class, () -> Functions.accept(testable::testObjLong, "X", 1L));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
assertNull(testable.getAcceptedObject());
assertNull(testable.getAcceptedPrimitiveObject());
testable.setThrowable(null);
Functions.accept(testable::testObjLong, "X", 1L);
assertEquals("X", testable.getAcceptedObject());
assertEquals(1L, testable.getAcceptedPrimitiveObject());
}
@Test
public void testApplyBiFunction() {
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::testInteger, testable, ise));
assertSame(ise, e);
@ -261,7 +479,7 @@ public void testApplyBiFunction() {
@Test
public void testApplyFunction() {
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::testInteger, testable));
assertSame(ise, e);
@ -286,7 +504,7 @@ public void testApplyFunction() {
@Test
void testAsBiConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
final Testable<?, ?> testable = new Testable(null);
final FailableBiConsumer<Testable, Throwable, Throwable> failableBiConsumer = (t, th) -> {
t.setThrowable(th); t.test();
};
@ -311,7 +529,7 @@ void testAsBiConsumer() {
@Test
public void testAsBiFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
t.setThrowable(th);
return Integer.valueOf(t.testInteger());
@ -372,7 +590,7 @@ void testAsCallable() {
@Test
void testAsConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
final Consumer<Testable> consumer = Functions.asConsumer(Testable::test);
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable));
assertSame(ise, e);
@ -396,7 +614,7 @@ void testAsConsumer() {
@Test
public void testAsFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
testable.setThrowable(th);
return Integer.valueOf(testable.testInteger());
@ -477,7 +695,7 @@ void testCallable() {
@Test
public void testGetAsBooleanSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
assertSame(ise, e);
@ -500,7 +718,7 @@ public void testGetAsBooleanSupplier() {
@Test
public void testGetAsDoubleSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
assertSame(ise, e);
@ -523,7 +741,7 @@ public void testGetAsDoubleSupplier() {
@Test
public void testGetAsIntSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testIntPrimitive));
assertSame(ise, e);
@ -547,7 +765,7 @@ public void testGetAsIntSupplier() {
@Test
public void testGetAsLongSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsLong(testable::testLongPrimitive));
assertSame(ise, e);
@ -583,7 +801,7 @@ public void testGetFromSupplier() {
@Test
public void testGetSupplier() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
final Testable<?, ?> testable = new Testable<>(ise);
Throwable e = assertThrows(IllegalStateException.class, () -> Functions.get(testable::testInteger));
assertSame(ise, e);