me.kuku.pojo.Result Maven / Gradle / Ivy
package me.kuku.pojo;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result {
private final Integer code;
private final String message;
private final T data;
private Result(Integer code, String message, T data){
this.code = code;
this.message = message;
this.data = data;
}
@JSONField(serialize = false)
@JsonIgnore
public boolean isSuccess(){
return this.code == 200;
}
@JSONField(serialize = false)
@JsonIgnore
public boolean isFailure(){
return this.code != 200;
}
public static Result success(){
return new Result<>(200, "成功", null);
}
public static Result success(T data){
return new Result<>(200, "成功", data);
}
public static Result success(String message, T data){
if (message == null){
return success(data);
}
return new Result<>(200, message, data);
}
public static Result failure(String message){
return new Result<>(500, message, null);
}
public static Result failure(T data){
return failure(500, "", data);
}
public static Result failure(Integer code, String message, T data){
return new Result<>(code, message, data);
}
public static Result failure(Integer code, String message){
return new Result<>(code, message, null);
}
public static Result failure(String message, T data){
return failure(500, message, data);
}
public static Result failure(ResultStatus resultStatus, T data){
return failure(resultStatus.getCode(), resultStatus.getMessage(), data);
}
public static Result failure(ResultStatus resultStatus){
return failure(resultStatus, null);
}
public boolean equals(ResultStatus resultStatus){
return this.code.equals(resultStatus.getCode());
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}