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

org.macrocloud.kernel.toolkit.api.R Maven / Gradle / Ivy

There is a newer version: 1.1.0-RELEASE
Show newest version
package org.macrocloud.kernel.toolkit.api;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.macrocloud.kernel.toolkit.constant.BaseConstant;
import org.macrocloud.kernel.toolkit.utils.ObjectUtil;
import org.springframework.lang.Nullable;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Optional;


/**
 * 统一API响应结果封装.
 *
 * @param  the generic type
 */


@Getter
@Setter
@ToString
@ApiModel(description = "返回信息")

/**
 * Instantiates a new r.
 */
@NoArgsConstructor
public class R implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The code. */
	@ApiModelProperty(value = "状态码", required = true)
	private int code;
	
	/** The success. */
	@ApiModelProperty(value = "是否成功", required = true)
	private boolean success;
	
	/** The data. */
	@ApiModelProperty(value = "承载数据")
	private T data;
	
	/** The msg. */
	@ApiModelProperty(value = "返回消息", required = true)
	private String msg;

	/**
	 * Instantiates a new r.
	 *
	 * @param resultCode the result code
	 */
	private R(IResultCode resultCode) {
		this(resultCode, null, resultCode.getMessage());
	}

	/**
	 * Instantiates a new r.
	 *
	 * @param resultCode the result code
	 * @param msg the msg
	 */
	private R(IResultCode resultCode, String msg) {
		this(resultCode, null, msg);
	}

	/**
	 * Instantiates a new r.
	 *
	 * @param resultCode the result code
	 * @param data the data
	 */
	private R(IResultCode resultCode, T data) {
		this(resultCode, data, resultCode.getMessage());
	}

	/**
	 * Instantiates a new r.
	 *
	 * @param resultCode the result code
	 * @param data the data
	 * @param msg the msg
	 */
	private R(IResultCode resultCode, T data, String msg) {
		this(resultCode.getCode(), data, msg);
	}

	/**
	 * Instantiates a new r.
	 *
	 * @param code the code
	 * @param data the data
	 * @param msg the msg
	 */
	private R(int code, T data, String msg) {
		this.code = code;
		this.data = data;
		this.msg = msg;
		this.success = ResultCode.SUCCESS.code == code;
	}

	/**
	 * 判断返回是否为成功.
	 *
	 * @param result Result
	 * @return boolean 是否成功
	 */
	public static boolean isSuccess(@Nullable R result) {
		return Optional.ofNullable(result)
			.map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code))
			.orElse(Boolean.FALSE);
	}

	/**
	 * 判断返回是否为成功.
	 *
	 * @param result Result
	 * @return boolean 是否成功
	 */
	public static boolean isNotSuccess(@Nullable R result) {
		return !R.isSuccess(result);
	}

	/**
	 * 返回R.
	 *
	 * @param   T 泛型标记
	 * @param data 数据
	 * @return R
	 */
	public static  R data(T data) {
		return data(data, BaseConstant.DEFAULT_SUCCESS_MESSAGE);
	}

	/**
	 * 返回R.
	 *
	 * @param   T 泛型标记
	 * @param data 数据
	 * @param msg  消息
	 * @return R
	 */
	public static  R data(T data, String msg) {
		return data(HttpServletResponse.SC_OK, data, msg);
	}

	/**
	 * 返回R.
	 *
	 * @param   T 泛型标记
	 * @param code 状态码
	 * @param data 数据
	 * @param msg  消息
	 * @return R
	 */
	public static  R data(int code, T data, String msg) {
		return new R<>(code, data, data == null ? BaseConstant.DEFAULT_NULL_MESSAGE : msg);
	}

	/**
	 * 返回R.
	 *
	 * @param  T 泛型标记
	 * @param msg 消息
	 * @return R
	 */
	public static  R success(String msg) {
		return new R<>(ResultCode.SUCCESS, msg);
	}

	/**
	 * 返回R.
	 *
	 * @param         T 泛型标记
	 * @param resultCode 业务代码
	 * @return R
	 */
	public static  R success(IResultCode resultCode) {
		return new R<>(resultCode);
	}

	/**
	 * 返回R.
	 *
	 * @param         T 泛型标记
	 * @param resultCode 业务代码
	 * @param msg        消息
	 * @return R
	 */
	public static  R success(IResultCode resultCode, String msg) {
		return new R<>(resultCode, msg);
	}

	/**
	 * 返回R.
	 *
	 * @param  T 泛型标记
	 * @param msg 消息
	 * @return R
	 */
	public static  R fail(String msg) {
		return new R<>(ResultCode.FAILURE, msg);
	}


	/**
	 * 返回R.
	 *
	 * @param   T 泛型标记
	 * @param code 状态码
	 * @param msg  消息
	 * @return R
	 */
	public static  R fail(int code, String msg) {
		return new R<>(code, null, msg);
	}

	/**
	 * 返回R.
	 *
	 * @param         T 泛型标记
	 * @param resultCode 业务代码
	 * @return R
	 */
	public static  R fail(IResultCode resultCode) {
		return new R<>(resultCode);
	}

	/**
	 * 返回R.
	 *
	 * @param         T 泛型标记
	 * @param resultCode 业务代码
	 * @param msg        消息
	 * @return R
	 */
	public static  R fail(IResultCode resultCode, String msg) {
		return new R<>(resultCode, msg);
	}

	/**
	 * 返回R.
	 *
	 * @param  the generic type
	 * @param flag 成功状态
	 * @return R
	 */
	public static  R status(boolean flag) {
		return flag ? success(BaseConstant.DEFAULT_SUCCESS_MESSAGE) : fail(BaseConstant.DEFAULT_FAILURE_MESSAGE);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy