package thyyxxk.webserver.utils;


import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;

public class TryUtil {

    @FunctionalInterface
    public interface TryRunnable {
        void apply();
    }

    public TryUtil() {

    }

    private final List<Supplier<Object>> predicates = new LinkedList<>();

    @Contract(" -> new")
    public static @NotNull TryUtil create() {
        return new TryUtil();
    }

    public TryUtil fun(Supplier<Object> predicate) {
        this.predicates.add(predicate);
        return this;
    }

    public Object execute() {
        for (Supplier<Object> predicate : predicates) {
            try {
                return predicate.get();
            } catch (Exception ignore) {

            }
        }
        return null;
    }

    public static void ignoreErr(TryRunnable runnable) {
        try {
            runnable.apply();
        } catch (Exception ignore) {
        }
    }


}
