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

cn.vorbote.web.model.ResponseResult Maven / Gradle / Ivy

package cn.vorbote.web.model;

import cn.vorbote.core.time.DateTime;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.function.Supplier;

/**
 * Response model for response body.
 *
 * @param  The type of the result.
 * @author vorbote [email protected]
 */
@Data
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public final class ResponseResult {

    private int code;
    private T data;
    private long timestamp;
    private String message;

    private String requestId;

    // region Getters and Setters
    /**
     * Get the request ID.
     *
     * @return the request ID.
     */
    public String requestId() {
        return requestId;
    }

    public ResponseResult requestId(String requestId) {
        this.requestId = requestId;
        return this;
    }

    /**
     * Get the data of status.
     *
     * @return The status.
     */
    public int code() {
        return code;
    }

    /**
     * Set the data of status.
     *
     * @param code The status.
     * @return The instance itself.
     */
    public ResponseResult code(int code) {
        this.code = code;
        return this;
    }

    /**
     * Get the data of result.
     *
     * @return The result.
     */
    public T data() {
        return data;
    }

    /**
     * Set the data of result.
     *
     * @param data The result.
     * @return The instance itself.
     */
    public ResponseResult data(T data) {
        this.data = data;
        return this;
    }

    /**
     * Get the data of timestamp.
     *
     * @return The timestamp.
     */
    public long timestamp() {
        return timestamp;
    }

    /**
     * Set the data of timestamp.
     *
     * @param timestamp The timestamp.
     * @return The instance itself.
     */
    public ResponseResult timestamp(long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    /**
     * Get the data of message.
     *
     * @return The message.
     */
    public String message() {
        return message;
    }

    /**
     * Set the data of message.
     *
     * @param message The message.
     * @return The instance itself.
     */
    public ResponseResult message(String message) {
        this.message = message;
        return this;
    }

    /**
     * Set the data of message.
     *
     * @param format The format message.
     * @param args   The args to be put into the message.
     * @return The instance itself.
     * @see String#format(String, Object...)
     */
    public ResponseResult message(String format, Object... args) {
        this.message = String.format(format, args);
        return this;
    }
    // endregion

    // region Constructors
    /**
     * Generate a new Response Result instance.
     */
    public ResponseResult() {
        this.timestamp = DateTime.now().unix();
    }
    // endregion

    /**
     * Generate a new Response Result instance with a success status.
     *
     * @param data The data.
     * @param   The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult success(T data) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_OK;
        result.data = data;
        return result;
    }

    /**
     * Generate a new Response Result instance with a success status.
     *
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult success(String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_OK;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with a success status.
     *
     * @param messageSupplier The message.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult success(Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_OK;
        result.message = messageSupplier.get();
        return result;
    }

    /**
     * Generate a new Response Result instance with a success status.
     *
     * @param data    The data.
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult success(T data, String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_OK;
        result.data = data;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with a success status.
     *
     * @param data            The data.
     * @param messageSupplier The message supplier.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult success(T data, Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_OK;
        result.data = data;
        result.message = messageSupplier.get();
        return result;
    }

    /**
     * Generate a new Response Result instance with a timeout status.
     *
     * @param messageSupplier The message supplier.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult timeout(Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_REQUEST_TIMEOUT;
        result.message = messageSupplier.get();
        return result;
    }

    /**
     * Generate a new Response Result instance with a timeout status.
     *
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult timeout(String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_REQUEST_TIMEOUT;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with an error status.
     *
     * @param messageSupplier The messageSupplier.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult error(Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        result.message = messageSupplier.get();
        return result;
    }

    /**
     * Generate a new Response Result instance with an error status.
     *
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult error(String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with an unauthorized status.
     *
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult unauthorized(String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_UNAUTHORIZED;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with an unauthorized status.
     *
     * @param messageSupplier The message supplier.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult unauthorized(Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_UNAUTHORIZED;
        result.message = messageSupplier.get();
        return result;
    }

    /**
     * Generate a new Response Result instance with a forbidden status.
     *
     * @param message The message.
     * @param      The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult forbidden(String message) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_FORBIDDEN;
        result.message = message;
        return result;
    }

    /**
     * Generate a new Response Result instance with a forbidden status.
     *
     * @param messageSupplier The message supplier.
     * @param              The type of the result.
     * @return The instance itself.
     */
    public static  ResponseResult forbidden(Supplier messageSupplier) {
        ResponseResult result = new ResponseResult<>();
        result.code = HttpServletResponse.SC_FORBIDDEN;
        result.message = messageSupplier.get();
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy