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

com.hn.utils.AssertUtils Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.utils;

import cn.hutool.core.util.StrUtil;

/**
 *  check 工具类
 * @author fei
 * 2019-11-19
 */
public class AssertUtils {

    /**
     *  判断data是否为null,为null抛出异常
     * @param data 数据
     * @param errorMsg 错误信息
     * @throws IllegalArgumentException 参数格式错误
     * @return T data
     */
    public static T notNull(T data, String errorMsg) {
        if (data == null) {
            throw new IllegalArgumentException(errorMsg);
        }
        if (data instanceof String) {
            if (StrUtil.isBlank((String) data)) {
                throw new IllegalArgumentException(errorMsg);
            }
        }
        return data;
    }

    /**
     *  判断data是否为null,为null抛出异常
     * @param data 数据
     * @param e 错误异常
     * @throws RuntimeException 参数格式错误
     * @return T data
     */
    public static T notNull(T data,RuntimeException e) {
        if (data == null) {
            throw e;
        }
        if (data instanceof String) {
            if (StrUtil.isBlank((String) data)) {
                throw e;
            }
        }
        return data;
    }

    /**
     *  判断表达式是否返回true
     * @param expression 表达
     * @param e 错误信息
     * @throws RuntimeException 参数格式错误
     */
    public static void isTrue(boolean expression, RuntimeException e) {
        if (!expression) {
            throw e;
        }
    }

    /**
     *  判断表达式是否返回true
     * @param expression 表达
     * @param errorMsg 错误信息
     * @throws IllegalArgumentException 参数格式错误
     */
    public static void isTrue(boolean expression, String errorMsg) {
        if (!expression) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    /**
     *  抛出非法数据异常
     * @param errorMsg 错误信息
     * @throws IllegalArgumentException 参数格式错误
     */
    public static void error(String errorMsg) {
        throw new IllegalArgumentException(errorMsg);
    }

    /**
     *  抛出非法数据异常
     * @param e 错误异常对象
     * @throws RuntimeException 运行时异常
     */
    public static void error(RuntimeException e) {
        throw e;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy