自定义的一个Java断言工具类Accertion

57 阅读2分钟

/**
 * 断言工具类
 */
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,String 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);
        }
    }
}