io.github.nichetoolkit.rest.DefaultResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-toolkit-core Show documentation
Show all versions of rest-toolkit-core Show documentation
Rest toolkit core project for Spring Boot
package io.github.nichetoolkit.rest;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
/**
* DefaultResult
* @author Cyan ([email protected])
* @version v1.0.0
*/
@Data
@SuppressWarnings({"UnnecessaryParentheses","unused"})
public class DefaultResult> implements Serializable {
private Integer status;
private String message;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
private DefaultError error;
public DefaultResult() {
}
public DefaultResult(Integer status, String message) {
this.status = status;
this.message = message;
}
public DefaultResult(DefaultResult.Builder builder) {
if (builder.cause != null) {
this.error = new DefaultError(builder.cause);
this.message = builder.message == null ? builder.cause.getMessage() == null ? this.error.getLocalizedMessage() : builder.cause.getMessage() : builder.message;
} else {
this.message = builder.message;
}
this.status = builder.status;
this.data = builder.data;
}
public DefaultResult(Integer status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public static > DefaultResult success() {
return (new DefaultResult.Builder()).status(RestErrorStatus.SUCCESS.getStatus()).build();
}
public static > DefaultResult success(T data) {
return (new DefaultResult.Builder()).status(RestErrorStatus.SUCCESS.getStatus()).data(data).build();
}
public static > DefaultResult success(String message) {
return (new DefaultResult.Builder()).status(RestErrorStatus.SUCCESS.getStatus()).message(message).build();
}
public static > DefaultResult success(String message, T data) {
return (new DefaultResult.Builder()).status(RestErrorStatus.SUCCESS.getStatus()).message(message).data(data).build();
}
public static > DefaultResult success(RestStatus status) {
return (new DefaultResult.Builder()).status(status.getStatus()).message(status.getMessage()).build();
}
public static > DefaultResult success(RestStatus status, T data) {
return (new DefaultResult.Builder()).status(status.getStatus()).message(status.getMessage()).data(data).build();
}
public static > DefaultResult fail(String message) {
return (new DefaultResult.Builder()).status(RestErrorStatus.FAILED.getStatus()).message(message).build();
}
public static > DefaultResult fail(String message, T data) {
return (new DefaultResult.Builder()).status(RestErrorStatus.FAILED.getStatus()).message(message).data(data).build();
}
public static > DefaultResult fail(Integer status, String message, T data) {
return (new DefaultResult.Builder()).status(status).message(message).data(data).build();
}
public static > DefaultResult fail(Integer status,Throwable cause) {
return (new DefaultResult.Builder()).status(status).message(cause.getMessage()).cause(cause).build();
}
public static > DefaultResult fail(Integer status, String message) {
return (new DefaultResult.Builder()).status(status).message(message).build();
}
public static > DefaultResult fail(RestStatus status) {
return (new DefaultResult.Builder()).status(status.getStatus()).message(status.getMessage()).build();
}
public static > DefaultResult fail(RestStatus status,Throwable cause) {
return (new Builder()).status(status.getStatus()).message(status.getMessage()).cause(cause).build();
}
public static > DefaultResult fail(RestStatus status, T data) {
return (new DefaultResult.Builder()).status(status.getStatus()).message(status.getMessage()).data(data).build();
}
public static > DefaultResult.Builder defaultBuilder() {
return new DefaultResult.Builder<>();
}
public static class Builder> {
protected Integer status;
protected String message;
protected T data;
private Throwable cause;
public Builder() {
}
public DefaultResult.Builder status(Integer status) {
this.status = status;
return this;
}
public DefaultResult.Builder status(RestStatus status) {
this.status = status.getStatus();
return this;
}
public DefaultResult.Builder message(String message) {
this.message = message;
return this;
}
public DefaultResult.Builder message(RestStatus status) {
this.message = status.getMessage();
return this;
}
public DefaultResult.Builder data(T data) {
this.data = data;
return this;
}
public DefaultResult.Builder cause(Throwable cause) {
this.cause = cause;
return this;
}
public DefaultResult build() {
return new DefaultResult<>(this);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy