Java断言工具

/**

  • 断言工具类
    */
    public class Accertion {

    // StringUtils.isBlank StringUtils.isEmpty的区别
    // StringUtils.isEmpty()只判断字符串是否为空和空字符串,如果字符串是" “它认为它不是空字符串的
    // StringUtils.isBlank()不仅判断字符串是否为空和空字符串,而且会判断其中的每一个字符是否是空字符串,如果字符串是” ",
    // 它也会认为是空字符串
    // 断言字符串为空
    public static void isBlank(String str,String message){
    // 如果字符串不为空,就抛出异常
    if(StringUtils.isNotBlank(str)){
    throw new BusinessException(message);
    }
    }

    // 断言字符串不为空
    public static void isNotBlank(String str, ResponseCode message){
    // 如果字符串为空,就抛出异常
    if(StringUtils.isBlank(str)){
    throw new BusinessException(message);
    }
    }

    // 断言对象为空
    public static void isNull(Object obj,String message){ //如果传入的对象是空,没问题,如果不是空,就抛出异常 前置请求,后者响应
    // 如果对象不为空,就抛出异常 执行if
    if(Objects.nonNull(obj)){
    throw new BusinessException(message);
    }
    }

    // 断言对象不为空 返回错误码
    public static void isNull(Object obj,ResponseCode responseCode){
    // 如果对象为空,就抛出异常
    if(Objects.nonNull(obj)){
    throw new BusinessException(responseCode);
    }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,String message){ //如果传入的对象不是空,没问题,如果是空,就抛出异常 执行if
    // 如果对象为空,就抛出异常
    if(Objects.isNull(obj)){
    throw new BusinessException(message);
    }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj, ResponseCode responseCode){ //如果传入的对象不是空,没问题,如果是空,就抛出异常 执行if
    // 如果对象为空,就抛出异常
    if(Objects.isNull(obj)){
    throw new BusinessException(responseCode);
    }
    }

    // 断言集合为空
    public static void isNull(Collection collection, String message){
    // 如果集合不为空,就抛出异常
    if(collection != null && collection.size() > 0){
    throw new BusinessException(message);
    }
    }

    // 断言集合不为空
    public static void isNotNull(Collection collection, String message){
    // 如果集合为空,就抛出异常
    if(collection == null || collection.size() == 0){
    throw new BusinessException(message);
    }
    }

    // 断言两个字符串值相等
    public static void isEq(String str1,String str2,String message){
    // 如果两个字符串不相等,就抛出异常
    if(!str1.equals(str2)){
    throw new BusinessException(message);
    }
    }
    // 断言两个字符串值相等
    public static void isEq(String str1, String str2, ResponseCode responseCode){
    // 如果两个字符串不相等,就抛出异常
    if(!str1.equals(str2)){
    throw new BusinessException(responseCode);
    }
    }

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值