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

com.github.azbh111.utils.java.model.Res Maven / Gradle / Ivy

The newest version!
package com.github.azbh111.utils.java.model;

import java.io.Serializable;
import java.util.function.Function;

/**
 * 可以用来封装带有失败状态的返回值
 * 如果status为null,会返回OK
 *
 * @author pyz
 * @date 2019/6/1 3:12 PM
 */
public class Res implements Cloneable, Serializable {
    private static final long serialVersionUID = 1067562144473066103L;

    private T data;
    private int status = 200;
    private String errorMsg;

    public static  Res empty() {
        return new Res<>();
    }

    public static  Res success() {
        return new Res();
    }

    public static  Res success(T data) {
        return new Res().setData(data);
    }

    public static  Res fail() {
        return fail(400);
    }

    public static  Res fail(int status) {
        return fail(null, status, null, new Object[0]);
    }

    public static  Res fail(String errorMsg, Object... os) {
        return fail(null, ResStatusEnum.BAD_REQUEST, errorMsg, os);
    }

    public static  Res fail(int status, String errorMsg, Object... os) {
        return fail(null, status, errorMsg, os);
    }

    public static  Res fail(T data, int status, String errorMsg, Object... os) {
        return new Res().setData(data).setStatus(status).errorMsg(errorMsg, os);
    }

    /**
     * 擦除泛型
     *
     * @return
     */
    public Res eraseGeneric() {
        return this;
    }

    /**
     * 将res的属性复制到当前实例
     *
     * @param res
     * @return
     */
    public Res apply(Res res) {
        this.status = res.status;
        this.data = res.data;
        this.errorMsg = res.errorMsg;
        return this;
    }

    public int getStatus() {
        return status;
    }

    /**
     * 链式操作,方便多层检查
     *
     * @param mapper
     * @return
     */
    public Res mapIfSuccess(Function, Res> mapper) {
        if (isFail()) {
            return this;
        }
        Res res = mapper.apply(this);
        if (res == null) {
            return this;
        }
        this.apply(res);
        return this;
    }

    public boolean isSuccess() {
        return status == 200;
    }

    public boolean isFail() {
        return status != 200;
    }

    public T getData() {
        return data;
    }

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

    public Res data(T data) {
        this.data = data;
        return this;
    }

    public Res setStatus(int status) {
        this.status = status;
        return this;
    }

    public Res status(int status) {
        this.status = status;
        return this;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public Res setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
        return this;
    }

    public Res errorMsg(String errorMsg, Object... os) {
        this.errorMsg = String.format(errorMsg, os);
        return this;
    }

    @Override
    public Res clone() {
        Res res = Res.empty();
        res.status = status;
        res.data = data;
        res.errorMsg = errorMsg;
        return res;
    }

    @Override
    public String toString() {
        return "Res{" +
                "data=" + data +
                ", status=" + status +
                ", errorMsg='" + errorMsg + '\'' +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy