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

io.github.jianjianghui.response.v1.Response Maven / Gradle / Ivy

The newest version!
package io.github.jianjianghui.response.v1;

/**
 * response
 *
 * @author Jianghui Jian
 * @date 2021/7/7 - 15:23
 */
public class Response {
    private static int DEFAULT_SUCCESS_CODE = 0;
    private static int DEFAULT_ERROR_CODE = -1;

    private static String DEFAULT_SUCCESS_MESSAGE = "successful";
    private static String DEFAULT_ERROR_MESSAGE = "fail";

    /**
     * DEFAULT_ERROR_CODE code
     */
    int code;

    /**
     * message
     */
    String message;

    /**
     * data
     */
    T data;


    public Response(int code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public Response() {
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }



    private static  Response create(int code, String msg, T data) {
        return new Response<>(code, msg, data);
    }

    public static  Response success(String msg, T data) {
        return create(DEFAULT_SUCCESS_CODE, msg, data);
    }

    public static  Response success(String msg) {
        return create(DEFAULT_SUCCESS_CODE, msg, null);
    }

    public static  Response success(T data) {
        return create(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static  Response success() {
        return create(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, null);
    }


    public static  Response error(int code, String msg, T data) {
        return create(code, msg, data);
    }

    public static  Response error(int code, String msg) {
        return create(code, msg, null);
    }

    public static  Response error(String msg, T data) {
        return create(DEFAULT_ERROR_CODE, msg, data);
    }

    public static  Response error(String msg) {
        return create(DEFAULT_ERROR_CODE, msg, null);
    }

    public static  Response error(T data) {
        return create(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE, data);
    }

    public static  Response error() {
        return create(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE, null);
    }


    public static void defaultSuccessCode(int successCode) {
        Response.DEFAULT_SUCCESS_CODE = successCode;
    }

    public static void defaultSuccessMessage(String successMessage) {
        Response.DEFAULT_SUCCESS_MESSAGE = successMessage;
    }

    public static int defaultSuccessCode() {
        return Response.DEFAULT_SUCCESS_CODE;
    }

    public static String defaultSuccessMessage() {
        return Response.DEFAULT_SUCCESS_MESSAGE;
    }

    public static void defaultErrorCode(int errorCode) {
        Response.DEFAULT_ERROR_CODE = errorCode;
    }

    public static void defaultErrorMessage(String errorMessage) {
        Response.DEFAULT_ERROR_MESSAGE = errorMessage;
    }

    public static int defaultErrorCode() {
        return Response.DEFAULT_ERROR_CODE;
    }

    public static String defaultErrorMessage() {
        return Response.DEFAULT_ERROR_MESSAGE;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy