断言工具类

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;
这些方法用于对表达式、对象、集合和字符串进行断言,如果不满足条件,则抛出 IllegalArgumentException 异常
public class Assert {

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

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

    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

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

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Collection<?> collection) {
        notEmpty(collection,
                "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(Map<?, ?> map) {
        notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
    }

    public static void notEmpty(String str, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notEmpty(String str) {
        if (StringUtils.isEmpty(str)) {
            notEmpty(str, "[Assertion failed] - this string must not be empty");
        }
    }

    public static void notBlank(String str, String message) {
        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void notBlank(String str) {
        notBlank(str, "[Assertion failed] - this string must not be blank");
    }

    public static void hasText(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasText(String text) {
        hasText(text,
                "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
    }
}

以下是该类中定义的一些断言方法及其作用:

  • isTrue(boolean expression, String message):如果表达式为 false,则抛出带有指定消息的异常。
  • isTrue(boolean expression):如果表达式为 false,则抛出默认消息的异常。
  • isNull(Object object, String message):如果对象不为空,则抛出带有指定消息的异常。
  • isNull(Object object):如果对象不为空,则抛出默认消息的异常。
  • notNull(Object object, String message):如果对象为空,则抛出带有指定消息的异常。
  • notNull(Object object):如果对象为空,则抛出默认消息的异常。
  • notEmpty(Collection<?> collection, String message):如果集合为空,则抛出带有指定消息的异常。
  • notEmpty(Collection<?> collection):如果集合为空,则抛出默认消息的异常。
  • notEmpty(Map<?, ?> map, String message):如果映射为空,则抛出带有指定消息的异常。
  • notEmpty(Map<?, ?> map):如果映射为空,则抛出默认消息的异常。
  • notEmpty(String str, String message):如果字符串为空,则抛出带有指定消息的异常。
  • notEmpty(String str):如果字符串为空,则抛出默认消息的异常。
  • notBlank(String str, String message):如果字符串为空白,则抛出带有指定消息的异常。
  • notBlank(String str):如果字符串为空白,则抛出默认消息的异常。
  • hasText(String text, String message):如果字符串为 null、空或空白,则抛出带有指定消息的异常。
  • hasText(String text):如果字符串为 null、空或空白,则抛出默认消息的异常。
  • 35
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值