org.unlaxer.util.function.Unchecked Maven / Gradle / Ivy
package org.unlaxer.util.function;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Unchecked {
static Logger logger = LoggerFactory.getLogger(Unchecked.class);
public static Consumer of(ThrowingConsumer target) throws LambdaException {
return consumer(target);
}
public static Consumer consumer(ThrowingConsumer target) throws LambdaException {
return (parameter -> {
try {
target.accept(parameter);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new LambdaException(e);
}
});
}
public static Supplier of(ThrowingSupplier target) throws LambdaException {
return supplier(target);
}
public static Supplier supplier(ThrowingSupplier target) throws LambdaException {
return (() -> {
try {
return target.get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new LambdaException(e);
}
});
}
public static Supplier> optionalOf(ThrowingSupplier target) throws LambdaException {
return optionalSupplier(target);
}
public static Supplier> optionalSupplier(ThrowingSupplier target) throws LambdaException {
return (() -> {
try {
return Optional.of(target.get());
} catch (Throwable e) {
return Optional.empty();
}
});
}
public static Function of(ThrowingFunction target) throws LambdaException {
return function(target);
}
public static Function function(ThrowingFunction target) throws LambdaException {
return (parameter -> {
try {
return target.apply(parameter);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new LambdaException(e);
}
});
}
public static T ofFuture(Future futule) throws LambdaException {
try {
return futule.get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new LambdaException(e);
}
}
public static Function> optionalOf(ThrowingFunction target){
return optionalFunction(target);
}
public static Function> optionalFunction(ThrowingFunction target){
return (parameter -> {
try {
return Optional.of(target.apply(parameter));
} catch (Throwable e){
return Optional.empty();
}
});
}
public static Function> optionalFunction(ThrowingFunction target , BiConsumer errorConsumer){
return (parameter -> {
try {
return Optional.of(target.apply(parameter));
} catch (Throwable e){
errorConsumer.accept(parameter, e);
return Optional.empty();
}
});
}
public static Predicate of(ThrowingPredicate target) throws LambdaException {
return predicate(target);
}
public static Predicate predicate(ThrowingPredicate target) throws LambdaException {
return (parameter -> {
try {
return target.test(parameter);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new LambdaException(e);
}
});
}
public static Consumer consumerWithSuppress(ThrowingConsumer target) {
return (parameter -> {
try {
target.accept(parameter);
} catch (Throwable e) {
logger.error("raised exception",e);
}
});
}
public static Runnable of(ThrowingRunnable runnable) {
return ()->{
try {
runnable.run();
} catch (Throwable e) {
logger.error("raised exception",e);
}
};
}
public static void run(ThrowingRunnable runnable){
try {
runnable.run();
} catch (Throwable e) {
logger.error("raised exception",e);
}
}
public static void runWithThrows(ThrowingRunnable runnable){
try {
runnable.run();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public static void run(ThrowingRunnable runnable , Consumer throwableConsumer){
try {
runnable.run();
} catch (Throwable e) {
throwableConsumer.accept(e);
}
}
@FunctionalInterface
public static interface ThrowingRunnable {
public void run() throws Exception;
}
@FunctionalInterface
public static interface ThrowingConsumer {
public void accept(T t) throws Exception;
}
@FunctionalInterface
public static interface ThrowingSupplier {
public T get() throws Exception;
}
@FunctionalInterface
public static interface ThrowingFunction {
public R apply(T t) throws Exception;
}
@FunctionalInterface
public static interface ThrowingPredicate {
public boolean test(T t) throws Exception;
}
public static class LambdaException extends RuntimeException {
private static final long serialVersionUID = 798257349281977890L;
private LambdaException(Throwable cause) {
super(cause);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy