Skip to content

断言工具类

依赖

xml
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.8.28</version>
</dependency>

自定义异常

java
public class BizException extends RuntimeException{

    public BizException() {}
    public BizException(String message) {
        super(message);
    }
    public BizException(String message, Throwable cause) {
        super(message, cause);
    }

    public static BizException instance(String message) {
        return new BizException(message);
    }

    public static void wrap(String message){
        throw new BizException(message);
    }
}

工具类

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.util.Collection;

import java.util.Map;
import java.util.function.Supplier;

public class ArgumentAssert {

    public ArgumentAssert() {
    }

    public static <X extends Throwable> void isTrue(boolean expression, Supplier<? extends X> supplier) throws X {
        if (!expression) {
            throw (X)supplier.get();
        }
    }

    public static void isTrue(boolean expression, String errorMsgTemplate, Object... params) throws BizException {
        isTrue(expression, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static void isTrue(boolean expression) throws BizException {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    public static <X extends Throwable> void isFalse(boolean expression, Supplier<X> errorSupplier) throws X {
        if (expression) {
            throw (X)errorSupplier.get();
        }
    }

    public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws BizException {
        isFalse(expression, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static void isFalse(boolean expression) throws BizException {
        isFalse(expression, "[Assertion failed] - this expression must be false");
    }

    public static <X extends Throwable> void isNull(Object object, Supplier<X> errorSupplier) throws X {
        if (null != object) {
            throw (X)errorSupplier.get();
        }
    }

    public static void isNull(Object object, String errorMsgTemplate, Object... params) throws BizException {
        isNull(object, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static void isNull(Object object) throws BizException {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    public static <T, X extends Throwable> T notNull(T object, Supplier<X> errorSupplier) throws X {
        if (null == object) {
            throw (X)errorSupplier.get();
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, String errorMsgTemplate, Object... params) throws BizException {
        return notNull(object, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T, E> T notAllNull(T object, E obj, String errorMsgTemplate, Object... params) throws BizException {
        if (object == null && obj == null) {
            throw new BizException(StrUtil.format(errorMsgTemplate, params));
        } else {
            return object;
        }
    }

    public static <T, E> T notAnyNull(T object, E obj, String errorMsgTemplate, Object... params) throws BizException {
        if (object != null && obj != null) {
            return object;
        } else {
            throw new BizException(StrUtil.format(errorMsgTemplate, params));
        }
    }

    public static <T> T notNull(T object) throws BizException {
        return notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    public static <T extends CharSequence, X extends Throwable> T notEmpty(T text, Supplier<X> errorSupplier) throws X {
        if (StrUtil.isEmpty(text)) {
            throw (X)errorSupplier.get();
        } else {
            return text;
        }
    }

    public static <T extends CharSequence> T notEmpty(T text, String errorMsgTemplate, Object... params) throws BizException {
        return notEmpty(text, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T extends CharSequence> T notEmpty(T text) throws BizException {
        return notEmpty(text, "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    public static <T extends CharSequence, X extends Throwable> T notBlank(T text, Supplier<X> errorMsgSupplier) throws X {
        if (StrUtil.isBlank(text)) {
            throw (X)errorMsgSupplier.get();
        } else {
            return text;
        }
    }

    public static <T extends CharSequence> T notBlank(T text, String errorMsgTemplate, Object... params) throws BizException {
        return notBlank(text, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T extends CharSequence> T notBlank(T text) throws BizException {
        return notBlank(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
    }

    public static <T extends CharSequence, X extends Throwable> T notContain(CharSequence textToSearch, T substring, Supplier<X> errorSupplier) throws X {
        if (StrUtil.contains(textToSearch, substring)) {
            throw (X)errorSupplier.get();
        } else {
            return substring;
        }
    }

    public static String notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params) throws BizException {
        return (String)notContain((CharSequence)textToSearch, (CharSequence)substring, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static String notContain(String textToSearch, String substring) throws BizException {
        return notContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [{}]", substring);
    }

    public static <T> T contain(Collection<T> collection, T item) throws BizException {
        return contain(collection, item, "[断言失败] - 此集合中参数不得包含该值 [{}]", item);
    }

    public static <T> T contain(Collection<T> collection, T item, String errorMsgTemplate, Object... params) throws BizException {
        return contain(collection, item, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T, X extends Throwable> T contain(Collection<T> collection, T item, Supplier<X> errorSupplier) throws X {
        if (!CollUtil.contains(collection, item)) {
            throw (X)errorSupplier.get();
        } else {
            return item;
        }
    }

    public static <T> T notContain(Collection<T> collection, T item) throws BizException {
        return notContain(collection, item, "[断言失败] - 此集合中参数不得包含该值 [{}]", item);
    }

    public static <T> T notContain(Collection<T> collection, T item, String errorMsgTemplate, Object... params) throws BizException {
        return notContain(collection, item, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T, X extends Throwable> T notContain(Collection<T> collection, T item, Supplier<X> errorSupplier) throws X {
        if (CollUtil.contains(collection, item)) {
            throw (X)errorSupplier.get();
        } else {
            return item;
        }
    }

    public static <T, X extends Throwable> T[] notEmpty(T[] array, Supplier<X> errorSupplier) throws X {
        if (ArrayUtil.isEmpty(array)) {
            throw (X)errorSupplier.get();
        } else {
            return array;
        }
    }

    public static <T> T[] notEmpty(T[] array, String errorMsgTemplate, Object... params) throws BizException {
        return notEmpty(array, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T> T[] notEmpty(T[] array) throws BizException {
        return notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
    }

    public static <T, X extends Throwable> T[] noNullElements(T[] array, Supplier<X> errorSupplier) throws X {
        if (ArrayUtil.hasNull(array)) {
            throw (X)errorSupplier.get();
        } else {
            return array;
        }
    }

    public static <T> T[] noNullElements(T[] array, String errorMsgTemplate, Object... params) throws BizException {
        return noNullElements(array, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <T> T[] noNullElements(T[] array) throws BizException {
        return noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
    }

    public static <E, T extends Iterable<E>, X extends Throwable> T notEmpty(T collection, Supplier<X> errorSupplier) throws X {
        if (CollUtil.isEmpty(collection)) {
            throw (X)errorSupplier.get();
        } else {
            return collection;
        }
    }

    public static <E, T extends Iterable<E>> T notEmpty(T collection, String errorMsgTemplate, Object... params) throws BizException {
        return notEmpty(collection, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <E, T extends Iterable<E>> T notEmpty(T collection) throws BizException {
        return notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }

    public static <K, V, T extends Map<K, V>, X extends Throwable> T notEmpty(T map, Supplier<X> errorSupplier) throws X {
        if (MapUtil.isEmpty(map)) {
            throw (X)errorSupplier.get();
        } else {
            return map;
        }
    }

    public static <K, V, T extends Map<K, V>> T notEmpty(T map, String errorMsgTemplate, Object... params) throws BizException {
        return notEmpty(map, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <K, V, T extends Map<K, V>> T notEmpty(T map) throws BizException {
        return notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

    public static <T> T isInstanceOf(Class<?> type, T obj) {
        return isInstanceOf(type, obj, "Object [{}] is not instanceof [{}]", obj, type);
    }

    public static <T> T isInstanceOf(Class<?> type, T obj, String errorMsgTemplate, Object... params) throws BizException {
        notNull(type, "Type to check against must not be null");
        if (!type.isInstance(obj)) {
            throw new BizException(StrUtil.format(errorMsgTemplate, params));
        } else {
            return obj;
        }
    }

    public static void isAssignable(Class<?> superType, Class<?> subType) throws BizException {
        isAssignable(superType, subType, "{} is not assignable to {})", subType, superType);
    }

    public static void isAssignable(Class<?> superType, Class<?> subType, String errorMsgTemplate, Object... params) throws BizException {
        notNull(superType, "Type to check against must not be null");
        if (subType == null || !superType.isAssignableFrom(subType)) {
            throw new BizException(StrUtil.format(errorMsgTemplate, params));
        }
    }

    public static void state(boolean expression, Supplier<String> errorMsgSupplier) throws IllegalStateException {
        if (!expression) {
            throw new IllegalStateException((String)errorMsgSupplier.get());
        }
    }

    public static void state(boolean expression, String errorMsgTemplate, Object... params) throws IllegalStateException {
        if (!expression) {
            throw new IllegalStateException(StrUtil.format(errorMsgTemplate, params));
        }
    }

    public static void state(boolean expression) throws IllegalStateException {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

    public static int checkIndex(int index, int size) throws BizException, IndexOutOfBoundsException {
        return checkIndex(index, size, "[Assertion failed]");
    }

    public static int checkIndex(int index, int size, String errorMsgTemplate, Object... params) throws BizException, IndexOutOfBoundsException {
        if (index >= 0 && index < size) {
            return index;
        } else {
            throw new IndexOutOfBoundsException(badIndexMsg(index, size, errorMsgTemplate, params));
        }
    }

    public static int checkBetween(int value, int min, int max) {
        if (value >= min && value <= max) {
            return value;
        } else {
            throw new BizException(StrUtil.format("Length must be between {} and {}.", new Object[]{min, max}));
        }
    }

    public static LocalDateTime checkGt(LocalDateTime value, LocalDateTime min, String... msg) {
        if (value.isBefore(min)) {
            String message = null;
            if (ArrayUtil.isNotEmpty(msg)) {
                message = msg[0];
            } else {
                message = StrUtil.format("{} 必须大于 {} .", new Object[]{value, min});
            }

            throw new BizException(message);
        } else {
            return value;
        }
    }

    public static LocalDateTime checkLt(LocalDateTime value, LocalDateTime max, String... msg) {
        if (value.isAfter(max)) {
            String message = null;
            if (ArrayUtil.isNotEmpty(msg)) {
                message = msg[0];
            } else {
                message = StrUtil.format("{} 必须小于 {} .", new Object[]{value, max});
            }

            throw new BizException(message);
        } else {
            return value;
        }
    }

    public static long checkBetween(long value, long min, long max) {
        if (value >= min && value <= max) {
            return value;
        } else {
            throw new BizException(StrUtil.format("Length must be between {} and {}.", new Object[]{min, max}));
        }
    }

    public static double checkBetween(double value, double min, double max) {
        if (!(value < min) && !(value > max)) {
            return value;
        } else {
            throw new BizException(StrUtil.format("Length must be between {} and {}.", new Object[]{min, max}));
        }
    }

    public static Number checkBetween(Number value, Number min, Number max) {
        notNull(value);
        notNull(min);
        notNull(max);
        double valueDouble = value.doubleValue();
        double minDouble = min.doubleValue();
        double maxDouble = max.doubleValue();
        if (!(valueDouble < minDouble) && !(valueDouble > maxDouble)) {
            return value;
        } else {
            throw new BizException(StrUtil.format("Length must be between {} and {}.", new Object[]{min, max}));
        }
    }

    public static void equals(Object expected, Object actual, String errorMsgTemplate, Object... params) {
        equals(expected, actual, () -> {
            return new BizException(StrUtil.format(errorMsgTemplate, params));
        });
    }

    public static <X extends Throwable> void equals(Object expected, Object actual, Supplier<? extends X> supplier) throws X {
        if (!ObjectUtil.equals(expected, actual)) {
            throw (X)supplier.get();
        }
    }

    private static String badIndexMsg(int index, int size, String desc, Object... params) {
        if (index < 0) {
            return StrUtil.format("{} ({}) must not be negative", new Object[]{StrUtil.format(desc, params), index});
        } else if (size < 0) {
            throw new BizException("negative size: " + size);
        } else {
            return StrUtil.format("{} ({}) must be less than size ({})", new Object[]{StrUtil.format(desc, params), index, size});
        }
    }
}

Last updated:

开发者笔记仓库