diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java
index 1efdd5aef..e97735163 100644
--- a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java
+++ b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java
@@ -16,6 +16,9 @@ import java.util.function.Supplier;
*/
public class Assert {
+ private static final String TEMPLATE_VALUE_MUST_BE_BETWEEN_AND = "The value must be between {} and {}.";
+
+
/**
* 断言是否为真,如果为 {@code false} 抛出给定的异常
*
@@ -834,6 +837,38 @@ public class Assert {
return index;
}
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @param errorSupplier 错误抛出异常附带的消息生产接口
+ * @throws X if value is out of bound
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static int checkBetween(int value, int min, int max, Supplier extends X> errorSupplier) throws X {
+ if (value < min || value > max) {
+ throw errorSupplier.get();
+ }
+
+ return value;
+ }
+
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static int checkBetween(int value, int min, int max, String errorMsgTemplate, Object... params) {
+ return checkBetween(value, min, max, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)));
+ }
+
/**
* 检查值是否在指定范围内
*
@@ -844,12 +879,41 @@ public class Assert {
* @since 4.1.10
*/
public static int checkBetween(int value, int min, int max) {
+ return checkBetween(value, min, max, TEMPLATE_VALUE_MUST_BE_BETWEEN_AND, min, max);
+ }
+
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @param errorSupplier 错误抛出异常附带的消息生产接口
+ * @throws X if value is out of bound
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static long checkBetween(long value, long min, long max, Supplier extends X> errorSupplier) throws X {
if (value < min || value > max) {
- throw new IllegalArgumentException(StrUtil.format("Length must be between {} and {}.", min, max));
+ throw errorSupplier.get();
}
+
return value;
}
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static long checkBetween(long value, long min, long max, String errorMsgTemplate, Object... params) {
+ return checkBetween(value, min, max, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)));
+ }
+
/**
* 检查值是否在指定范围内
*
@@ -860,12 +924,41 @@ public class Assert {
* @since 4.1.10
*/
public static long checkBetween(long value, long min, long max) {
+ return checkBetween(value, min, max, TEMPLATE_VALUE_MUST_BE_BETWEEN_AND, min, max);
+ }
+
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @param errorSupplier 错误抛出异常附带的消息生产接口
+ * @throws X if value is out of bound
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static double checkBetween(double value, double min, double max, Supplier extends X> errorSupplier) throws X {
if (value < min || value > max) {
- throw new IllegalArgumentException(StrUtil.format("Length must be between {} and {}.", min, max));
+ throw errorSupplier.get();
}
+
return value;
}
+ /**
+ * 检查值是否在指定范围内
+ *
+ * @param value 值
+ * @param min 最小值(包含)
+ * @param max 最大值(包含)
+ * @return 经过检查后的值
+ * @since 5.7.15
+ */
+ public static double checkBetween(double value, double min, double max, String errorMsgTemplate, Object... params) {
+ return checkBetween(value, min, max, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)));
+ }
+
/**
* 检查值是否在指定范围内
*
@@ -876,10 +969,7 @@ public class Assert {
* @since 4.1.10
*/
public static double checkBetween(double value, double min, double max) {
- if (value < min || value > max) {
- throw new IllegalArgumentException(StrUtil.format("Length must be between {} and {}.", min, max));
- }
- return value;
+ return checkBetween(value, min, max, TEMPLATE_VALUE_MUST_BE_BETWEEN_AND, min, max);
}
/**
@@ -899,7 +989,7 @@ public class Assert {
double minDouble = min.doubleValue();
double maxDouble = max.doubleValue();
if (valueDouble < minDouble || valueDouble > maxDouble) {
- throw new IllegalArgumentException(StrUtil.format("Length must be between {} and {}.", min, max));
+ throw new IllegalArgumentException(StrUtil.format(TEMPLATE_VALUE_MUST_BE_BETWEEN_AND, min, max));
}
return value;
}