From 210ff8882bd625464c03578adb887fde8522a057 Mon Sep 17 00:00:00 2001 From: Uncarbon <4840454+uncarbon97@user.noreply.gitee.com> Date: Wed, 13 Oct 2021 11:04:58 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=A2=9E=E5=8A=A0checkBetween=E7=9A=84?= =?UTF-8?q?=E5=87=A0=E4=B8=AA=E9=87=8D=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/lang/Assert.java | 104 ++++++++++++++++-- 1 file changed, 97 insertions(+), 7 deletions(-) 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 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 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 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; }