All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.utils.AssertUtil Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.utils;

import matrix.boot.common.exception.BusinessException;
import matrix.boot.common.exception.I18nBusinessException;
import matrix.boot.common.exception.ServiceException;

import java.util.regex.Pattern;

/**
 * 框架断言工具
 *
 * @author wangcheng
 * 2021/8/10
 **/
public class AssertUtil {

    /**
     * 判断不为空(带提示语)
     *
     * @param content 内容
     * @param varName 参数名称
     */
    public static void notNullTip(Object content, String varName) {
        AssertUtil.state(content != null && StringUtil.notEmpty(String.valueOf(content)), varName + " not be null!");
    }

    /**
     * 判断不为空
     *
     * @param content 内容
     * @param msg     错误信息
     */
    public static void notNull(Object content, String msg) {
        AssertUtil.state(content != null && StringUtil.notEmpty(String.valueOf(content)), msg);
    }

    /**
     * 判断有内容
     *
     * @param content 内容
     * @param msg     错误信息
     */
    public static void hasText(String content, String msg) {
        AssertUtil.state(StringUtil.notEmpty(content), msg);
    }

    /**
     * 不满足正则
     *
     * @param regex   正则
     * @param content 内容
     * @param msg     信息
     * @param flag    标记
     */
    public static void matchRegex(String regex, String content, String msg, Integer flag) {
        if (flag == null) {
            AssertUtil.state(Pattern.compile(regex).matcher(content).matches(), msg);
        } else {
            AssertUtil.state(Pattern.compile(regex, flag).matcher(content).matches(), msg);
        }
    }

    /**
     * 状态不为True
     *
     * @param value 是否正确,否抛异常
     * @param msg   异常消息
     */
    public static void state(Boolean value, String msg) {
        if (!value) {
            throw new BusinessException(msg);
        }
    }

    /**
     * 抛出i18异常
     *
     * @param value             是否正确,否抛异常
     * @param businessException 异常
     */
    public static void state(Boolean value, I18nBusinessException businessException) {
        if (!value) {
            throw businessException;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy