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

com.gitee.maskit.utils.Response Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.gitee.maskit.utils;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Response implements Serializable {
    public static final String GENERAL_SUCCESS_CODE = "maskit.success.general";
    public static final String GENERAL_SUCCESS_MESSAGE = "Maskit general success";
    public static final String GENERAL_FAILURE_CODE = "maskit.failure.general";
    public static final String GENERAL_FAILURE_MESSAGE = "Maskit general failure";
    public static final int GENERAL_SUCCESS_STATUS = 200;
    public static final int GENERAL_FAILURE_STATUS = 400;

    private int status = GENERAL_SUCCESS_STATUS;
    private String error = GENERAL_SUCCESS_CODE;
    private String message = GENERAL_SUCCESS_MESSAGE;
    @JsonSerialize(nullsUsing = ResponseNullTimestampSerializer.class, converter = ResponseTimestampStringConverter.class)
    @JsonDeserialize(converter = ResponseStringTimestampConverter.class)
    private Date timestamp = null;
    private T data = null;
    private String path = null;

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

    public boolean success() {
        return status >= 200 && status < 300;
    }

    public static  Response yes() {
        return new Response(GENERAL_SUCCESS_STATUS, GENERAL_SUCCESS_CODE, GENERAL_SUCCESS_MESSAGE, true);
    }

    public static  Response yes(final T data) {
        return new Response(GENERAL_SUCCESS_STATUS, GENERAL_SUCCESS_CODE, GENERAL_SUCCESS_MESSAGE, data);
    }

    public static  Response yes(final T data, final String code) {
        return new Response(GENERAL_SUCCESS_STATUS, GENERAL_SUCCESS_CODE, GENERAL_SUCCESS_MESSAGE, data);
    }

    public static  Response yes(final T data, final int status) {
        return new Response(status, GENERAL_SUCCESS_CODE, GENERAL_SUCCESS_MESSAGE, data);
    }

    public static  Response yes(final T data, final int status, final String code) {
        return new Response(status, code, GENERAL_SUCCESS_MESSAGE, data);
    }

    public static  Response yes(final T data, final int status, final String code, final String message) {
        return new Response(status, code, message, data);
    }

    public static  Response no() {
        return new Response(GENERAL_FAILURE_STATUS, GENERAL_FAILURE_CODE, GENERAL_FAILURE_MESSAGE, null);
    }

    public static  Response no(final String code) {
        return new Response(GENERAL_FAILURE_STATUS, code, GENERAL_FAILURE_MESSAGE, null);
    }

    public static  Response no(final int status) {
        return new Response(status, GENERAL_FAILURE_CODE, GENERAL_FAILURE_MESSAGE, null);
    }

    public static  Response no(final int status, final String code) {
        return new Response(status, code, GENERAL_FAILURE_MESSAGE, null);
    }

    public static  Response no(final int status, final String code, final String message) {
        return new Response(status, code, message, null);
    }

    public static  Response no(final int status, final String code, final T data) {
        return new Response(status, code, GENERAL_FAILURE_MESSAGE, data);
    }

    public static  Response no(final T data) {
        return new Response(GENERAL_FAILURE_STATUS, GENERAL_FAILURE_CODE, GENERAL_FAILURE_MESSAGE, data);
    }

    public static  Response auto(final Boolean success, final String code, final T data) {
        return new Response(success ? GENERAL_SUCCESS_STATUS : GENERAL_FAILURE_STATUS,
            code, success ? GENERAL_SUCCESS_MESSAGE : GENERAL_FAILURE_MESSAGE, data);
    }

    public static  Response auto(final I input, final ResponseAdapter adapter) {
        return adapter.apply(input);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy