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