统一返回值,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() {
    }
}
复制代码

 

posted @   满Sir  阅读(202)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示