import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.util.Collection; import java.util.Map;
这些方法用于对表达式、对象、集合和字符串进行断言,如果不满足条件,则抛出 IllegalArgumentException
异常
public class Assert {
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}
public static void isTrue(boolean expression) {
isTrue(expression, "[Assertion failed] - this expression must be true");
}
public static void isNull(Object object, String message) {
if (object != null) {
throw new IllegalArgumentException(message);
}
}
public static void isNull(Object object) {
isNull(object, "[Assertion failed] - the object argument must be null");
}
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
public static void notNull(Object object) {
notNull(object, "[Assertion failed] - this argument is required; it must not be null");
}
public static void notEmpty(Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Collection<?> collection) {
notEmpty(collection,
"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
}
public static void notEmpty(Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Map<?, ?> map) {
notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
}
public static void notEmpty(String str, String message) {
if (StringUtils.isEmpty(str)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(String str) {
if (StringUtils.isEmpty(str)) {
notEmpty(str, "[Assertion failed] - this string must not be empty");
}
}
public static void notBlank(String str, String message) {
if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
throw new IllegalArgumentException(message);
}
}
public static void notBlank(String str) {
notBlank(str, "[Assertion failed] - this string must not be blank");
}
public static void hasText(String text, String message) {
if (!StringUtils.hasText(text)) {
throw new IllegalArgumentException(message);
}
}
public static void hasText(String text) {
hasText(text,
"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}
}
以下是该类中定义的一些断言方法及其作用:
isTrue(boolean expression, String message)
:如果表达式为 false,则抛出带有指定消息的异常。isTrue(boolean expression)
:如果表达式为 false,则抛出默认消息的异常。isNull(Object object, String message)
:如果对象不为空,则抛出带有指定消息的异常。isNull(Object object)
:如果对象不为空,则抛出默认消息的异常。notNull(Object object, String message)
:如果对象为空,则抛出带有指定消息的异常。notNull(Object object)
:如果对象为空,则抛出默认消息的异常。notEmpty(Collection<?> collection, String message)
:如果集合为空,则抛出带有指定消息的异常。notEmpty(Collection<?> collection)
:如果集合为空,则抛出默认消息的异常。notEmpty(Map<?, ?> map, String message)
:如果映射为空,则抛出带有指定消息的异常。notEmpty(Map<?, ?> map)
:如果映射为空,则抛出默认消息的异常。notEmpty(String str, String message)
:如果字符串为空,则抛出带有指定消息的异常。notEmpty(String str)
:如果字符串为空,则抛出默认消息的异常。notBlank(String str, String message)
:如果字符串为空白,则抛出带有指定消息的异常。notBlank(String str)
:如果字符串为空白,则抛出默认消息的异常。hasText(String text, String message)
:如果字符串为 null、空或空白,则抛出带有指定消息的异常。hasText(String text)
:如果字符串为 null、空或空白,则抛出默认消息的异常。