public class Assert extends Object
| Constructor and Description |
|---|
Assert() |
| Modifier and Type | Method and Description |
|---|---|
static void |
hasText(String text,
String message)
Assert that the given String contains valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static void |
isAssignable(Class<?> superType,
Class<?> subType)
Assert that
superType.isAssignableFrom(subType) is true. |
static void |
isAssignable(Class<?> superType,
Class<?> subType,
String errorMsgTemplate,
Object... params)
Assert that
superType.isAssignableFrom(subType) is true. |
static void |
isFalse(boolean expression)
断言是否为假,如果为
true 抛出 IllegalArgumentException 异常Assert.isFalse(i < 0); |
static void |
isFalse(boolean expression,
String errorMsgTemplate,
Object... params)
断言是否为假,如果为
true 抛出 IllegalArgumentException 异常Assert.isFalse(i < 0, "The value must be greater than zero"); |
static <T> T |
isInstanceOf(Class<?> type,
T obj)
断言给定对象是否是给定类的实例
Assert.instanceOf(Foo.class, foo); |
static <T> T |
isInstanceOf(Class<?> type,
T obj,
String errorMsgTemplate,
Object... params)
断言给定对象是否是给定类的实例
Assert.instanceOf(Foo.class, foo); |
static void |
isNull(Object object)
|
static void |
isNull(Object object,
String errorMsgTemplate,
Object... params)
|
static void |
isTrue(boolean expression)
断言是否为真,如果为
false 抛出 IllegalArgumentException 异常Assert.isTrue(i > 0, "The value must be greater than zero"); |
static void |
isTrue(boolean expression,
String errorMsgTemplate,
Object... params)
断言是否为真,如果为
false 抛出 IllegalArgumentException 异常Assert.isTrue(i > 0, "The value must be greater than zero"); |
static <T> T[] |
noNullElements(T[] array)
断言给定数组是否不包含
null元素,如果数组为空或 null将被认为不包含
Assert.noNullElements(array); |
static <T> T[] |
noNullElements(T[] array,
String errorMsgTemplate,
Object... params)
断言给定数组是否不包含
null元素,如果数组为空或 null将被认为不包含
Assert.noNullElements(array, "The array must have non-null elements"); |
static String |
notBlank(String text)
检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出
IllegalArgumentException
Assert.notBlank(name, "Name must not be blank"); |
static String |
notBlank(String text,
String errorMsgTemplate,
Object... params)
检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出
IllegalArgumentException
Assert.notBlank(name, "Name must not be blank"); |
static String |
notContain(String textToSearch,
String substring)
断言给定字符串是否不被另一个字符串包含(既是否为子串)
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'"); |
static String |
notContain(String textToSearch,
String substring,
String errorMsgTemplate,
Object... params)
断言给定字符串是否不被另一个字符串包含(既是否为子串)
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'"); |
static <T> Collection<T> |
notEmpty(Collection<T> collection)
断言给定集合非空
Assert.notEmpty(collection); |
static <T> Collection<T> |
notEmpty(Collection<T> collection,
String errorMsgTemplate,
Object... params)
断言给定集合非空
Assert.notEmpty(collection, "Collection must have elements"); |
static <K,V> Map<K,V> |
notEmpty(Map<K,V> map)
断言给定Map非空
Assert.notEmpty(map, "Map must have entries"); |
static <K,V> Map<K,V> |
notEmpty(Map<K,V> map,
String errorMsgTemplate,
Object... params)
断言给定Map非空
Assert.notEmpty(map, "Map must have entries"); |
static Object[] |
notEmpty(Object[] array)
断言给定数组是否包含元素,数组必须不为
null 且至少包含一个元素
Assert.notEmpty(array, "The array must have elements"); |
static Object[] |
notEmpty(Object[] array,
String errorMsgTemplate,
Object... params)
断言给定数组是否包含元素,数组必须不为
null 且至少包含一个元素
Assert.notEmpty(array, "The array must have elements"); |
static String |
notEmpty(String text)
检查给定字符串是否为空,为空抛出
IllegalArgumentException
Assert.notEmpty(name); |
static String |
notEmpty(String text,
String errorMsgTemplate,
Object... params)
检查给定字符串是否为空,为空抛出
IllegalArgumentException
Assert.notEmpty(name, "Name must not be empty"); |
static <T> T |
notNull(T object)
|
static <T> T |
notNull(T object,
String errorMsgTemplate,
Object... params)
|
static void |
state(boolean expression)
Assert a boolean expression, throwing
IllegalStateException if the test result is false. |
static void |
state(boolean expression,
String errorMsgTemplate,
Object... params)
Assert a boolean expression, throwing
IllegalStateException if the test result is false. |
public static void isTrue(boolean expression,
String errorMsgTemplate,
Object... params)
throws IllegalArgumentException
false 抛出 IllegalArgumentException 异常Assert.isTrue(i > 0, "The value must be greater than zero");
expression - 波尔值errorMsgTemplate - 错误抛出异常附带的消息模板,变量用{}代替params - 参数列表IllegalArgumentException - if expression is falsepublic static void isTrue(boolean expression)
throws IllegalArgumentException
false 抛出 IllegalArgumentException 异常Assert.isTrue(i > 0, "The value must be greater than zero");
expression - 波尔值IllegalArgumentException - if expression is falsepublic static void isFalse(boolean expression,
String errorMsgTemplate,
Object... params)
throws IllegalArgumentException
true 抛出 IllegalArgumentException 异常Assert.isFalse(i < 0, "The value must be greater than zero");
expression - 波尔值errorMsgTemplate - 错误抛出异常附带的消息模板,变量用{}代替params - 参数列表IllegalArgumentException - if expression is falsepublic static void isFalse(boolean expression)
throws IllegalArgumentException
true 抛出 IllegalArgumentException 异常Assert.isFalse(i < 0);
expression - 波尔值IllegalArgumentException - if expression is falsepublic static void isNull(Object object, String errorMsgTemplate, Object... params) throws IllegalArgumentException
object - 被检查的对象errorMsgTemplate - 消息模板,变量使用{}表示params - 参数列表IllegalArgumentException - if the object is not nullpublic static void isNull(Object object) throws NullPointerException
object - 被检查对象NullPointerException - if the object is not nullpublic static <T> T notNull(T object,
String errorMsgTemplate,
Object... params)
throws NullPointerException
null ,如果为null 抛出IllegalArgumentException 异常 Assert that an object is not null .
Assert.notNull(clazz, "The class must not be null");
T - 被检查对象泛型类型object - 被检查对象errorMsgTemplate - 错误消息模板,变量使用{}表示params - 参数NullPointerException - if the object is nullpublic static <T> T notNull(T object)
throws NullPointerException
T - 被检查对象类型object - 被检查对象NullPointerException - if the object is nullpublic static String notEmpty(String text, String errorMsgTemplate, Object... params) throws IllegalArgumentException
IllegalArgumentException
Assert.notEmpty(name, "Name must not be empty");
text - 被检查字符串errorMsgTemplate - 错误消息模板,变量使用{}表示params - 参数IllegalArgumentException - 被检查字符串为空public static String notEmpty(String text) throws IllegalArgumentException
IllegalArgumentException
Assert.notEmpty(name);
text - 被检查字符串IllegalArgumentException - 被检查字符串为空public static String notBlank(String text, String errorMsgTemplate, Object... params) throws IllegalArgumentException
IllegalArgumentException
Assert.notBlank(name, "Name must not be blank");
text - 被检查字符串errorMsgTemplate - 错误消息模板,变量使用{}表示params - 参数IllegalArgumentException - 被检查字符串为空白StringUtils.isNotBlank(CharSequence)public static String notBlank(String text) throws IllegalArgumentException
IllegalArgumentException
Assert.notBlank(name, "Name must not be blank");
text - 被检查字符串IllegalArgumentException - 被检查字符串为空白StringUtils.isNotBlank(CharSequence)public static String notContain(String textToSearch, String substring, String errorMsgTemplate, Object... params) throws IllegalArgumentException
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
textToSearch - 被搜索的字符串substring - 被检查的子串errorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - 非子串抛出异常public static String notContain(String textToSearch, String substring) throws IllegalArgumentException
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
textToSearch - 被搜索的字符串substring - 被检查的子串IllegalArgumentException - 非子串抛出异常public static Object[] notEmpty(Object[] array, String errorMsgTemplate, Object... params) throws IllegalArgumentException
null 且至少包含一个元素
Assert.notEmpty(array, "The array must have elements");
array - 被检查的数组errorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the object array is null or has no elementspublic static Object[] notEmpty(Object[] array) throws IllegalArgumentException
null 且至少包含一个元素
Assert.notEmpty(array, "The array must have elements");
array - 被检查的数组IllegalArgumentException - if the object array is null or has no elementspublic static <T> T[] noNullElements(T[] array,
String errorMsgTemplate,
Object... params)
throws IllegalArgumentException
null元素,如果数组为空或 null将被认为不包含
Assert.noNullElements(array, "The array must have non-null elements");
T - 数组元素类型array - 被检查的数组errorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the object array contains a null elementpublic static <T> T[] noNullElements(T[] array)
throws IllegalArgumentException
null元素,如果数组为空或 null将被认为不包含
Assert.noNullElements(array);
T - 数组元素类型array - 被检查的数组IllegalArgumentException - if the object array contains a null elementpublic static <T> Collection<T> notEmpty(Collection<T> collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException
Assert.notEmpty(collection, "Collection must have elements");
T - 集合元素类型collection - 被检查的集合errorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the collection is null or has no elementspublic static <T> Collection<T> notEmpty(Collection<T> collection) throws IllegalArgumentException
Assert.notEmpty(collection);
T - 集合元素类型collection - 被检查的集合IllegalArgumentException - if the collection is null or has no elementspublic static <K,V> Map<K,V> notEmpty(Map<K,V> map, String errorMsgTemplate, Object... params) throws IllegalArgumentException
Assert.notEmpty(map, "Map must have entries");
K - Key类型V - Value类型map - 被检查的MaperrorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the map is null or has no entriespublic static <K,V> Map<K,V> notEmpty(Map<K,V> map) throws IllegalArgumentException
Assert.notEmpty(map, "Map must have entries");
K - Key类型V - Value类型map - 被检查的MapIllegalArgumentException - if the map is null or has no entriespublic static <T> T isInstanceOf(Class<?> type, T obj)
Assert.instanceOf(Foo.class, foo);
T - 被检查对象泛型类型type - 被检查对象匹配的类型obj - 被检查对象IllegalArgumentException - if the object is not an instance of clazzClass.isInstance(Object)public static <T> T isInstanceOf(Class<?> type, T obj, String errorMsgTemplate, Object... params) throws IllegalArgumentException
Assert.instanceOf(Foo.class, foo);
T - 被检查对象泛型类型type - 被检查对象匹配的类型obj - 被检查对象errorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the object is not an instance of clazzClass.isInstance(Object)public static void isAssignable(Class<?> superType, Class<?> subType) throws IllegalArgumentException
superType.isAssignableFrom(subType) is true.
Assert.isAssignable(Number.class, myClass);
superType - the super type to checksubType - the sub type to checkIllegalArgumentException - if the classes are not assignablepublic static void isAssignable(Class<?> superType, Class<?> subType, String errorMsgTemplate, Object... params) throws IllegalArgumentException
superType.isAssignableFrom(subType) is true.
Assert.isAssignable(Number.class, myClass);
superType - the super type to check againstsubType - the sub type to checkerrorMsgTemplate - 异常时的消息模板params - 参数列表IllegalArgumentException - if the classes are not assignablepublic static void state(boolean expression,
String errorMsgTemplate,
Object... params)
throws IllegalStateException
IllegalStateException if the test result is false. Call isTrue if you wish to throw IllegalArgumentException on an assertion failure.
Assert.state(id == null, "The id property must not already be initialized");
expression - a boolean expressionerrorMsgTemplate - 异常时的消息模板params - 参数列表IllegalStateException - if expression is falsepublic static void state(boolean expression)
throws IllegalStateException
IllegalStateException if the test result is false.
Call isTrue(boolean) if you wish to throw IllegalArgumentException on an assertion failure.
Assert.state(id == null);
expression - a boolean expressionIllegalStateException - if the supplied expression is falsepublic static void hasText(String text, String message)
null and must contain at least one non-whitespace character.
Assert.hasText(name, "'name' must not be empty");
text - the String to checkmessage - the exception message to use if the assertion failsIllegalArgumentException - if the text does not contain valid text contentStringUtils.hasText(java.lang.String)Copyright © 2019. All rights reserved.