java-commons/src/main/java/de/nclazz/commons/func/Checked.java

133 lines
3.2 KiB
Java

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;
}
@FunctionalInterface
public interface CheckedSupplier<T> {
T get() 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(CheckedRunnable runnable) {
return () -> {
try {
runnable.run();
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
public static <T> Supplier<T> checked(CheckedSupplier<T> supplier) {
return () -> {
try {
return supplier.get();
}catch(Exception e) {
throw new RuntimeException(e);
}
};
}
}