package eu.mikroskeem.shuriken.common;

import eu.mikroskeem.shuriken.reflect.Reflect;
import eu.mikroskeem.shuriken.reflect.wrappers.TypeWrapper;
import lombok.NonNull;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

import static eu.mikroskeem.shuriken.common.SneakyThrow.throwException;
import static eu.mikroskeem.shuriken.reflect.wrappers.TypeWrapper.of;

/**
 * Ensure conditions
 *
 * @author Mark Vainomaa
 * @version 0.0.1
 */
public class Ensure {
    /**
     * Ensure that condition is true
     *
     * @param condition Condition
     * @param exception Exception what will be thrown, if condition isn't true
     * @param args Exception arguments
     */
    public static void ensureCondition(boolean condition, Class<? extends Exception> exception, TypeWrapper... args){
        if(!condition){
            try {
                throw Reflect.construct(Reflect.wrapClass(exception), args).getClassInstance();
            } catch (Throwable e){
                throwException(e);
            }
        }
    }

    /**
     * Check if reference is not null
     *
     * @param ref Object reference
     * @param errorMessage NullPointerException message
     * @param <T> Reference type
     * @return Passed reference
     */
    @Contract("null, _ -> fail; !null, _ -> !null")
    public static <T> T notNull(T ref, @Nullable String errorMessage) {
        ensureCondition(ref != null, NullPointerException.class, of(""+errorMessage));
        return ref;
    }

    /**
     * Ensure that {@link Optional} value is present
     *
     * @param optional Optional to check
     * @param errorMessage NullPointerException message
     * @param <T> Value type wrapped inside {@link Optional}
     * @return Optional value
     */
    @SuppressWarnings({"ConstantConditions", "OptionalUsedAsFieldOrParameterType"})
    @NotNull
    @Contract("null, _ -> fail")
    public static <T> T ensurePresent(@NonNull Optional<T> optional, @Nullable String errorMessage){
        ensureCondition(optional.isPresent(), NullPointerException.class, of(""+errorMessage));
        return optional.get();
    }
}
@@ -1121,32 +1121,370 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Throws %7B@link IllegalStateException%7D if condition is not true%0A     *%0A     * @param condition Condition to assert%0A     * @param text Message in %7B@link IllegalStateException%7D%0A     */%0A    public static void ensureCondition(boolean condition, String text) %7B%0A        if(!condition) throw new IllegalStateException(text);%0A    %7D%0A%0A
     /**%0A     * C

Add ensureCondition what throws IllegalStateException directly