package fr.she3y.util;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
public class Objects {
@Contract(value = "false, _, _ -> fail", pure = true)
public static <E extends RuntimeException, T> void require(boolean op, Function<T, E> exception, T description) throws E {
if(!op) throw exception.apply(description);
}
@Contract(value = "false, _ -> fail", pure = true)
public static void require(boolean op, String message) {
require(op, IllegalArgumentException::new, message);
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireEmpty(String name, @Nullable Collection<?> obj) {
requireNonNull(name, obj);
require(obj.isEmpty(), name + " must be empty");
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireEmpty(String name, @Nullable Map<?, ?> obj) {
requireNonNull(name, obj);
require(obj.isEmpty(), name + " must be empty");
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireEmpty(String name, @Nullable String obj) {
requireNonNull(name, obj);
require(obj.isEmpty(), name + " must be empty");
}
public static <T, S extends T> void requireEqual(String name, @Nullable S a, T b) {
//noinspection EqualsReplaceableByObjectsCall
require((a == b) || (a != null && a.equals(b)), name + " must be equal to " + b + ", got " + a);
}
@Contract(value = "_, true -> fail", pure = true)
public static void requireFalse(String name, boolean op) {
require(!op, name + " must be false");
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireNonEmpty(String name, @Nullable Collection<?> obj) {
requireNonNull(name, obj);
require(!obj.isEmpty(), name + " must not be empty");
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireNonEmpty(String name, @Nullable Map<?, ?> obj) {
requireNonNull(name, obj);
require(!obj.isEmpty(), name + " must not be empty");
}
@Contract(value = "_, null -> fail", pure = true)
public static void requireNonEmpty(String name, @Nullable String obj) {
requireNonNull(name, obj);
require(!obj.isEmpty(), name + " must not be empty");
}
public static <T, S extends T> void requireNonEqual(String name, @Nullable S a, T b) {
require(a == null || !a.equals(b), name + " must not be equal to " + b);
}
@Contract(value = "_, null -> fail", pure = true)
public static <T> void requireNonNull(String name, @Nullable T obj) {
require(obj != null, NullPointerException::new, name + " must not be null");
}
@Contract(value = "_, !null -> fail", pure = true)
public static <T> void requireNull(String name, @Nullable T obj) {
require(obj == null, name + " must be null");
}
@Contract(value = "_, false -> fail", pure = true)
public static void requireTrue(String name, boolean op) {
require(op, name + " must be true");
}
}