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

com.github.yingzhuo.carnival.json.ApiResult Maven / Gradle / Ivy

There is a newer version: 1.10.40
Show newest version
/*
 *  ____    _    ____  _   _ _____     ___    _
 * / ___|  / \  |  _ \| \ | |_ _\ \   / / \  | |
 * | |    / _ \ | |_) |  \| || | \ \ / / _ \ | |
 * | |___/ ___ \|  _ <| |\  || |  \ V / ___ \| |___
 * \____/_/   \_\_| \_\_| \_|___|  \_/_/   \_\_____|
 *
 * https://github.com/yingzhuo/carnival
 */
package com.github.yingzhuo.carnival.json;

import com.fasterxml.jackson.annotation.JsonView;
import com.github.yingzhuo.carnival.common.StringCoded;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author 应卓
 * @since 1.0.4
 */
@JsonView(Views.Normal.class)
public interface ApiResult extends Serializable, StringCoded {

    public static  ApiResult newInstance(final T payload) {
        return newInstance(HttpStatus.OK, null, payload);
    }

    public static  ApiResult newInstance(final HttpStatus httpStatus, final T payload) {
        return newInstance(String.valueOf(Objects.requireNonNull(httpStatus).value()), null, payload);
    }

    public static  ApiResult newInstance(final String code, final T payload) {
        return newInstance(code, null, payload);
    }

    // -----------------------------------------------------------------------------------------------------------------

    public static  ApiResult newInstance(final HttpStatus httpStatus, final String errorMessage, final T payload) {
        return newInstance(String.valueOf(Objects.requireNonNull(httpStatus).value()), errorMessage, payload);
    }

    public static  ApiResult newInstance(final String code, final String errorMessage, final T payload) {
        Objects.requireNonNull(code);

        return new ApiResult() {
            @Override
            public String getCode() {
                return code;
            }

            @Override
            public String getErrorMessage() {
                return errorMessage;
            }

            @Override
            public T getPayload() {
                return payload;
            }
        };
    }

    public String getCode();

    public String getErrorMessage();

    public T getPayload();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy