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

cloud.tianai.rpc.common.Result Maven / Gradle / Ivy

package cloud.tianai.rpc.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * @Author: 天爱有情
 * @Date: 2020/01/21 23:12
 * @Description: 统一返回对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {

    /** 成功的code码. */
    public static final Integer SUCCESS_CODE = 200;
    /** 失败的code码. */
    public static final Integer ERROR_CODE = 500;

    private Integer code;
    private String msg;
    private T data;


    public static  Result ofSuccess(T data) {
        return new Result(SUCCESS_CODE, null, data);
    }

    public static  Result ofMessage(Integer code, String msg) {
        return new Result(code, msg, null);
    }

    public static  Result ofError(String msg) {
        return new Result(ERROR_CODE, msg, null);
    }

    public boolean isSuccess() {
        return SUCCESS_CODE.equals(this.code);
    }

    public boolean compare(Integer code) {
        return Objects.equals(this.code, code);
    }

    public Result compare(Integer code, Consumer consumer) {
        if(compare(code)) {
            consumer.accept(data);
        }
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy