AssertUtil类:
package org.springblade.demo.util;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Map;
public class AssertUtil extends Assert {
public static void isTrue(boolean expression, String message) {
if (!expression) {
throw new ServiceException(message);
}
}
public static void isNull(@Nullable Object object, String message) {
if (object != null) {
throw new ServiceException(message);
}
}
public static void notNull(@Nullable Object object, String message) {
if (object == null) {
throw new ServiceException(message);
}
}
public static void hasLength(@Nullable String text, String message) {
if (!StringUtils.hasLength(text)) {
throw new ServiceException(message);
}
}
public static void hasText(@Nullable String text, String message) {
if (!StringUtils.hasText(text)) {
throw new ServiceException(message);
}
}
public static void notEmpty(@Nullable Collection<?> collection, String message) {
if (CollectionUtils.isEmpty(collection)) {
throw new ServiceException(message);
}
}
public static void notEmpty(@Nullable Map<?, ?> map, String message) {
if (CollectionUtils.isEmpty(map)) {
throw new ServiceException(message);
}
}
public static void notEmpty(@Nullable Object[] array, String message) {
if (ObjectUtils.isEmpty(array)) {
throw new ServiceException(message);
}
}
}
使用方法示例:
// new:
AssertUtil.notNull(contractModel, "未找到相关合同模板");
// old:
if(contractModel!=null){
return "未找到相关合同模板";
}