package cz.senslog.connector.tools.util.function;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

@FunctionalInterface
public interface ThrowingConsumer<T, E extends Exception> {

    void accept(T t) throws E;

    static <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer, BiConsumer<T, Exception> exceptionHandler) {
        return i -> {
            try {
                throwingConsumer.accept(i);
            } catch (Exception ex) {
                exceptionHandler.accept(i, ex);
            }
        };
    }

    static <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer) {
        return i -> {
            try {
                throwingConsumer.accept(i);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }
}