java 自定义断言工具类

本文介绍了在Java开发中如何使用全局统一的断言工具类进行对象空值和非空校验,以及如何通过该工具类抛出异常,提升代码可维护性。
摘要由CSDN通过智能技术生成

前言

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));
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Whitemeen太白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值