public class AssertUtil {
@FunctionalInterface
public interface CommonExceptionSupplier extends Supplier<CommonException> {
}
private AssertUtil() {
}
public static void checkState(boolean expression) throws CommonException {
if (expression) {
throw new CommonException(ErrorCode.SYSTEM_ERROR);
}
}
public static void checkState(boolean expression, String errorMessage) throws CommonException {
if (expression) {
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
}
public static void checkState(boolean expression, Logger logger, String errorMessage, Object... arguments) throws CommonException {
if (expression) {
errorMessage = logMsg(logger, errorMessage, arguments);
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
}
public static void checkState(boolean expression, Logger logger, String errorMessage) throws CommonException {
if (expression) {
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
}
public static void checkState(boolean expression, CommonExceptionSupplier supplier) throws CommonException {
if (expression) {
throw supplier.get();
}
}
public static <T> T checkNull(T reference) throws CommonException {
if (reference == null) {
throw new CommonException(ErrorCode.SYSTEM_ERROR);
}
return reference;
}
public static <T> T checkNull(T reference, String errorMessage) throws CommonException {
if (reference == null) {
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return reference;
}
public static <T> T checkNull(T reference, Logger logger, String errorMessage, Object... arguments) throws CommonException {
if (reference == null) {
errorMessage = logMsg(logger, errorMessage, arguments);
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return reference;
}
public static <T> T checkNull(T reference, CommonExceptionSupplier supplier) throws CommonException {
if (reference == null) {
throw supplier.get();
}
return reference;
}
public static String checkBlank(String reference) throws CommonException {
if (StringUtils.isBlank(reference)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR);
}
return reference;
}
public static String checkBlank(String reference, Logger logger, String errorMessage, Object... arguments) throws CommonException {
if (StringUtils.isBlank(reference)) {
errorMessage = logMsg(logger, errorMessage, arguments);
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return reference;
}
public static String checkBlank(String reference, CommonExceptionSupplier supplier) throws CommonException {
if (StringUtils.isBlank(reference)) {
throw supplier.get();
}
return reference;
}
public static <T> Collection<T> checkEmpty(Collection<T> collection, String errorMessage) throws CommonException {
if (CollectionUtils.isEmpty(collection)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return collection;
}
public static <T> Collection<T> checkEmpty(Collection<T> collection, Logger logger, String errorMessage, Object... arguments) throws CommonException {
if (CollectionUtils.isEmpty(collection)) {
errorMessage = logMsg(logger, errorMessage, arguments);
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return collection;
}
public static <T> Collection<T> checkEmpty(Collection<T> collection, CommonExceptionSupplier supplier) throws CommonException {
if (CollectionUtils.isEmpty(collection)) {
throw supplier.get();
}
return collection;
}
public static <K, V> Map<K, V> checkEmpty(Map<K, V> map, String errorMessage) throws CommonException {
if (MapUtils.isEmpty(map)) {
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return map;
}
public static <K, V> Map<K, V> checkEmpty(Map<K, V> map, Logger logger, String errorMessage, Object... arguments) throws CommonException {
if (MapUtils.isEmpty(map)) {
errorMessage = logMsg(logger, errorMessage, arguments);
throw new CommonException(ErrorCode.SYSTEM_ERROR, errorMessage);
}
return map;
}
private static String logMsg(Logger logger, String message, Object... arguments) {
logger(logger, message, arguments);
try {
if (null != arguments) {
return String.format(message.replace("{}", "%s"), arguments);
}
} catch (Exception e) {
}
return message;
}
private static void logger(Logger logger, String message, Object... arguments) {
if (logger != null && arguments != null) {
logger.error(message, arguments);
} else if (logger != null) {
logger.error(message);
}
}
}
public class CommonException extends Exception {
private static final long serialVersionUID = 1L;
private ErrorCode errorCode;
public CommonException(ErrorCode errorCode) {
super(errorCode.getDesc());
this.errorCode = errorCode;
}
public CommonException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public ErrorCode getErrorCode() {
return errorCode;
}
}
public enum ErrorCode {
SYSTEM_ERROR(1001, "系统错误"),
USER_AUTH(2001, "认证错误");
private int code;
private String desc;
ErrorCode(int code, String desc) {
this.code = code;
this.desc = desc;
}
public String getDesc() {
return this.desc;
}
public int getCode() {
return this.code;
}
}
@Slf4j
public class Client {
public static void main(String[] args) throws CommonException {
String name = "";
AssertUtil.checkState(StringUtils.isBlank(name), "姓名不能为空");
AssertUtil.checkState(StringUtils.isBlank(name), log, "姓名{}不能为空", name);
}
}