diff --git a/CHANGELOG.md b/CHANGELOG.md index b3d9e393b..4728f6010 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * 【core 】 优化Combination.countAll(pr#1159@Github) * 【core 】 优化针对list的split方法(pr#194@Gitee) * 【poi 】 ExcelWriter增加setRowStyle方法 +* 【core 】 Assert增加函数接口(pr#1166@Github) ### Bug修复 * 【core 】 解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee) 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 83b0ce9a6..c3137e67a 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 @@ -1,41 +1,21 @@ package cn.hutool.core.lang; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; -import java.util.Collection; import java.util.Map; import java.util.function.Supplier; /** * 断言
* 断言某些对象或值是否符合规定,否则抛出异常。经常用于做变量检查 - * - * @author Looly * + * @author Looly */ public class Assert { - /** - * 断言是否为真,如果为 {@code false} 抛出异常
- * 并使用指定的函数获取错误信息返回 - *
-	 *  Assert.isTrue(i > 0, ()->{
-	 *      // to query relation message
-	 *      return "relation message to return ";
-	 *  });
-	 * 
- * - * @param expression 布尔值 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 - * @throws IllegalArgumentException if expression is {@code false} - */ - public static void isTrue(boolean expression, Supplier errorMsgSupplier) throws IllegalArgumentException { - if (!expression) { - isTrue(false, errorMsgSupplier.get()); - } - } + /** * 断言是否为真,如果为 {@code false} 抛出给定的异常
* @@ -43,142 +23,144 @@ public class Assert { * Assert.isTrue(i > 0, IllegalArgumentException::new); * * - * @param 异常类型 + * @param 异常类型 * @param expression 布尔值 * @param supplier 指定断言不通过时抛出的异常 * @throws X if expression is {@code false} */ - public static void isTrue(boolean expression, Func0 supplier) throws X { + public static void isTrue(boolean expression, Supplier supplier) throws X { if (false == expression) { - throw supplier.callWithRuntimeException(); + throw supplier.get(); } } /** * 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常
- * + * *
 	 * Assert.isTrue(i > 0, "The value must be greater than zero");
 	 * 
- * - * @param expression 布尔值 + * + * @param expression 布尔值 * @param errorMsgTemplate 错误抛出异常附带的消息模板,变量用{}代替 - * @param params 参数列表 + * @param params 参数列表 * @throws IllegalArgumentException if expression is {@code false} */ public static void isTrue(boolean expression, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (false == expression) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); - } + isTrue(expression, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); } /** * 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常
- * + * *
 	 * Assert.isTrue(i > 0, "The value must be greater than zero");
 	 * 
- * + * * @param expression 布尔值 * @throws IllegalArgumentException if expression is {@code false} */ public static void isTrue(boolean expression) throws IllegalArgumentException { isTrue(expression, "[Assertion failed] - this expression must be true"); } + /** - * 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常
+ * 断言是否为假,如果为 {@code true} 抛出指定类型异常
* 并使用指定的函数获取错误信息返回 *
 	 *  Assert.isFalse(i > 0, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param expression 布尔值 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 - * @throws IllegalArgumentException if expression is {@code false} + * @param 异常类型 + * @param expression 布尔值 + * @param errorSupplier 指定断言不通过时抛出的异常 + * @throws X if expression is {@code false} + * @since 5.4.5 */ - public static void isFalse(boolean expression, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static void isFalse(boolean expression, Supplier errorSupplier) throws X { if (expression) { - isFalse(true, errorMsgSupplier.get()); - } - } - /** - * 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常
- * - *
-	 * Assert.isFalse(i < 0, "The value must be greater than zero");
-	 * 
- * - * @param expression 布尔值 - * @param errorMsgTemplate 错误抛出异常附带的消息模板,变量用{}代替 - * @param params 参数列表 - * @throws IllegalArgumentException if expression is {@code false} - */ - public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (expression) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + throw errorSupplier.get(); } } /** * 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常
- * + * + *
+	 * Assert.isFalse(i < 0, "The value must be greater than zero");
+	 * 
+ * + * @param expression 布尔值 + * @param errorMsgTemplate 错误抛出异常附带的消息模板,变量用{}代替 + * @param params 参数列表 + * @throws IllegalArgumentException if expression is {@code false} + */ + public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + isFalse(expression, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常
+ * *
 	 * Assert.isFalse(i < 0);
 	 * 
- * + * * @param expression 布尔值 * @throws IllegalArgumentException if expression is {@code false} */ public static void isFalse(boolean expression) throws IllegalArgumentException { isFalse(expression, "[Assertion failed] - this expression must be false"); } + /** - * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 + * 断言对象是否为{@code null} ,如果不为{@code null} 抛出指定类型异常 * 并使用指定的函数获取错误信息返回 *
-	 * Assert.isNull(value,  ()->{
+	 * Assert.isNull(value, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param object 被检查的对象 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 - * @throws IllegalArgumentException if the object is not {@code null} + * @param 异常类型 + * @param object 被检查的对象 + * @param errorSupplier 错误抛出异常附带的消息生产接口 + * @throws X if the object is not {@code null} + * @since 5.4.5 */ - public static void isNull(Object object, Supplier errorMsgSupplier) throws IllegalArgumentException { - if (object != null) { - isNull(object, errorMsgSupplier.get()); - } - } - /** - * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 - * - *
-	 * Assert.isNull(value, "The value must be null");
-	 * 
- * - * @param object 被检查的对象 - * @param errorMsgTemplate 消息模板,变量使用{}表示 - * @param params 参数列表 - * @throws IllegalArgumentException if the object is not {@code null} - */ - public static void isNull(Object object, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (object != null) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + public static void isNull(Object object, Supplier errorSupplier) throws X { + if (null != object) { + throw errorSupplier.get(); } } /** * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 - * + * + *
+	 * Assert.isNull(value, "The value must be null");
+	 * 
+ * + * @param object 被检查的对象 + * @param errorMsgTemplate 消息模板,变量使用{}表示 + * @param params 参数列表 + * @throws IllegalArgumentException if the object is not {@code null} + */ + public static void isNull(Object object, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + isNull(object, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 + * *
 	 * Assert.isNull(value);
 	 * 
- * + * * @param object 被检查对象 * @throws IllegalArgumentException if the object is not {@code null} */ @@ -187,57 +169,58 @@ public class Assert { } // ----------------------------------------------------------------------------------------------------------- Check not null + /** - * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} . + * 断言对象是否不为{@code null} ,如果为{@code null} 抛出指定类型异常 * 并使用指定的函数获取错误信息返回 *
 	 * Assert.notNull(clazz, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param 被检查对象泛型类型 - * @param object 被检查对象 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 被检查对象泛型类型 + * @param 异常类型 + * @param object 被检查对象 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 被检查后的对象 - * @throws IllegalArgumentException if the object is {@code null} + * @throws X if the object is {@code null} + * @since 5.4.5 */ - public static T notNull(T object, Supplier errorMsgSupplier) throws IllegalArgumentException { - if (object == null) { - notNull(null, errorMsgSupplier.get()); - } - return object; - } - /** - * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} . - * - *
-	 * Assert.notNull(clazz, "The class must not be null");
-	 * 
- * - * @param 被检查对象泛型类型 - * @param object 被检查对象 - * @param errorMsgTemplate 错误消息模板,变量使用{}表示 - * @param params 参数 - * @return 被检查后的对象 - * @throws IllegalArgumentException if the object is {@code null} - */ - public static T notNull(T object, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (object == null) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + public static T notNull(T object, Supplier errorSupplier) throws X { + if (null == object) { + throw errorSupplier.get(); } return object; } + /** + * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} . + * + *
+	 * Assert.notNull(clazz, "The class must not be null");
+	 * 
+ * + * @param 被检查对象泛型类型 + * @param object 被检查对象 + * @param errorMsgTemplate 错误消息模板,变量使用{}表示 + * @param params 参数 + * @return 被检查后的对象 + * @throws IllegalArgumentException if the object is {@code null} + */ + public static T notNull(T object, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notNull(object, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + /** * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 - * + * *
 	 * Assert.notNull(clazz);
 	 * 
- * - * @param 被检查对象类型 + * + * @param 被检查对象类型 * @param object 被检查对象 * @return 非空对象 * @throws IllegalArgumentException if the object is {@code null} @@ -247,292 +230,302 @@ public class Assert { } // ----------------------------------------------------------------------------------------------------------- Check empty + /** - * 检查给定字符串是否为空,为空抛出 {@link IllegalArgumentException} - * 并使用指定的函数获取错误信息返回 + * 检查给定字符串是否为空,为空抛出自定义异常,并使用指定的函数获取错误信息返回。 *
 	 * Assert.notEmpty(name, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param 字符串类型 - * @param text 被检查字符串 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 异常类型 + * @param 字符串类型 + * @param text 被检查字符串 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 非空字符串 + * @throws X 被检查字符串为空抛出此异常 * @see StrUtil#isNotEmpty(CharSequence) - * @throws IllegalArgumentException 被检查字符串为空 + * @since 5.4.5 */ - public static T notEmpty(T text, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static T notEmpty(T text, Supplier errorSupplier) throws X { if (StrUtil.isEmpty(text)) { - notEmpty(text, errorMsgSupplier.get()); + throw errorSupplier.get(); } return text; } + /** * 检查给定字符串是否为空,为空抛出 {@link IllegalArgumentException} - * + * *
 	 * Assert.notEmpty(name, "Name must not be empty");
 	 * 
* - * @param 字符串类型 - * @param text 被检查字符串 + * @param 字符串类型 + * @param text 被检查字符串 * @param errorMsgTemplate 错误消息模板,变量使用{}表示 - * @param params 参数 + * @param params 参数 * @return 非空字符串 - * @see StrUtil#isNotEmpty(CharSequence) * @throws IllegalArgumentException 被检查字符串为空 + * @see StrUtil#isNotEmpty(CharSequence) */ public static T notEmpty(T text, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (StrUtil.isEmpty(text)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); - } - return text; + return notEmpty(text, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); } /** * 检查给定字符串是否为空,为空抛出 {@link IllegalArgumentException} - * + * *
 	 * Assert.notEmpty(name);
 	 * 
* - * @param 字符串类型 + * @param 字符串类型 * @param text 被检查字符串 * @return 被检查的字符串 - * @see StrUtil#isNotEmpty(CharSequence) * @throws IllegalArgumentException 被检查字符串为空 + * @see StrUtil#isNotEmpty(CharSequence) */ public static T notEmpty(T text) throws IllegalArgumentException { return notEmpty(text, "[Assertion failed] - this String argument must have length; it must not be null or empty"); } + /** - * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException} + * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出自定义异常。 * 并使用指定的函数获取错误信息返回 *
 	 * Assert.notBlank(name, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param 字符串类型 - * @param text 被检查字符串 + * @param 异常类型 + * @param 字符串类型 + * @param text 被检查字符串 * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 * @return 非空字符串 + * @throws X 被检查字符串为空白 * @see StrUtil#isNotBlank(CharSequence) - * @throws IllegalArgumentException 被检查字符串为空白 */ - public static T notBlank(T text, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static T notBlank(T text, Supplier errorMsgSupplier) throws X { if (StrUtil.isBlank(text)) { - notBlank(text, errorMsgSupplier.get()); - } - return text; - } - /** - * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException} - * - *
-	 * Assert.notBlank(name, "Name must not be blank");
-	 * 
- * - * @param 字符串类型 - * @param text 被检查字符串 - * @param errorMsgTemplate 错误消息模板,变量使用{}表示 - * @param params 参数 - * @return 非空字符串 - * @see StrUtil#isNotBlank(CharSequence) - * @throws IllegalArgumentException 被检查字符串为空白 - */ - public static T notBlank(T text, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (StrUtil.isBlank(text)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + throw errorMsgSupplier.get(); } return text; } /** * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException} - * + * *
 	 * Assert.notBlank(name, "Name must not be blank");
 	 * 
* - * @param 字符串类型 + * @param 字符串类型 + * @param text 被检查字符串 + * @param errorMsgTemplate 错误消息模板,变量使用{}表示 + * @param params 参数 + * @return 非空字符串 + * @throws IllegalArgumentException 被检查字符串为空白 + * @see StrUtil#isNotBlank(CharSequence) + */ + public static T notBlank(T text, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notBlank(text, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException} + * + *
+	 * Assert.notBlank(name, "Name must not be blank");
+	 * 
+ * + * @param 字符串类型 * @param text 被检查字符串 * @return 非空字符串 - * @see StrUtil#isNotBlank(CharSequence) * @throws IllegalArgumentException 被检查字符串为空白 + * @see StrUtil#isNotBlank(CharSequence) */ public static T notBlank(T text) throws IllegalArgumentException { return notBlank(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); } + /** * 断言给定字符串是否不被另一个字符串包含(即是否为子串) * 并使用指定的函数获取错误信息返回 *
-	 * Assert.doesNotContain(name, "rod", ()->{
+	 * Assert.notContain(name, "rod", ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return ");
 	 *  });
 	 * 
* - * @param textToSearch 被搜索的字符串 - * @param substring 被检查的子串 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 字符串类型 + * @param 异常类型 + * @param textToSearch 被搜索的字符串 + * @param substring 被检查的子串 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 被检查的子串 - * @throws IllegalArgumentException 非子串抛出异常 + * @throws X 非子串抛出异常 + * @see StrUtil#contains(CharSequence, CharSequence) + * @since 5.4.5 */ - public static String notContain(String textToSearch, String substring, Supplier errorMsgSupplier) throws IllegalArgumentException { - if (StrUtil.isNotEmpty(textToSearch) && StrUtil.isNotEmpty(substring) && textToSearch.contains(substring)) { - throw new IllegalArgumentException(errorMsgSupplier.get()); - } - return substring; - } - /** - * 断言给定字符串是否不被另一个字符串包含(即是否为子串) - * - *
-	 * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
-	 * 
- * - * @param textToSearch 被搜索的字符串 - * @param substring 被检查的子串 - * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 - * @return 被检查的子串 - * @throws IllegalArgumentException 非子串抛出异常 - */ - public static String notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (StrUtil.isNotEmpty(textToSearch) && StrUtil.isNotEmpty(substring) && textToSearch.contains(substring)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + public static T notContain(CharSequence textToSearch, T substring, Supplier errorSupplier) throws X { + if (StrUtil.contains(textToSearch, substring)) { + throw errorSupplier.get(); } return substring; } /** * 断言给定字符串是否不被另一个字符串包含(即是否为子串) - * + * *
-	 * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
+	 * Assert.notContain(name, "rod", "Name must not contain 'rod'");
 	 * 
- * + * + * @param textToSearch 被搜索的字符串 + * @param substring 被检查的子串 + * @param errorMsgTemplate 异常时的消息模板 + * @param params 参数列表 + * @return 被检查的子串 + * @throws IllegalArgumentException 非子串抛出异常 + */ + public static String notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notContain(textToSearch, substring, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言给定字符串是否不被另一个字符串包含(即是否为子串) + * + *
+	 * Assert.notContain(name, "rod", "Name must not contain 'rod'");
+	 * 
+ * * @param textToSearch 被搜索的字符串 - * @param substring 被检查的子串 + * @param substring 被检查的子串 * @return 被检查的子串 * @throws IllegalArgumentException 非子串抛出异常 */ public static String notContain(String textToSearch, String substring) throws IllegalArgumentException { return notContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [{}]", substring); } + /** * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 * 并使用指定的函数获取错误信息返回 + * *
 	 * Assert.notEmpty(array, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param array 被检查的数组 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 数组元素类型 + * @param 异常类型 + * @param array 被检查的数组 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 被检查的数组 - * @throws IllegalArgumentException if the object array is {@code null} or has no elements + * @throws X if the object array is {@code null} or has no elements + * @see ArrayUtil#isNotEmpty(Object[]) + * @since 5.4.5 */ - public static Object[] notEmpty(Object[] array, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static T[] notEmpty(T[] array, Supplier errorSupplier) throws X { if (ArrayUtil.isEmpty(array)) { - throw new IllegalArgumentException(errorMsgSupplier.get()); - } - return array; - } - /** - * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 - * - *
-	 * Assert.notEmpty(array, "The array must have elements");
-	 * 
- * - * @param array 被检查的数组 - * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 - * @return 被检查的数组 - * @throws IllegalArgumentException if the object array is {@code null} or has no elements - */ - public static Object[] notEmpty(Object[] array, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (ArrayUtil.isEmpty(array)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + throw errorSupplier.get(); } return array; } /** * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 - * + * *
 	 * Assert.notEmpty(array, "The array must have elements");
 	 * 
- * + * + * @param 数组元素类型 + * @param array 被检查的数组 + * @param errorMsgTemplate 异常时的消息模板 + * @param params 参数列表 + * @return 被检查的数组 + * @throws IllegalArgumentException if the object array is {@code null} or has no elements + */ + public static T[] notEmpty(T[] array, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notEmpty(array, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 + * + *
+	 * Assert.notEmpty(array, "The array must have elements");
+	 * 
+ * + * @param 数组元素类型 * @param array 被检查的数组 * @return 被检查的数组 * @throws IllegalArgumentException if the object array is {@code null} or has no elements */ - public static Object[] notEmpty(Object[] array) throws IllegalArgumentException { + public static T[] notEmpty(T[] array) throws IllegalArgumentException { return notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); } + /** * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 * 并使用指定的函数获取错误信息返回 *
 	 * Assert.noNullElements(array, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return ");
 	 *  });
 	 * 
* - * @param 数组元素类型 - * @param array 被检查的数组 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 数组元素类型 + * @param array 被检查的数组 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 被检查的数组 - * @throws IllegalArgumentException if the object array contains a {@code null} element + * @throws X if the object array contains a {@code null} element + * @see ArrayUtil#hasNull(Object[]) + * @since 5.4.5 */ - public static T[] noNullElements(T[] array, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static T[] noNullElements(T[] array, Supplier errorSupplier) throws X { if (ArrayUtil.hasNull(array)) { - throw new IllegalArgumentException(errorMsgSupplier.get()); - } - return array; - } - /** - * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 - * - *
-	 * Assert.noNullElements(array, "The array must have non-null elements");
-	 * 
- * - * @param 数组元素类型 - * @param array 被检查的数组 - * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 - * @return 被检查的数组 - * @throws IllegalArgumentException if the object array contains a {@code null} element - */ - public static T[] noNullElements(T[] array, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (ArrayUtil.hasNull(array)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + throw errorSupplier.get(); } return array; } /** * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 - * + * + *
+	 * Assert.noNullElements(array, "The array must have non-null elements");
+	 * 
+ * + * @param 数组元素类型 + * @param array 被检查的数组 + * @param errorMsgTemplate 异常时的消息模板 + * @param params 参数列表 + * @return 被检查的数组 + * @throws IllegalArgumentException if the object array contains a {@code null} element + */ + public static T[] noNullElements(T[] array, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return noNullElements(array, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 + * *
 	 * Assert.noNullElements(array);
 	 * 
- * - * @param 数组元素类型 + * + * @param 数组元素类型 * @param array 被检查的数组 * @return 被检查的数组 * @throws IllegalArgumentException if the object array contains a {@code null} element @@ -540,139 +533,146 @@ public class Assert { public static T[] noNullElements(T[] array) throws IllegalArgumentException { return noNullElements(array, "[Assertion failed] - this array must not contain any null elements"); } + /** * 断言给定集合非空 * 并使用指定的函数获取错误信息返回 *
 	 * Assert.notEmpty(collection, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param 集合元素类型 - * @param collection 被检查的集合 - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param 集合元素类型 + * @param 集合类型 + * @param 异常类型 + * @param collection 被检查的集合 + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 非空集合 - * @throws IllegalArgumentException if the collection is {@code null} or has no elements + * @throws X if the collection is {@code null} or has no elements + * @see CollUtil#isNotEmpty(Iterable) + * @since 5.4.5 */ - public static Collection notEmpty(Collection collection, Supplier errorMsgSupplier) throws IllegalArgumentException { + public static , X extends Throwable> T notEmpty(T collection, Supplier errorSupplier) throws X { if (CollUtil.isEmpty(collection)) { - throw new IllegalArgumentException(errorMsgSupplier.get()); - } - return collection; - } - /** - * 断言给定集合非空 - * - *
-	 * Assert.notEmpty(collection, "Collection must have elements");
-	 * 
- * - * @param 集合元素类型 - * @param collection 被检查的集合 - * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 - * @return 非空集合 - * @throws IllegalArgumentException if the collection is {@code null} or has no elements - */ - public static Collection notEmpty(Collection collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (CollUtil.isEmpty(collection)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + throw errorSupplier.get(); } return collection; } /** * 断言给定集合非空 - * + * + *
+	 * Assert.notEmpty(collection, "Collection must have elements");
+	 * 
+ * + * @param 集合元素类型 + * @param 集合类型 + * @param collection 被检查的集合 + * @param errorMsgTemplate 异常时的消息模板 + * @param params 参数列表 + * @return 非空集合 + * @throws IllegalArgumentException if the collection is {@code null} or has no elements + */ + public static > T notEmpty(T collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notEmpty(collection, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言给定集合非空 + * *
 	 * Assert.notEmpty(collection);
 	 * 
- * - * @param 集合元素类型 + * + * @param 集合元素类型 + * @param 集合类型 * @param collection 被检查的集合 * @return 被检查集合 * @throws IllegalArgumentException if the collection is {@code null} or has no elements */ - public static Collection notEmpty(Collection collection) throws IllegalArgumentException { + public static > T notEmpty(T collection) throws IllegalArgumentException { return notEmpty(collection, "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); } + /** * 断言给定Map非空 * 并使用指定的函数获取错误信息返回 *
 	 * Assert.notEmpty(map, ()->{
 	 *      // to query relation message
-	 *      return "relation message to return ";
+	 *      return new IllegalArgumentException("relation message to return");
 	 *  });
 	 * 
* - * @param Key类型 - * @param Value类型 - * - * @param map 被检查的Map - * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 + * @param Key类型 + * @param Value类型 + * @param Map类型 + * @param 异常类型 + * @param map 被检查的Map + * @param errorSupplier 错误抛出异常附带的消息生产接口 * @return 被检查的Map - * @throws IllegalArgumentException if the map is {@code null} or has no entries + * @throws X if the map is {@code null} or has no entries + * @see MapUtil#isNotEmpty(Map) + * @since 5.4.5 */ - public static Map notEmpty(Map map, Supplier errorMsgSupplier) throws IllegalArgumentException { - if (CollUtil.isEmpty(map)) { - throw new IllegalArgumentException(errorMsgSupplier.get()); - } - return map; - } - /** - * 断言给定Map非空 - * - *
-	 * Assert.notEmpty(map, "Map must have entries");
-	 * 
- * - * @param Key类型 - * @param Value类型 - * - * @param map 被检查的Map - * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 - * @return 被检查的Map - * @throws IllegalArgumentException if the map is {@code null} or has no entries - */ - public static Map notEmpty(Map map, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - if (CollUtil.isEmpty(map)) { - throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); + public static , X extends Throwable> T notEmpty(T map, Supplier errorSupplier) throws X { + if (MapUtil.isEmpty(map)) { + throw errorSupplier.get(); } return map; } /** * 断言给定Map非空 - * + * *
 	 * Assert.notEmpty(map, "Map must have entries");
 	 * 
- * + * + * @param Key类型 + * @param Value类型 + * @param Map类型 + * @param map 被检查的Map + * @param errorMsgTemplate 异常时的消息模板 + * @param params 参数列表 + * @return 被检查的Map + * @throws IllegalArgumentException if the map is {@code null} or has no entries + */ + public static > T notEmpty(T map, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + return notEmpty(map, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言给定Map非空 + * + *
+	 * Assert.notEmpty(map, "Map must have entries");
+	 * 
+ * * @param Key类型 * @param Value类型 - * + * @param Map类型 * @param map 被检查的Map * @return 被检查的Map * @throws IllegalArgumentException if the map is {@code null} or has no entries */ - public static Map notEmpty(Map map) throws IllegalArgumentException { + public static > T notEmpty(T map) throws IllegalArgumentException { return notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry"); } /** * 断言给定对象是否是给定类的实例 - * + * *
 	 * Assert.instanceOf(Foo.class, foo);
 	 * 
- * - * @param 被检查对象泛型类型 + * + * @param 被检查对象泛型类型 * @param type 被检查对象匹配的类型 - * @param obj 被检查对象 + * @param obj 被检查对象 * @return 被检查的对象 * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance(Object) @@ -683,16 +683,16 @@ public class Assert { /** * 断言给定对象是否是给定类的实例 - * + * *
 	 * Assert.instanceOf(Foo.class, foo);
 	 * 
- * - * @param 被检查对象泛型类型 - * @param type 被检查对象匹配的类型 - * @param obj 被检查对象 + * + * @param 被检查对象泛型类型 + * @param type 被检查对象匹配的类型 + * @param obj 被检查对象 * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 + * @param params 参数列表 * @return 被检查对象 * @throws IllegalArgumentException if the object is not an instance of clazz * @see Class#isInstance(Object) @@ -707,13 +707,13 @@ public class Assert { /** * 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}. - * + * *
 	 * Assert.isAssignable(Number.class, myClass);
 	 * 
- * + * * @param superType 需要检查的父类或接口 - * @param subType 需要检查的子类 + * @param subType 需要检查的子类 * @throws IllegalArgumentException 如果子类非继承父类,抛出此异常 */ public static void isAssignable(Class superType, Class subType) throws IllegalArgumentException { @@ -722,15 +722,15 @@ public class Assert { /** * 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}. - * + * *
 	 * Assert.isAssignable(Number.class, myClass);
 	 * 
- * - * @param superType 需要检查的父类或接口 - * @param subType 需要检查的子类 + * + * @param superType 需要检查的父类或接口 + * @param subType 需要检查的子类 * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 + * @param params 参数列表 * @throws IllegalArgumentException 如果子类非继承父类,抛出此异常 */ public static void isAssignable(Class superType, Class subType, String errorMsgTemplate, Object... params) throws IllegalArgumentException { @@ -739,6 +739,7 @@ public class Assert { throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); } } + /** * 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。 * 并使用指定的函数获取错误信息返回 @@ -749,7 +750,7 @@ public class Assert { * }); * * - * @param expression boolean 表达式 + * @param expression boolean 表达式 * @param errorMsgSupplier 错误抛出异常附带的消息生产接口 * @throws IllegalStateException 表达式为 {@code false} 抛出此异常 */ @@ -758,16 +759,17 @@ public class Assert { throw new IllegalStateException(errorMsgSupplier.get()); } } + /** * 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。 - * + * *
 	 * Assert.state(id == null, "The id property must not already be initialized");
 	 * 
- * - * @param expression boolean 表达式 + * + * @param expression boolean 表达式 * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 + * @param params 参数列表 * @throws IllegalStateException 表达式为 {@code false} 抛出此异常 */ public static void state(boolean expression, String errorMsgTemplate, Object... params) throws IllegalStateException { @@ -778,11 +780,11 @@ public class Assert { /** * 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。 - * + * *
 	 * Assert.state(id == null);
 	 * 
- * + * * @param expression boolean 表达式 * @throws IllegalStateException 表达式为 {@code false} 抛出此异常 */ @@ -792,15 +794,15 @@ public class Assert { /** * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足: - * + * *
 	 * 0 ≤ index < size
 	 * 
- * + * * @param index 下标 - * @param size 长度 + * @param size 长度 * @return 检查后的下标 - * @throws IllegalArgumentException 如果size < 0 抛出此异常 + * @throws IllegalArgumentException 如果size < 0 抛出此异常 * @throws IndexOutOfBoundsException 如果index < 0或者 index ≥ size 抛出此异常 * @since 4.1.9 */ @@ -810,17 +812,17 @@ public class Assert { /** * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足: - * + * *
 	 * 0 ≤ index < size
 	 * 
- * - * @param index 下标 - * @param size 长度 + * + * @param index 下标 + * @param size 长度 * @param errorMsgTemplate 异常时的消息模板 - * @param params 参数列表 + * @param params 参数列表 * @return 检查后的下标 - * @throws IllegalArgumentException 如果size < 0 抛出此异常 + * @throws IllegalArgumentException 如果size < 0 抛出此异常 * @throws IndexOutOfBoundsException 如果index < 0或者 index ≥ size 抛出此异常 * @since 4.1.9 */ @@ -833,10 +835,10 @@ public class Assert { /** * 检查值是否在指定范围内 - * + * * @param value 值 - * @param min 最小值(包含) - * @param max 最大值(包含) + * @param min 最小值(包含) + * @param max 最大值(包含) * @return 检查后的长度值 * @since 4.1.10 */ @@ -846,13 +848,13 @@ public class Assert { } return value; } - + /** * 检查值是否在指定范围内 - * + * * @param value 值 - * @param min 最小值(包含) - * @param max 最大值(包含) + * @param min 最小值(包含) + * @param max 最大值(包含) * @return 检查后的长度值 * @since 4.1.10 */ @@ -862,13 +864,13 @@ public class Assert { } return value; } - + /** * 检查值是否在指定范围内 - * + * * @param value 值 - * @param min 最小值(包含) - * @param max 最大值(包含) + * @param min 最小值(包含) + * @param max 最大值(包含) * @return 检查后的长度值 * @since 4.1.10 */ @@ -878,13 +880,13 @@ public class Assert { } return value; } - + /** * 检查值是否在指定范围内 - * + * * @param value 值 - * @param min 最小值(包含) - * @param max 最大值(包含) + * @param min 最小值(包含) + * @param max 最大值(包含) * @return 检查后的长度值 * @since 4.1.10 */ @@ -902,12 +904,13 @@ public class Assert { } // -------------------------------------------------------------------------------------------------------------------------------------------- Private method start + /** * 错误的下标时显示的消息 - * - * @param index 下标 - * @param size 长度 - * @param desc 异常时的消息模板 + * + * @param index 下标 + * @param size 长度 + * @param desc 异常时的消息模板 * @param params 参数列表 * @return 消息 */ diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java index 492b82532..2fc2b0532 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java @@ -18,12 +18,21 @@ public class AssertTest { @Test(expected = IllegalArgumentException.class) public void isTrueTest() { int i = 0; + //noinspection ConstantConditions cn.hutool.core.lang.Assert.isTrue(i > 0, IllegalArgumentException::new); } @Test(expected = IndexOutOfBoundsException.class) public void isTrueTest2() { int i = -1; + //noinspection ConstantConditions cn.hutool.core.lang.Assert.isTrue(i >= 0, IndexOutOfBoundsException::new); } + + @Test(expected = IndexOutOfBoundsException.class) + public void isTrueTest3() { + int i = -1; + //noinspection ConstantConditions + Assert.isTrue(i > 0, ()-> new IndexOutOfBoundsException("relation message to return")); + } } diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONNull.java b/hutool-json/src/main/java/cn/hutool/json/JSONNull.java index 7efe4ace8..729fba3a2 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONNull.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONNull.java @@ -1,5 +1,7 @@ package cn.hutool.json; +import cn.hutool.core.util.StrUtil; + import java.io.Serializable; /** @@ -39,6 +41,6 @@ public class JSONNull implements Serializable{ */ @Override public String toString() { - return "null"; + return StrUtil.NULL; } } \ No newline at end of file diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelReadTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelReadTest.java index 85742679c..95c6d2177 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelReadTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelReadTest.java @@ -214,4 +214,10 @@ public class ExcelReadTest { reader.read((cell, value)-> Console.log("{}, {} {}", cell.getRowIndex(), cell.getColumnIndex(), value)); } + @Test + public void readTest() { + final ExcelReader reader = ExcelUtil.getReader("d:/test/人员体检信息表.xlsx"); + final List> read = reader.read(); +// Console.log(read); + } }