ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

统一返回值,Result

2022-05-27 11:31:46  阅读:178  来源: 互联网

标签:return Result msg 返回值 resultCode data public 统一


统一返回值,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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有