断言工具类


使用断言工具类,优化开发代码 ,在需要校验的地方,执行 相应的校验即可,例如


AssertUtils.assertReplaceIf(params.isEmpty(), "参数不匹配,请核对");

校验不通过会抛出 ServiceException 异常 ,

ServiceException异常为项目所封装,可以根据项目需求,抛出自定义的异常。
全局拦截异常ServiceException,返回相关的返回信息,供前端展示,或提醒


package
org.springblade.common.utils; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.tool.utils.StringUtil; import java.util.Arrays; import java.util.Collection; import java.util.Objects; /** * tdf * 断言工具类 */ public class AssertUtils { private AssertUtils() { } /** * 断言或字符串不为空,若为空抛出ServiceException类型异常 */ public static void assertNotNull(Object obj, String msg) { if (obj == null || "".equals(obj)) { throw new ServiceException(msg); } } /** * 断言或字符串为空,若不为空抛出ServiceException类型异常 */ public static void assertNull(Object obj, String msg) { if (obj == null) { throw new ServiceException(msg); } } /** * 断言为true,若不为true抛出ServiceException类型异常 */ public static void assertTrue(boolean condition, String msg) { if (!condition) { throw new ServiceException(msg); } } /** * 断言 类似于if操作,替换if操作, */ public static void assertReplaceIf(boolean condition, String msg) { if (condition) { throw new ServiceException(msg); } } /** * 断言 类似于if操作,替换if操作, */ public static void assertNotNullReplaceIf(String condition, String msg) { if (StringUtil.isNotBlank(condition)) { throw new ServiceException(msg + "【"+condition+"】"); } } /** * 断言为false,若不为false抛出ServiceException类型异常 */ public static void assertFalse(boolean condition, String msg) { if (condition) { throw new ServiceException(msg); } } /** * 判断某俩个字符串或对象相同,若不相同抛出ServiceException类型异常 * */ public static void assertEquals(Object expected, Object actual, String msg) { if (!Objects.equals(expected, actual)) { throw new ServiceException(msg); } } /** * 判断某俩个字符串或对象不相同,若相同抛出ServiceException类型异常 * */ public static void assertNotEquals(Object expected, Object actual, String msg) { if (Objects.equals(expected, actual)) { throw new ServiceException(msg ); } } /** * 断言俩个数组元素相同,若不相同抛出ServiceException类型异常 * */ public static void assertArrayEquals(Object[] expected, Object[] actual, String msg) { if (!Arrays.equals(expected, actual)) { throw new ServiceException(msg); } } /** * 断言俩个数组元素不相同,若相同抛出ServiceException类型异常 * */ public static void assertArrayNotEquals(Object[] expected, Object[] actual, String msg) { if (Arrays.equals(expected, actual)) { throw new ServiceException(msg); } } /** * 断言俩个对象相同,若不相同抛出ServiceException类型异常 * */ public static void assertSame(Object expected, Object actual, String msg) { if (expected != actual) { throw new ServiceException(msg + ", Expected: " + expected + ", Actual: " + actual); } } /** * 断言俩个对象不相同,若相同抛出ServiceException类型异常 * */ public static void assertNotSame(Object expected, Object actual, String msg) { if (expected == actual) { throw new ServiceException(msg + ", Expected and actual should not be the same."); } } /** * 断言错误,直接抛出ServiceException类型异常,打印fail信息 * */ public static void fail(String msg) { throw new ServiceException(msg); } public static void empty(Collection<?> coll, String msg) { if (coll == null || coll.isEmpty()){ throw new ServiceException(msg); } } public static void notEmpty(Collection<?> coll, String msg) { if (coll != null && !coll.isEmpty()){ throw new ServiceException(msg); } } }

 

posted @ 2024-03-05 14:51  沐海风  阅读(27)  评论(0编辑  收藏  举报