From bf05d11d8e1301ba21849935674e4a449471a4c6 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 25 Sep 2025 19:13:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Assert=20=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E5=9C=A8=E6=89=A7=E8=A1=8C=20update=20=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E8=BF=9B=E8=A1=8C=E6=96=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/query/Assert.java | 107 ++++++++++++++++++ .../com/mybatisflex/coretest/AssertTest.java | 21 ++++ 2 files changed, 128 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Assert.java create mode 100644 mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AssertTest.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Assert.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Assert.java new file mode 100644 index 00000000..96070fd7 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Assert.java @@ -0,0 +1,107 @@ +package com.mybatisflex.core.query; + +import com.mybatisflex.core.util.StringUtil; + +public class Assert { + + + private Assert() { + } + + + /** + * 断言表达式为 true + * + * @param 要返回的值类型 + * @param value 要返回的值 + * @param expression 断言表达式 + * @return 原始值 + * @throws IllegalArgumentException 当表达式为 false 时抛出 + */ + public static T that(T value, boolean expression) { + if (!expression) { + throw new IllegalArgumentException("Assertion failed"); + } + return value; + } + + /** + * 断言表达式为 true,成功返回指定的值 + * + * @param 要返回的值类型 + * @param value 要返回的值 + * @param expression 断言表达式 + * @param message 异常消息 + * @return 原始值 + * @throws IllegalArgumentException 当表达式为 false 时抛出 + */ + public static T that(T value, boolean expression, String message) { + if (!expression) { + throw new IllegalArgumentException(message != null ? message : "Assertion failed"); + } + return value; + } + + + /** + * 断言字符串不为空白(延迟计算异常消息) + * + * @param text 要验证的字符串 + * @param message 异常消息 + * @return 原始字符串 + * @throws IllegalArgumentException 当字符串为空白时抛出 + */ + public static String hasText(String text, String message) { + if (StringUtil.noText(text)) { + throw new IllegalArgumentException( + StringUtil.hasText(message) ? message : "value must have text" + ); + } + return text; + } + + + /** + * 断言字符串不为空白 + * + * @param text 要验证的参数 + * @return 原始字符串 + * @throws IllegalArgumentException 当字符串为空白时抛出 + */ + public static boolean hasText(String text) { + if (StringUtil.noText(text)) { + throw new IllegalArgumentException("value must have text"); + } + return true; + } + + + /** + * 断言对象不为空 + * + * @param obj 要验证的参数 + * @return 原始对象 + * @throws IllegalArgumentException 当对象为空时抛出 + */ + public static boolean notNull(Object obj) { + if (obj == null) { + throw new IllegalArgumentException("value must not be null"); + } + return true; + } + + /** + * 断言对象不为空 + * + * @param obj 要验证的参数 + * @param message 异常消息 + * @return 原始对象 + * @throws IllegalArgumentException 当对象为空时抛出 + */ + public static boolean notNull(Object obj, String message) { + if (obj == null) { + throw new IllegalArgumentException(StringUtil.hasText(message) ? message : "value must not be null"); + } + return true; + } +} diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AssertTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AssertTest.java new file mode 100644 index 00000000..6dceeed6 --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AssertTest.java @@ -0,0 +1,21 @@ +package com.mybatisflex.coretest; + +import com.mybatisflex.core.query.Assert; +import com.mybatisflex.core.query.QueryWrapper; +import org.junit.Test; + +import static com.mybatisflex.core.query.QueryMethods.column; + + +public class AssertTest { + + + @Test(expected = IllegalArgumentException.class) + public void testAssert() { + String testArg = null; + QueryWrapper queryWrapper = new QueryWrapper() + .from("account") + .where(column("id").eq(1, Assert::notNull)) + .and(column("name").eq(testArg, Assert::hasText)); + } +}