package com.boman.system.common; import com.alibaba.fastjson.JSONObject; import lombok.Data; import java.io.Serializable; /** * @author shiqian * @description * @date 2021年03月22日 09:26 **/ @Data public class ValueHolder implements Serializable { public static final int OK = 0; public static final int FAIL = -1; private T data; private int code; private String message; public ValueHolder() { } public ValueHolder(int code, String message, T data) { this.data = data; this.code = code; this.message = message; } public ValueHolder(int code, String message) { this.code = code; this.message = message; } public JSONObject toJSONObject() { JSONObject obj = new JSONObject(); obj.put("code", this.code); obj.put("message", this.message); obj.put("data", this.data); return obj; } public static ValueHolder ok(String message){ return new ValueHolder<>(OK, message); } public static ValueHolder ok(String message, T data){ return new ValueHolder<>(OK, message, data); } public static ValueHolder ok(T data){ return new ValueHolder<>(OK, "成功", data); } public static ValueHolder fail(String message, T data){ return new ValueHolder<>(FAIL, message, data); } public static ValueHolder fail(String message){ return new ValueHolder<>(FAIL, message); } public boolean isOK() { return this.code == 0; } public String toDebugString() { return this.toJSONObject().toString(); } public T getData() { return this.data; } public int getCode() { return this.code; } public String getMessage() { return this.message; } public void setData(final T data) { this.data = data; } public void setCode(final int code) { this.code = code; } public void setMessage(final String message) { this.message = message; } @Override public boolean equals(final Object o) { if (o == this) { return true; } else if (!(o instanceof ValueHolder)) { return false; } else { ValueHolder other = (ValueHolder)o; if (!other.canEqual(this)) { return false; } else { label39: { Object this$data = this.getData(); Object other$data = other.getData(); if (this$data == null) { if (other$data == null) { break label39; } } else if (this$data.equals(other$data)) { break label39; } return false; } if (this.getCode() != other.getCode()) { return false; } else { Object this$message = this.getMessage(); Object other$message = other.getMessage(); if (this$message == null) { return other$message == null; } else return this$message.equals(other$message); } } } } protected boolean canEqual(final Object other) { return other instanceof ValueHolder; } @Override public int hashCode() { int result = 1; Object $data = this.getData(); result = result * 59 + ($data == null ? 43 : $data.hashCode()); result = result * 59 + this.getCode(); Object $message = this.getMessage(); result = result * 59 + ($message == null ? 43 : $message.hashCode()); return result; } @Override public String toString() { return "ValueHolder(data=" + this.getData() + ", code=" + this.getCode() + ", message=" + this.getMessage() + ")"; } }