2020-05-23 04:20:01 +02:00
|
|
|
package net.minestom.server.utils.validate;
|
|
|
|
|
2020-10-26 13:57:02 +01:00
|
|
|
import org.jetbrains.annotations.Contract;
|
2020-10-24 10:45:27 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2021-03-26 22:23:41 +01:00
|
|
|
import java.text.MessageFormat;
|
2020-05-23 04:20:01 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2020-10-26 13:57:02 +01:00
|
|
|
/**
|
2020-11-07 19:39:22 +01:00
|
|
|
* Convenient class to check for common exceptions.
|
2020-10-26 13:57:02 +01:00
|
|
|
*/
|
2020-10-24 10:45:27 +02:00
|
|
|
public final class Check {
|
2020-10-26 13:57:02 +01:00
|
|
|
|
2020-10-24 10:45:27 +02:00
|
|
|
private Check() {
|
|
|
|
|
|
|
|
}
|
2020-05-23 04:20:01 +02:00
|
|
|
|
2020-10-26 13:57:02 +01:00
|
|
|
@Contract("null, _ -> fail")
|
2020-10-24 10:45:27 +02:00
|
|
|
public static void notNull(@Nullable Object object, @NotNull String reason) {
|
|
|
|
if (Objects.isNull(object)) {
|
2020-05-23 04:20:01 +02:00
|
|
|
throw new NullPointerException(reason);
|
2020-10-24 10:45:27 +02:00
|
|
|
}
|
2020-05-23 04:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:23:41 +01:00
|
|
|
@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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:57:02 +01:00
|
|
|
@Contract("true, _ -> fail")
|
2020-10-24 10:45:27 +02:00
|
|
|
public static void argCondition(boolean condition, @NotNull String reason) {
|
|
|
|
if (condition) {
|
2020-05-23 04:20:01 +02:00
|
|
|
throw new IllegalArgumentException(reason);
|
2020-10-24 10:45:27 +02:00
|
|
|
}
|
2020-05-23 04:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 05:40:40 +02:00
|
|
|
@Contract("true, _, _ -> fail")
|
|
|
|
public static void argCondition(boolean condition, @NotNull String reason, Object... arguments) {
|
|
|
|
if (condition) {
|
|
|
|
throw new IllegalArgumentException(MessageFormat.format(reason, arguments));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:58:52 +01:00
|
|
|
@Contract("_ -> fail")
|
|
|
|
public static void fail(@NotNull String reason) {
|
|
|
|
throw new IllegalArgumentException(reason);
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:57:02 +01:00
|
|
|
@Contract("true, _ -> fail")
|
2020-10-24 10:45:27 +02:00
|
|
|
public static void stateCondition(boolean condition, @NotNull String reason) {
|
|
|
|
if (condition) {
|
2020-05-23 04:20:01 +02:00
|
|
|
throw new IllegalStateException(reason);
|
2020-10-24 10:45:27 +02:00
|
|
|
}
|
2020-05-23 04:20:01 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 20:40:27 +02:00
|
|
|
@Contract("true, _, _ -> fail")
|
|
|
|
public static void stateCondition(boolean condition, @NotNull String reason, Object... arguments) {
|
|
|
|
if (condition) {
|
|
|
|
throw new IllegalStateException(MessageFormat.format(reason, arguments));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 04:20:01 +02:00
|
|
|
}
|