手动编写Assert断言替换繁琐的if-else

断言作用

Assert关键字是在JDK1.4之后出现,使用Java中的 assert 语句实现,配合布尔表达式一起使用,达到调试程序开发过程中的判断、调试程序的作用。 在执行断言时,它被认为是正确的。 如果失败,JVM会抛出一个名为 AssertionError 的错误。 断言是默认关闭的,如果想使用断言进行判断,需要手动打开断言功能。如果要开启断言检查,则需使用-enableassertions 或 -ea JVM参数来开启;如果要手动忽略断言检查,则可以通过使用 -disableassertions 或 -da JVM参数来忽略断言语句。

注意:不在IDEA开启断言(项目在linux上以jar的形式运行时),运行这个demo,发现并没有报异常。这是因为Java在执行程序的时候默认是不启动断言检查的,即所有的断言语句都将被忽略。那么,这样大家就会觉得断言这个功能有些鸡肋了,只能是作为一种调试方式,或者在单元测试中使用。

手动编写断言

package com.bonc.bxm.portal.util.other;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.bonc.bxm.security.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Map;

/**
 * @Author :
 * @Date :Created in 2022/9/5 11:03
 * @Description:业务断言,替换if-ese
 */
@Slf4j
public class BizAssertUtils {

    /**
     * 如果入参collection集合为空,则抛出自定义异常
     * @param collection
     * @param excepMsg
     * @throws BaseException
     */
    public static void notEmpty(Collection collection, String excepMsg)  {
        if (CollectionUtil.isEmpty(collection)) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
    }

    /**
     * 如果入参collection集合为空,则抛出自定义异常
     * @param collection
     * @param excepMsg
     * @param className
     */
    public static void notEmpty(Collection collection, String excepMsg,Class<? extends RuntimeException> className)  {
        if (CollectionUtil.isEmpty(collection)) {
            log.info(excepMsg);
            RuntimeException exception = null;
            try {
                exception = className.getConstructor(String.class).newInstance(excepMsg);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
            log.info(excepMsg);
            throw exception;
        }
    }

    /**
     * 如果入参map为空,则抛出自定义异常
     * @param map
     * @param excepMsg
     * @throws BaseException
     */
    public static void notEmpty(Map map, String excepMsg)  {
        if (CollectionUtil.isEmpty(map)) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
    }

    /**
     * 如果入参map为空,则抛出自定义异常
     * @param map
     * @param excepMsg
     * @throws BaseException
     */
    public static void notEmpty(Map map, String excepMsg,Class<? extends RuntimeException> className)  {
        if (CollectionUtil.isEmpty(map)) {
            log.info(excepMsg);
            RuntimeException exception = null;
            try {
                exception = className.getConstructor(String.class).newInstance(excepMsg);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
            log.info(excepMsg);
            throw exception;
        }
    }

    /**
     * 如果入参对象为null,则抛出自定义异常
     * @param object
     * @param excepMsg
     * @param <T>
     * @return
     * @throws BaseException
     */
    public static <T> T notNull(T object,String excepMsg) {
        if (object == null) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
        return object;
    }

    /**
     * 如果入参text为空,则抛出自定义异常
     * @param text
     * @param excepMsg
     * @throws BaseException
     */
    public static void hasText(String text,String excepMsg) {
        if (StrUtil.isBlank(text)) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
    }

    /**
     *  断言是否为真,如果为 false 抛出 异常
     * @param expression
     * @param excepMsg
     * @throws BaseException
     */
    public static void isTrue(boolean expression, String excepMsg) {
        if (Constant.FALSE == expression) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
    }

    /**
     *  断言是否为真,如果为 false 抛出 异常
     * @param expression
     * @param excepMsg
     * @param className
     */
    public static void isTrue(boolean expression, String excepMsg,Class<? extends RuntimeException> className){
        if (Constant.FALSE == expression) {
            RuntimeException exception = null;
            try {
                exception = className.getConstructor(String.class).newInstance(excepMsg);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
            log.info(excepMsg);
            throw exception;
        }
    }

    /**
     * 断言是否为假,如果为 true 抛出 异常
     * @param expression
     * @param excepMsg
     * @throws BaseException
     */
    public static void isFalse(boolean expression, String excepMsg)  {
        if (expression) {
            log.info(excepMsg);
            throw new BaseException(excepMsg);
        }
    }

    /**
     * 断言是否为假,如果为 true 抛出 异常
     * @param expression
     * @param excepMsg
     * @param className
     * @throws BaseException
     */
    public static void isFalse(boolean expression, String excepMsg,Class<? extends RuntimeException> className){
        if (expression) {
            RuntimeException exception = null;
            try {
                exception = className.getConstructor(String.class).newInstance(excepMsg);
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
            log.info(excepMsg);
            throw exception;
        }
    }
}

posted @ 2022-11-29 09:24  KeepArlen  阅读(487)  评论(0)    收藏  举报