common-utils/src/main/java/org/warp/commonutils/functional/Unchecked.java

31 lines
727 B
Java
Raw Normal View History

2020-06-12 18:36:36 +02:00
package org.warp.commonutils.functional;
import java.util.function.Function;
public class Unchecked<T> implements Function<T, UncheckedResult> {
private final UncheckedConsumer<T> uncheckedConsumer;
public Unchecked(UncheckedConsumer<T> uncheckedConsumer) {
this.uncheckedConsumer = uncheckedConsumer;
}
public static <T> Unchecked<T> wrap(UncheckedConsumer<T> uncheckedConsumer) {
return new Unchecked<>(uncheckedConsumer);
}
@Override
public UncheckedResult apply(T t) {
try {
uncheckedConsumer.consume(t);
return new UncheckedResult();
} catch (Exception e) {
return new UncheckedResult(e);
}
}
public interface UncheckedConsumer<T> {
public void consume(T value) throws Exception;
}
}