其他分享
首页 > 其他分享> > 统一返回值,Result

统一返回值,Result

作者:互联网

统一返回值,Result

 

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private T data;
    private int code;
    private String message;

    private Result(IResultCode resultCode) {
        this(resultCode, (Object)null, resultCode.getMessage());
    }

    private Result(IResultCode resultCode, String msg) {
        this(resultCode, (Object)null, msg);
    }

    private Result(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private Result(IResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private Result(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
    }

    public static boolean isSuccess(@Nullable Result<?> result) {
        return (Boolean)Optional.ofNullable(result).map((x) -> {
            return ObjectUtils.nullSafeEquals(ResultCode.SUCCESS.code, x.code);
        }).orElse(Boolean.FALSE);
    }

    public static Result data(List data) {
        return data(data, "操作成功");
    }

    public static Result data(List data, String msg) {
        return data(200, data, msg);
    }

    public static Result data(int code, List data, String msg) {
        return new Result(code, data, data != null && data.size() > 0 ? msg : "暂时没有更多数据");
    }

    public static Result data(int code, String msg) {
        return new Result(code, (Object)null, msg);
    }

    public static <T> Result<T> succeed(String msg) {
        return new Result(ResultCode.SUCCESS, msg);
    }

    public static <T> Result<T> succeed(T data) {
        return new Result(ResultCode.SUCCESS, data, "操作成功");
    }

    public static <T> Result<T> succeed(T data, String msg) {
        return new Result(HttpStatus.OK.value(), data, msg);
    }

    public static <T> Result<T> succeed(IResultCode resultCode, T data, String msg) {
        return new Result(resultCode, data, msg);
    }

    public static <T> Result<T> succeed(IResultCode resultCode) {
        return new Result(resultCode);
    }

    public static <T> Result<T> succeed(IResultCode resultCode, String msg) {
        return new Result(resultCode, msg);
    }

    public static <T> Result<T> failed(String msg) {
        return new Result(ResultCode.FAILURE, msg);
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return new Result(resultCode);
    }

    public static <T> Result<T> failed(IResultCode resultCode, String msg) {
        return new Result(resultCode, msg);
    }

    public static <T> Result<T> failed(T data, String msg) {
        return new Result(ResultCode.FAILURE, data, msg);
    }

    public static <T> Result<T> status(boolean flag) {
        return flag ? succeed("操作成功") : failed("操作失败");
    }

    public static <T> Result<T> status(int result) {
        return result > 0 ? succeed("操作成功") : failed("操作失败");
    }

    public T getData() {
        return this.data;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String toString() {
        return "Result(data=" + this.getData() + ", code=" + this.getCode() + ", message=" + this.getMessage() + ")";
    }

    public Result() {
    }
}

 

标签:return,Result,msg,返回值,resultCode,data,public,统一
来源: https://www.cnblogs.com/manmanblogs/p/16316937.html