FEAT checked FunctionalInterfaces
de.nclazz/java-commons/pipeline/head This commit looks good Details

pull/1/head
Niclas Thobaben 2020-12-01 12:28:37 +01:00
parent 09d9b085cf
commit 0941bbf100
2 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,117 @@
package de.nclazz.commons.func;
import java.rmi.server.ExportException;
import java.util.function.*;
public abstract class Checked {
private Checked() { /* no-op */ }
@FunctionalInterface
public interface CheckedConsumer<T> {
void accept(T t) throws Exception;
}
@FunctionalInterface
public interface CheckedBiConsumer<T, U> {
void accept(T t, U u) throws Exception;
}
@FunctionalInterface
public interface CheckedFunction<T, R> {
R apply(T t) throws Exception;
}
@FunctionalInterface
public interface CheckedBiFunction<T, U, R> {
R apply(T t, U u) throws Exception;
}
@FunctionalInterface
public interface CheckedPredicate<T> {
boolean test(T t) throws Exception;
}
@FunctionalInterface
public interface CheckedBiPredicate<T, U> {
boolean test(T t, U u) throws Exception;
}
@FunctionalInterface
public interface CheckedRunnable {
void run() throws Exception;
}
public static <T> Consumer<T> checked(CheckedConsumer<T> consumer) {
return t -> {
try {
consumer.accept(t);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T, U> BiConsumer<T, U> checked(CheckedBiConsumer<T, U> consumer) {
return (t, u) -> {
try {
consumer.accept(t, u);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T, R> Function<T, R> checked(CheckedFunction<T, R> function) {
return t -> {
try {
return function.apply(t);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T, U, R> BiFunction<T, U, R> checked(CheckedBiFunction<T, U, R> function) {
return (t, u) -> {
try {
return function.apply(t, u);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T> Predicate<T> checked(Predicate<T> predicate) {
return t -> {
try {
return predicate.test(t);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T, U> BiPredicate<T, U> checked(CheckedBiPredicate<T, U> predicate) {
return (t, u) -> {
try {
return predicate.test(t, u);
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static Runnable checked(Runnable runnable) {
return () -> {
try {
runnable.run();
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
}

View File

@ -0,0 +1,33 @@
package de.nclazz.commons.func;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static de.nclazz.commons.func.Checked.checked;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class CheckedTests {
void throwExceptionConsumer(String _) throws ClassNotFoundException {
throw new ClassNotFoundException();
}
void throwExceptionBiConsumer(String _, String __) throws ClassNotFoundException {
throw new ClassNotFoundException();
}
String throwExceptionFunction(String _) throws ClassNotFoundException {
throw new ClassNotFoundException();
}
String throwExceptionBiFunction(String _, String __) throws ClassNotFoundException {
throw new ClassNotFoundException();
}
@Test
void testCheckedThrowsRuntimeException() {
assertThrows(RuntimeException.class, () -> checked(this::throwExceptionConsumer).accept(""));
assertThrows(RuntimeException.class, () -> checked(this::throwExceptionBiConsumer).accept("", ""));
assertThrows(RuntimeException.class, () -> checked(this::throwExceptionFunction).apply(""));
assertThrows(RuntimeException.class, () -> checked(this::throwExceptionBiFunction).apply("", ""));
}
}