All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.woodwhales.common.model.result.OpResult Maven / Gradle / Ivy

There is a newer version: 3.8.3
Show newest version
package cn.woodwhales.common.model.result;

import cn.woodwhales.common.model.enums.RespCodeEnum;

import java.util.Objects;

/**
 * 业务数据响应体
 *
 * @author: woodwhales on 2020-09-18 22:05
 *
 */
public class OpResult {

    /**
     * 通用基础响应结果对象
     */
    private BaseRespResult baseRespResult;

    /**
     * 业务数据
     */
    private T data;

    public static  OpResult success(T data) {
        return new OpResult(RespCodeEnum.SUCCESS, data);
    }

    public static  OpResult success() {
        return new OpResult(RespCodeEnum.SUCCESS, null);
    }

    public static  OpResult failure() {
        return new OpResult(RespCodeEnum.ERROR, null);
    }

    public static  OpResult failure(BaseRespResult baseRespResult) {
        return new OpResult(baseRespResult, null);
    }

    public static  OpResult failure(BaseRespResult baseRespResult, T data) {
        return new OpResult(baseRespResult, data);
    }

    /**
     * 获取数据对象
     * @return 数据对象
     */
    public T getData() {
        return data;
    }

    /**
     * 获取响应状态码对象
     * @return 响应状态码对象
     */
    public BaseRespResult getBaseRespResult() {
        return baseRespResult;
    }

    /**
     * 数据是否为空
     * @return 数据是否为空
     */
    public boolean dataIsNull() {
        return Objects.isNull(data);
    }

    /**
     * 数据是否不为空
     * @return 数据是否不为空
     */
    public boolean dataIsNonNull() {
        return Objects.nonNull(data);
    }

    /**
     * 是否响应成功
     * @return 是否响应成功
     */
    public boolean isSuccessful() {
        return Objects.equals(baseRespResult.getCode(), RespCodeEnum.SUCCESS.getCode());
    }

    /**
     * 是否响应失败
     * @return 是否响应失败
     */
    public boolean isFailure() {
        return !isSuccessful();
    }

    private OpResult() {
    }

    private OpResult(BaseRespResult baseRespResult, T data) {
        this.baseRespResult = baseRespResult;
        this.data = data;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy