All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.utils.validate.Check Maven / Gradle / Ivy

There is a newer version: lib-module-20989dc8ad
Show newest version
package net.minestom.server.utils.validate;

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

import java.text.MessageFormat;
import java.util.Objects;

/**
 * Convenient class to check for common exceptions.
 */
public final class Check {

    private Check() {
    }

    @Contract("null, _ -> fail")
    public static void notNull(@Nullable Object object, @NotNull String reason) {
        if (Objects.isNull(object)) {
            throw new NullPointerException(reason);
        }
    }

    @Contract("null, _, _ -> fail")
    public static void notNull(@Nullable Object object, @NotNull String reason, Object... arguments) {
        if (Objects.isNull(object)) {
            throw new NullPointerException(MessageFormat.format(reason, arguments));
        }
    }

    @Contract("true, _ -> fail")
    public static void argCondition(boolean condition, @NotNull String reason) {
        if (condition) {
            throw new IllegalArgumentException(reason);
        }
    }

    @Contract("true, _, _ -> fail")
    public static void argCondition(boolean condition, @NotNull String reason, Object... arguments) {
        if (condition) {
            throw new IllegalArgumentException(MessageFormat.format(reason, arguments));
        }
    }

    @Contract("_ -> fail")
    public static void fail(@NotNull String reason) {
        throw new IllegalArgumentException(reason);
    }

    @Contract("true, _ -> fail")
    public static void stateCondition(boolean condition, @NotNull String reason) {
        if (condition) {
            throw new IllegalStateException(reason);
        }
    }

    @Contract("true, _, _ -> fail")
    public static void stateCondition(boolean condition, @NotNull String reason, Object... arguments) {
        if (condition) {
            throw new IllegalStateException(MessageFormat.format(reason, arguments));
        }
    }

    @Contract("false, _ -> fail")
    public static void isTrue(boolean condition, @NotNull String reason) {
        if (!condition) {
            throw new IllegalStateException(reason);
        }
    }

    @Contract("false, _, _ -> fail")
    public static void isTrue(boolean condition, @NotNull String reason, Object... arguments) {
        if (!condition) {
            throw new IllegalStateException(MessageFormat.format(reason, arguments));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy