前言
java 开发过程中,应使用全局统一的断言工具类,使系统的断言处理一致,便于维护。
什么时候使用?
当对象空值或非空校验,并需要抛出异常时,就必须调用统一断言工具类。
断言工具类
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.Objects;
/**
* 断言类
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BusinessAsserts {
/**
* 是否为true,false抛出异常
*
* @param expression bool
* @param message 提示信息
*/
public static void isTrue(boolean expression, String message) {
isTrue(expression, message, StrUtil.EMPTY);
}
public static void isTrue(boolean expression, String message, Object... args) {
if (!expression) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否为false,true抛出异常
*
* @param expression bool
* @param message 提示信息
*/
public static void isFalse(boolean expression, String message) {
isTrue(!expression, message);
}
public static void isFalse(boolean expression, String message, Object... args) {
isTrue(!expression, message, StrUtil.format(message, args));
}
/**
* 是否为null,不为null抛出异常
*
* @param object obj
* @param message 提示信息
*/
public static void isNull(Object object, String message) {
isNull(object, message, StrUtil.EMPTY);
}
public static void isNull(Object object, String message, Object... args) {
if (Objects.nonNull(object)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否不为null,null抛出异常
*
* @param object obj
* @param message 提示信息
*/
public static void isNotNull(Object object, String message) {
isNotNull(object, message, StrUtil.EMPTY);
}
public static void isNotNull(Object object, String message, Object... args) {
if (Objects.isNull(object)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否allNotNull,hasNull抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isAllNotNull(Collection<?> col, String message) {
isAllNotNull(col, message, StrUtil.EMPTY);
}
public static void isAllNotNull(Collection<?> col, String message, Object... args) {
Objects.requireNonNull(col);
if (ObjectUtil.hasNull(col)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否为blank,notBlank抛出异常
*
* @param string str
* @param message 提示信息
*/
public static void isBlank(String string, String message) {
isBlank(string, message, StrUtil.EMPTY);
}
public static void isBlank(String string, String message, Object... args) {
if (StrUtil.isNotBlank(string)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否notBlank,blank抛出异常
*
* @param string str
* @param message 提示信息
*/
public static void isNotBlank(String string, String message) {
isNotBlank(string, message, StrUtil.EMPTY);
}
public static void isNotBlank(String string, String message, Object... args) {
if (StrUtil.isBlank(string)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否allBlank,hasNotBlank抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isAllBlank(Collection<String> col, String message) {
isAllBlank(col, message, StrUtil.EMPTY);
}
public static void isAllBlank(Collection<String> col, String message, Object... args) {
Objects.requireNonNull(col);
String[] strings = col.stream().toArray(String[]::new);
if (!StrUtil.hasBlank(strings)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否allNotBlank,hasBlank抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isAllNotBlank(Collection<String> col, String message) {
isAllNotBlank(col, message, StrUtil.EMPTY);
}
public static void isAllNotBlank(Collection<String> col, String message, Object... args) {
Objects.requireNonNull(col);
String[] strings = col.stream().toArray(String[]::new);
if (StrUtil.hasBlank(strings)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否equal,notEqual抛出异常
*
* @param object1 obj1
* @param object2 obj2
* @param message 提示信息
*/
public static void equal(Object object1, Object object2, String message) {
equal(object1, object2, message, StrUtil.EMPTY);
}
public static void equal(Object object1, Object object2, String message, Object... args) {
if (!Objects.equals(object1, object2)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否notEqual,equal抛出异常
*
* @param object1 obj1
* @param object2 obj2
* @param message 提示信息
*/
public static void notEqual(Object object1, Object object2, String message) {
notEqual(object1, object2, message, StrUtil.EMPTY);
}
public static void notEqual(Object object1, Object object2, String message, Object... args) {
if (Objects.equals(object1, object2)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否empty,notEmpty抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isEmpty(Collection<?> col, String message) {
isEmpty(col, message, StrUtil.EMPTY);
}
public static void isEmpty(Collection<?> col, String message, Object... args) {
if (CollectionUtil.isNotEmpty(col)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否notEmpty,empty抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isNotEmpty(Collection<?> col, String message) {
isNotEmpty(col, message, StrUtil.EMPTY);
}
public static void isNotEmpty(Collection<?> col, String message, Object... args) {
if (CollectionUtil.isEmpty(col)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
/**
* 是否allNotEmpty,hasEmpty抛出异常
*
* @param col 集合
* @param message 提示信息
*/
public static void isAllNotEmpty(Collection<?> col, String message) {
isAllNotEmpty(col, message, StrUtil.EMPTY);
}
public static void isAllNotEmpty(Collection<?> col, String message, Object... args) {
Objects.requireNonNull(col);
if (ObjectUtil.hasEmpty(col)) {
throw new ServiceException(StrUtil.format(message, args));
}
}
}