From 4ea3a3a6ab4349b16eeb9affba26f1e4b0a98558 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 18 Oct 2024 18:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=BD=BD=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/AssertTools.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java index c255d1b..d2446d6 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java @@ -24,21 +24,34 @@ import javax.annotation.Nonnull; * 断言工具 * *

- * 不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。 * 本工具类基本仅对表达式进行判断,并在表达式为 {@code false} 时抛出对应异常。 + * 不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。 + *

+ * + *
+ * AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
+ * AssertTools.checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
+ * AssertTools.checkCondition(!CollectionUtils.isEmpty(roles), () -> new InvalidInputException("The roles cannot be empty."));
+ * 
+ * + * @author ZhouXY */ public class AssertTools { - public static void checkArgumentNotNull(T obj) { - checkCondition(obj != null, () -> new IllegalArgumentException("The argument cannot be null.")); + public static void checkArgumentNotNull(T argument) { + checkCondition(argument != null, () -> new IllegalArgumentException("The argument cannot be null.")); } - public static void checkArgumentNotNull(T obj, String errMsg) { - checkCondition(obj != null, () -> new IllegalArgumentException(errMsg)); + public static void checkArgumentNotNull(T argument, String errMsg) { + checkCondition(argument != null, () -> new IllegalArgumentException(errMsg)); } - public static void checkArgumentNotNull(T obj, String format, Object... args) { - checkCondition(obj != null, () -> new IllegalArgumentException(String.format(format, args))); + public static void checkArgumentNotNull(T argument, Supplier messageSupplier) { + checkCondition(argument != null, () -> new IllegalArgumentException(messageSupplier.get())); + } + + public static void checkArgumentNotNull(T argument, String format, Object... args) { + checkCondition(argument != null, () -> new IllegalArgumentException(String.format(format, args))); } public static void checkArgument(boolean condition) { @@ -49,6 +62,10 @@ public class AssertTools { checkCondition(condition, () -> new IllegalArgumentException(errMsg)); } + public static void checkArgument(boolean condition, Supplier messageSupplier) { + checkCondition(condition, () -> new IllegalArgumentException(messageSupplier.get())); + } + public static void checkArgument(boolean condition, String format, Object... args) { checkCondition(condition, () -> new IllegalArgumentException(String.format(format, args))); } @@ -61,6 +78,10 @@ public class AssertTools { checkCondition(condition, () -> new IllegalStateException(errMsg)); } + public static void checkState(boolean condition, Supplier messageSupplier) { + checkCondition(condition, () -> new IllegalStateException(messageSupplier.get())); + } + public static void checkState(boolean condition, String format, Object... args) { checkCondition(condition, () -> new IllegalStateException(String.format(format, args))); }