!436 feat 增加checkBetween的几个重载方法

Merge pull request !436 from Uncarbon/v5-dev
This commit is contained in:
Looly 2021-10-14 01:47:50 +00:00 committed by Gitee
commit d204d58057

View File

@ -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} 抛出给定的异常<br>
*
@ -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 <X extends Throwable> 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 <X extends Throwable> 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 <X extends Throwable> 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;
}