Assert(斷言) 替換 throw exception

class BaseException extends RuntimeException{
    public BaseException(IResponseEnum responseEnum, Object[] args, String message){
        System.out.println(message);

    }
    public BaseException(IResponseEnum responseEnum, Object[] args, String message, Throwable cause){
        System.out.println(message);

    }
}

class BusinessException extends  BaseException {
    public BusinessException(IResponseEnum responseEnum, Object[] args, String message) {
        super(responseEnum, args, message);
    }
    public BusinessException(IResponseEnum responseEnum, Object[] args, String message, Throwable cause) {
        super(responseEnum, args, message, cause);
    }
}

interface IResponseEnum{
    int getCode();
    String getMessage();
}

interface Assert {
    /**
     * 創建異常
     * @param args
     * @return
     */
    BaseException newException(Object... args);

    /**
     * 創建異常
     * @param t
     * @param args
     * @return
     */
    BaseException newException(Throwable t, Object... args);

    /**
     * <p>斷言對象<code>obj</code>非空。如果對象<code>obj</code>爲空,則拋出異常
     *
     * @param obj 待判斷對象
     */
    default void assertNotNull(Object obj) {
        if (obj == null) {
            throw newException(obj);
        }
    }

    /**
     * <p>斷言對象<code>obj</code>非空。如果對象<code>obj</code>爲空,則拋出異常
     * <p>異常信息<code>message</code>支持傳遞參數方式,避免在判斷之前進行字符串拼接操作
     *
     * @param obj 待判斷對象
     * @param args message佔位符對應的參數列表
     */
    default void assertNotNull(Object obj, Object... args) {
        if (obj == null) {
            throw newException(args);
        }
    }
}

interface BusinessExceptionAssert extends IResponseEnum, Assert {
    @Override
    default BaseException newException(Object... args) {
        String msg = MessageFormat.format(this.getMessage(), args);
        return new BusinessException(this, args, msg);
    }
    @Override
    default BaseException newException(Throwable t, Object... args) {
        String msg = MessageFormat.format(this.getMessage(), args);
        return new BusinessException(this, args, msg, t);
    }
}


enum ResponseEnum implements BusinessExceptionAssert {

    /**
     * Bad licence type
     */
    BAD_LICENCE_TYPE(7001, "Bad licence type."),
    /**
     * Licence not found
     */
    LICENCE_NOT_FOUND(7002, "Licence not found.")
    ;

    /**
     * 返回碼
     */
    private int code;
    /**
     * 返回消息
     */
    private String message;

    private ResponseEnum(int i, String s) {
        this.code = i;
        this.message = s;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

class Licence{
    private void checkNotNull(Licence licence) {
        ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);
    }

    @Test
    public void test1() {
        Licence licence = null;
        checkNotNull(licence);

    }

    @Test
    public void test2() {
        // 另一種寫法
        Licence licence = null;
        if (licence == null) {
            throw new BusinessException(7002, "Licence not found.");
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章