@@ -1,11 +1,10 @@
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 ;
@@ -14,28 +13,9 @@ import java.util.function.Supplier;
* 断言某些对象或值是否符合规定,否则抛出异常。经常用于做变量检查
*
* @author Looly
*
*/
public class Assert {
/**
* 断言是否为真,如果为 {@code false} 抛出异常<br>
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.isTrue(i > 0, ()->{
* // to query relation message
* return "relation message to return ";
* });
* </pre>
*
* @param expression 布尔值
* @param errorMsgSupplier 错误抛出异常附带的消息生产接口
* @throws IllegalArgumentException if expression is {@code false}
*/
public static void isTrue ( boolean expression , Supplier < String > errorMsgSupplier ) throws IllegalArgumentException {
if ( ! expression ) {
isTrue ( false , errorMsgSupplier . get ( ) ) ;
}
}
/**
* 断言是否为真,如果为 {@code false} 抛出给定的异常<br>
*
@@ -48,9 +28,9 @@ public class Assert {
* @param supplier 指定断言不通过时抛出的异常
* @throws X if expression is {@code false}
*/
public static < X extends Throwable > void isTrue ( boolean expression , Func0 < ? extends X > supplier ) throws X {
public static < X extends Throwable > void isTrue ( boolean expression , Supplier < ? extends X > supplier ) throws X {
if ( false = = expression ) {
throw supplier . callWithRuntimeException ( ) ;
throw supplier . get ( ) ;
}
}
@@ -67,9 +47,7 @@ public class Assert {
* @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 ) ) ) ;
}
/**
@@ -85,25 +63,29 @@ public class Assert {
public static void isTrue ( boolean expression ) throws IllegalArgumentException {
isTrue ( expression , " [Assertion failed] - this expression must be true " ) ;
}
/**
* 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常<br>
* 断言是否为假,如果为 {@code true} 抛出指定类型 异常<br>
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.isFalse(i > 0, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <X> 异常类型
* @param expression 布尔值
* @param errorMsg Supplier 错误 抛出异常附带的消息生产接口
* @throws IllegalArgumentException if expression is {@code false}
* @param errorSupplier 指定断言不通过时 抛出的 异常
* @throws X if expression is {@code false}
* @since 5.4.5
*/
public static void isFalse ( boolean expression , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
public static < X extends Throwable > void isFalse ( boolean expression , Supplier < X > errorSupplier ) throws X {
if ( expression ) {
isFalse ( true , errorMsg Supplier . get ( ) ) ;
throw errorSupplier . get ( ) ;
}
}
/**
* 断言是否为假,如果为 {@code true} 抛出 {@code IllegalArgumentException} 异常<br>
*
@@ -117,9 +99,7 @@ public class Assert {
* @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 ) ) ;
}
isFalse ( expression , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
@@ -135,25 +115,29 @@ public class Assert {
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} 抛出指定类型 异常
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.isNull(value, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <X> 异常类型
* @param object 被检查的对象
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @throws IllegalArgumentException if the object is not {@code null}
* @param errorSupplier 错误抛出异常附带的消息生产接口
* @throws X if the object is not {@code null}
* @since 5.4.5
*/
public static void isNull ( Object object , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
if ( object ! = null ) {
isNull ( object , errorMsg Supplier . get ( ) ) ;
public static < X extends Throwable > void isNull ( Object object , Supplier < X > errorSupplier ) throws X {
if ( null ! = object ) {
throw errorSupplier . get ( ) ;
}
}
/**
* 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常
*
@@ -167,9 +151,7 @@ public class Assert {
* @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 ) ) ;
}
isNull ( object , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
@@ -187,28 +169,32 @@ public class Assert {
}
// ----------------------------------------------------------------------------------------------------------- Check not null
/**
* 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} .
* 断言对象是否不为{@code null} ,如果为{@code null} 抛出指定类型异常
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.notNull(clazz, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <T> 被检查对象泛型类型
* @param <X> 异常类型
* @param object 被检查对象
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 > T notNull ( T object , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
if ( object = = null ) {
notNull ( null , errorMsg Supplier . get ( ) ) ;
public static < T , X extends Throwable > T notNull ( T object , Supplier < X > 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} .
*
@@ -224,10 +210,7 @@ public class Assert {
* @throws IllegalArgumentException if the object is {@code null}
*/
public static < T > T notNull ( T object , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
if ( object = = null ) {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
}
return object ;
return notNull ( object , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
@@ -247,29 +230,32 @@ public class Assert {
}
// ----------------------------------------------------------------------------------------------------------- Check empty
/**
* 检查给定字符串是否为空,为空抛出 {@link IllegalArgumentException}
* 并使用指定的函数获取错误信息返回
* 检查给定字符串是否为空,为空抛出自定义异常,并使用指定的函数获取错误信息返回。
* <pre class="code">
* Assert.notEmpty(name, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <X> 异常类型
* @param <T> 字符串类型
* @param text 被检查字符串
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @param errorSupplier 错误抛出异常附带的消息生产接口
* @return 非空字符串
* @throws X 被检查字符串为空抛出此异常
* @see StrUtil#isNotEmpty(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空
* @since 5.4.5
*/
public static < T extends CharSequence > T notEmpty ( T text , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
public static < T extends CharSequence , X extends Throwable > T notEmpty ( T text , Supplier < X > errorSupplier ) throws X {
if ( StrUtil . isEmpty ( text ) ) {
notEmpty ( text , errorMsg Supplier . get ( ) ) ;
throw errorSupplier . get ( ) ;
}
return text ;
}
/**
* 检查给定字符串是否为空,为空抛出 {@link IllegalArgumentException}
*
@@ -282,14 +268,11 @@ public class Assert {
* @param errorMsgTemplate 错误消息模板,变量使用{}表示
* @param params 参数
* @return 非空字符串
* @see StrUtil#isNotEmpty(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空
* @see StrUtil#isNotEmpty(CharSequence)
*/
public static < T extends CharSequence > 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 ) ) ) ;
}
/**
@@ -302,35 +285,38 @@ public class Assert {
* @param <T> 字符串类型
* @param text 被检查字符串
* @return 被检查的字符串
* @see StrUtil#isNotEmpty(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空
* @see StrUtil#isNotEmpty(CharSequence)
*/
public static < T extends CharSequence > 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、空串或只包含空白符) , 为空抛出自定义异常。
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.notBlank(name, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <X> 异常类型
* @param <T> 字符串类型
* @param text 被检查字符串
* @param errorMsgSupplier 错误抛出异常附带的消息生产接口
* @return 非空字符串
* @throws X 被检查字符串为空白
* @see StrUtil#isNotBlank(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空白
*/
public static < T extends CharSequence > T notBlank ( T text , Supplier < String > errorMsgSupplier ) throws IllegalArgumentException {
public static < T extends CharSequence , X extends Throwable > T notBlank ( T text , Supplier < X > errorMsgSupplier ) throws X {
if ( StrUtil . isBlank ( text ) ) {
notBlank ( text , errorMsgSupplier . get ( ) ) ;
throw errorMsgSupplier . get ( ) ;
}
return text ;
}
/**
* 检查给定字符串是否为空白( null、空串或只包含空白符) , 为空抛出 {@link IllegalArgumentException}
*
@@ -343,14 +329,11 @@ public class Assert {
* @param errorMsgTemplate 错误消息模板,变量使用{}表示
* @param params 参数
* @return 非空字符串
* @see StrUtil#isNotBlank(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空白
* @see StrUtil#isNotBlank(CharSequence)
*/
public static < T extends CharSequence > T notBlank ( T text , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
if ( StrUtil . isBlank ( text ) ) {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
}
return text ;
return notBlank ( text , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
@@ -363,39 +346,45 @@ public class Assert {
* @param <T> 字符串类型
* @param text 被检查字符串
* @return 非空字符串
* @see StrUtil#isNotBlank(CharSequence)
* @throws IllegalArgumentException 被检查字符串为空白
* @see StrUtil#isNotBlank(CharSequence)
*/
public static < T extends CharSequence > 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 " ) ;
}
/**
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.doesN otContain(name, "rod", ()->{
* Assert.n otContain(name, "rod", ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return ") ;
* });
* </pre>
*
* @param <T> 字符串类型
* @param <X> 异常类型
* @param textToSearch 被搜索的字符串
* @param substring 被检查的子串
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 < String > errorMsg Supplier ) throws IllegalArgumentException {
if ( StrUtil . isNotEmpty ( textToSearch ) & & StrUtil . isNotEmpty ( substring ) & & textToSearch . contains( substring ) ) {
throw new IllegalArgumentException ( errorMsg Supplier . get ( ) ) ;
public static < T extends CharSequence , X extends Throwable > T notContain ( CharSequence textToSearch , T substring , Supplier < X > errorSupplier ) throws X {
if ( StrUtil . contains (textToSearch , substring ) ) {
throw errorSupplier . get ( ) ;
}
return substring ;
}
/**
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)
*
* <pre class="code">
* Assert.doesN otContain(name, "rod", "Name must not contain 'rod'");
* Assert.n otContain(name, "rod", "Name must not contain 'rod'");
* </pre>
*
* @param textToSearch 被搜索的字符串
@@ -406,17 +395,14 @@ public class Assert {
* @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 ) ) ;
}
return substring ;
return notContain ( textToSearch , substring , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)
*
* <pre class="code">
* Assert.doesN otContain(name, "rod", "Name must not contain 'rod'");
* Assert.n otContain(name, "rod", "Name must not contain 'rod'");
* </pre>
*
* @param textToSearch 被搜索的字符串
@@ -427,43 +413,30 @@ public class Assert {
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} 且至少包含一个元素
* 并使用指定的函数获取错误信息返回
*
* <pre class="code">
* Assert.notEmpty(array, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <T> 数组元素类型
* @param <X> 异常类型
* @param array 被检查的数组
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 < String > errorMsg Supplier ) throws IllegalArgumentException {
public static < T , X extends Throwable > T [ ] notEmpty ( T [ ] array , Supplier < X > errorSupplier ) throws X {
if ( ArrayUtil . isEmpty ( array ) ) {
throw new IllegalArgumentException ( errorMsg Supplier . get ( ) ) ;
}
return array ;
}
/**
* 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素
*
* <pre class="code">
* Assert.notEmpty(array, "The array must have elements");
* </pre>
*
* @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 ;
}
@@ -475,35 +448,58 @@ public class Assert {
* Assert.notEmpty(array, "The array must have elements");
* </pre>
*
* @param <T> 数组元素类型
* @param array 被检查的数组
* @param errorMsgTemplate 异常时的消息模板
* @param params 参数列表
* @return 被检查的数组
* @throws IllegalArgumentException if the object array is {@code null} or has no elements
*/
public static < T > T [ ] notEmpty ( T [ ] array , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
return notEmpty ( array , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
* 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素
*
* <pre class="code">
* Assert.notEmpty(array, "The array must have elements");
* </pre>
*
* @param <T> 数组元素类型
* @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 > 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}将被认为不包含
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.noNullElements(array, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return ") ;
* });
* </pre>
*
* @param <T> 数组元素类型
* @param array 被检查的数组
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 > T [ ] noNullElements ( T [ ] array , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
public static < T , X extends Throwable > T [ ] noNullElements ( T [ ] array , Supplier < X > errorSupplier ) throws X {
if ( ArrayUtil . hasNull ( array ) ) {
throw new IllegalArgumentException ( errorMsg Supplier . get ( ) ) ;
throw errorSupplier . get ( ) ;
}
return array ;
}
/**
* 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含
*
@@ -519,10 +515,7 @@ public class Assert {
* @throws IllegalArgumentException if the object array contains a {@code null} element
*/
public static < T > T [ ] noNullElements ( T [ ] array , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
if ( A rrayUtil . hasNull ( array ) ) {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
}
return array ;
return noNullElements ( a rray, ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , p arams ) ) ) ;
}
/**
@@ -540,28 +533,34 @@ public class Assert {
public static < T > T [ ] noNullElements ( T [ ] array ) throws IllegalArgumentException {
return noNullElements ( array , " [Assertion failed] - this array must not contain any null elements " ) ;
}
/**
* 断言给定集合非空
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.notEmpty(collection, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <T> 集合元素类型
* @param <E> 集合元素类型
* @param <T> 集合类型
* @param <X> 异常类型
* @param collection 被检查的集合
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 < T > Collection < T > notEmpty ( Collection < T > collection , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
public static < E , T extends Iterable < E > , X extends Throwable > T notEmpty ( T collection , Supplier < X > errorSupplier ) throws X {
if ( CollUtil . isEmpty ( collection ) ) {
throw new IllegalArgumentException ( errorMsg Supplier . get ( ) ) ;
throw errorSupplier . get ( ) ;
}
return collection ;
}
/**
* 断言给定集合非空
*
@@ -569,18 +568,16 @@ public class Assert {
* Assert.notEmpty(collection, "Collection must have elements");
* </pre>
*
* @param <T> 集合元素类型
* @param <E> 集合元素类型
* @param <T> 集合类型
* @param collection 被检查的集合
* @param errorMsgTemplate 异常时的消息模板
* @param params 参数列表
* @return 非空集合
* @throws IllegalArgumentException if the collection is {@code null} or has no elements
*/
public static < T > Collection < T > notEmpty ( Collection < T > collection , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
if ( CollUtil . isEmpty ( collection ) ) {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
}
return collection ;
public static < E , T extends Iterable < E > > T notEmpty ( T collection , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
return notEmpty ( collection , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
@@ -590,57 +587,40 @@ public class Assert {
* Assert.notEmpty(collection);
* </pre>
*
* @param <T> 集合元素类型
* @param <E> 集合元素类型
* @param <T> 集合类型
* @param collection 被检查的集合
* @return 被检查集合
* @throws IllegalArgumentException if the collection is {@code null} or has no elements
*/
public static < T > Collection < T > notEmpty ( Collection < T > collection ) throws IllegalArgumentException {
public static < E , T extends Iterable < E > > 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非空
* 并使用指定的函数获取错误信息返回
* <pre class="code">
* Assert.notEmpty(map, ()->{
* // to query relation message
* return "relation message to return ";
* return new IllegalArgumentException( "relation message to return") ;
* });
* </pre>
*
* @param <K> Key类型
* @param <V> Value类型
*
* @param <T> Map类型
* @param <X> 异常类型
* @param map 被检查的Map
* @param errorMsg Supplier 错误抛出异常附带的消息生产接口
* @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 < K , V > Map < K , V > notEmpty ( Map < K , V > map , Supplier < String > errorMsg Supplier ) throws IllegalArgumentException {
if ( Coll Util. isEmpty ( map ) ) {
throw new IllegalArgumentException ( errorMsg Supplier . get ( ) ) ;
}
return map ;
}
/**
* 断言给定Map非空
*
* <pre class="code">
* Assert.notEmpty(map, "Map must have entries");
* </pre>
*
* @param <K> Key类型
* @param <V> Value类型
*
* @param map 被检查的Map
* @param errorMsgTemplate 异常时的消息模板
* @param params 参数列表
* @return 被检查的Map
* @throws IllegalArgumentException if the map is {@code null} or has no entries
*/
public static < K , V > Map < K , V > notEmpty ( Map < K , V > map , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
if ( CollUtil . isEmpty ( map ) ) {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
public static < K , V , T extends Map < K , V > , X extends Throwable > T notEmpty ( T map , Supplier < X > errorSupplier ) throws X {
if ( Map Util. isEmpty ( map ) ) {
throw errorSupplier . get ( ) ;
}
return map ;
}
@@ -654,12 +634,32 @@ public class Assert {
*
* @param <K> Key类型
* @param <V> Value类型
* @param <T> Map类型
* @param map 被检查的Map
* @param errorMsgTemplate 异常时的消息模板
* @param params 参数列表
* @return 被检查的Map
* @throws IllegalArgumentException if the map is {@code null} or has no entries
*/
public static < K , V , T extends Map < K , V > > T notEmpty ( T map , String errorMsgTemplate , Object . . . params ) throws IllegalArgumentException {
return notEmpty ( map , ( ) - > new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ) ;
}
/**
* 断言给定Map非空
*
* <pre class="code">
* Assert.notEmpty(map, "Map must have entries");
* </pre>
*
* @param <K> Key类型
* @param <V> Value类型
* @param <T> Map类型
* @param map 被检查的Map
* @return 被检查的Map
* @throws IllegalArgumentException if the map is {@code null} or has no entries
*/
public static < K , V > Map < K , V > notEmpty ( Map < K , V > map ) throws IllegalArgumentException {
public static < K , V , T extends Map < K , V > > T notEmpty ( T map ) throws IllegalArgumentException {
return notEmpty ( map , " [Assertion failed] - this map must not be empty; it must contain at least one entry " ) ;
}
@@ -739,6 +739,7 @@ public class Assert {
throw new IllegalArgumentException ( StrUtil . format ( errorMsgTemplate , params ) ) ;
}
}
/**
* 检查boolean表达式, 当检查结果为false时抛出 {@code IllegalStateException}。
* 并使用指定的函数获取错误信息返回
@@ -758,6 +759,7 @@ public class Assert {
throw new IllegalStateException ( errorMsgSupplier . get ( ) ) ;
}
}
/**
* 检查boolean表达式, 当检查结果为false时抛出 {@code IllegalStateException}。
*
@@ -902,6 +904,7 @@ public class Assert {
}
// -------------------------------------------------------------------------------------------------------------------------------------------- Private method start
/**
* 错误的下标时显示的消息
*