3、如何优雅的进行参数校验


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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值