com.stellariver.milky.common.base.Result Maven / Gradle / Ivy
package com.stellariver.milky.common.base;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
public class Result implements Serializable {
protected Boolean success = true;
protected T data;
protected String errorCode;
protected String message;
protected List errors;
public Boolean isSuccess() {
return success;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public String getErrorCode() {
return this.errorCode;
}
public String getMessage() {
return this.message;
}
public List getErrors() {
return this.errors;
}
@Override
public String toString() {
return "Result{" +
"success=" + success +
", data=" + data +
", errorCode='" + errorCode + '\'' +
", message='" + message + '\'' +
", errors=" + errors +
'}';
}
public static Result success() {
return new Result<>();
}
public static Result success(T data) {
Result result = new Result<>();
result.data = data;
return result;
}
public static Result error(Error error) {
Result result = new Result<>();
result.success = false;
result.errorCode = error.getCode();
result.message = error.getMessage();
result.errors = Collections.singletonList(error);
return result;
}
public static Result error(List errors) {
Result result = new Result<>();
result.success = false;
result.errorCode = errors.get(0).getCode();
result.message = errors.get(0).getMessage();
result.errors = errors;
return result;
}
}