isTrue(boolean expression) / isTrue(boolean expression, String message)当 expression 不为 true 抛出异常;
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”);
}
notEmpty(Collection collection) / notEmpty(Collection collection, String message)
当集合未包含元素时抛出异常。
notEmpty(Map map) / notEmpty(Map map, String message) 和 notEmpty(Object[] array, String message) / ## notEmpty(Object[] array, String message) 分别对 Map 和 Object[] 类型的入参进行判断;
public static void notEmpty(Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new IllegalArgumentException(message);
}
}
public static void notEmpty(Object[] array) {
notEmpty(array, “[Assertion failed] - this array must not be empty: it must contain at least 1 element”);
}
hasLength(String text) / hasLength(String text, String message)
当 text 为 null 或长度为 0 时抛出异常;
public static void hasLength(String text) {
hasLength(text,
“[Assertion failed] - this String argument must have length; it must not be null or empty”);
}
public static void hasLength(String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new IllegalArgumentException(message);
}
}
hasText(String text) / hasText(String text, String message)
text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;
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”);
}
isInstanceOf(Class clazz, Object obj) / isInstanceOf(Class type, Object obj, String message)
如果 obj 不能被正确造型为 clazz 指定的类将抛出异常;
isAssignable(Class superType, Class subType) / isAssignable(Class superType, Class subType, String message)
subType 必须可以按类型匹配于 superType,否则将抛出异常;
最后
现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**
全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**
[外链图片转存中…(img-pSpU0Ile-1719723329232)]