From 979ff1760f6f0c5361ce19d905c6ca70179c549f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 12 Oct 2024 00:21:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95=E5=90=8D?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/ArrayTools.java | 2 +- .../plusone/commons/util/AssertTools.java | 21 ++++++++++++------- .../plusone/commons/util/BigDecimals.java | 12 +++++------ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java index 5c416f4..9f3c312 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java @@ -179,7 +179,7 @@ public class ArrayTools { * @throws IllegalArgumentException 当参数为空时抛出 */ public static boolean isAllElementsNotNull(@Nonnull final T[] arr) { - AssertTools.checkParameter(arr != null, "The array cannot be null."); + AssertTools.checkArgument(arr != null, "The array cannot be null."); for (T element : arr) { if (element == null) { return false; 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 95512eb..0c84bda 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/AssertTools.java @@ -29,27 +29,34 @@ import javax.annotation.Nonnull; */ public class AssertTools { - public static void checkParameterNotNull(String paramName, T value) { - checkCondition(value != null, - () -> new IllegalArgumentException(String.format("The parameter \"%s\" cannot be null.", paramName))); + public static void checkArgumentNotNull(T obj) { + checkCondition(obj != null, () -> new IllegalArgumentException("The argument cannot be null.")); } - public static void checkParameterNotNull(T obj, String errMsg) { + public static void checkArgumentNotNull(T obj, String errMsg) { checkCondition(obj != null, () -> new IllegalArgumentException(errMsg)); } - public static void checkParameterNotNull(T obj, String format, Object... args) { + public static void checkArgumentNotNull(T obj, String format, Object... args) { checkCondition(obj != null, () -> new IllegalArgumentException(String.format(format, args))); } - public static void checkParameter(boolean condition, String errMsg) { + public static void checkArgument(boolean condition) { + checkCondition(condition, IllegalArgumentException::new); + } + + public static void checkArgument(boolean condition, String errMsg) { checkCondition(condition, () -> new IllegalArgumentException(errMsg)); } - public static void checkParameter(boolean condition, String format, Object... args) { + public static void checkArgument(boolean condition, String format, Object... args) { checkCondition(condition, () -> new IllegalArgumentException(String.format(format, args))); } + public static void checkState(boolean condition) { + checkCondition(condition, IllegalStateException::new); + } + public static void checkState(boolean condition, String errMsg) { checkCondition(condition, () -> new IllegalStateException(errMsg)); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java b/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java index cce0335..cc86677 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/BigDecimals.java @@ -51,7 +51,7 @@ public class BigDecimals { * 使用四舍五入({@link RoundingMode#HALF_UP}),保留两位小数,转为字符串。 */ public static String toPlainString(@Nonnull BigDecimal value) { - AssertTools.checkParameterNotNull("value", value); + AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null."); return toPlainStringInternal(value, DEFAULT_STR_SCALE, DEFAULT_STR_ROUNDING_MODE); } @@ -59,7 +59,7 @@ public class BigDecimals { * 使用四舍五入({@link RoundingMode#HALF_UP}),保留指定位的小数,转为字符串。 */ public static String toPlainString(@Nonnull BigDecimal value, int scale) { - AssertTools.checkParameterNotNull("value", value); + AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null."); return toPlainStringInternal(value, scale, DEFAULT_STR_ROUNDING_MODE); } @@ -67,8 +67,8 @@ public class BigDecimals { * 使用指定 {@link RoundingMode},保留两位小数,转为字符串。 */ public static String toPlainString(@Nonnull BigDecimal value, @Nonnull RoundingMode roundingMode) { - AssertTools.checkParameterNotNull("value", value); - AssertTools.checkParameterNotNull("rounding mode", roundingMode); + AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null."); + AssertTools.checkArgumentNotNull(roundingMode, "The argument \"rounding mode\" cannot be null."); return toPlainStringInternal(value, DEFAULT_STR_SCALE, roundingMode); } @@ -77,8 +77,8 @@ public class BigDecimals { */ public static String toPlainString(@Nonnull BigDecimal value, int scale, @Nonnull RoundingMode roundingMode) { - AssertTools.checkParameterNotNull("value", value); - AssertTools.checkParameterNotNull("rounding mode", roundingMode); + AssertTools.checkArgumentNotNull(value, "The argument \"value\" cannot be null."); + AssertTools.checkArgumentNotNull(roundingMode, "The argument \"rounding mode\" cannot be null."); return toPlainStringInternal(value, scale, roundingMode); }