java的Result类

 
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Result <T> extends BaseDomain implements Serializable {
private String code;
private String msg;
private T data;

private Result() {
this.code = UnifyStrErrorCode.SUCCESS.getCode();
this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
}

private Result(T data) {
this.code = UnifyStrErrorCode.SUCCESS.getCode();
this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
this.setData(data);
}

private Result(String code, String msg) {
this.code = UnifyStrErrorCode.SUCCESS.getCode();
this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
this.setCode(code);
this.setMsg(msg);
}

private Result(String code, String msg, T data) {
this.code = UnifyStrErrorCode.SUCCESS.getCode();
this.msg = UnifyStrErrorCode.SUCCESS.getMsg();
this.setCode(code);
this.setMsg(msg);
this.setData(data);
}

public Result<T> setError(String code, String msg) {
this.setCode(code);
this.setMsg(msg);
return this;
}

public Result<T> setError(UnifyStrErrorCode errorCode) {
this.setCode(errorCode.getCode());
this.setMsg(errorCode.getMsg());
return this;
}

public boolean isSuccess() {
return StringUtils.equals(this.getCode(), UnifyStrErrorCode.SUCCESS.getCode());
}

public static Result instance() {
return new Result();
}

public static <T> Result instance(T data) {
return new Result(data);
}

public static <T> Result instance(String code, String msg) {
return new Result(code, msg);
}

public static <T> Result instance(String code, String msg, T data) {
return new Result(code, msg, data);
}

public String getCode() {
return this.code;
}

public void setCode(String code) {
this.code = code;
}

public String getMsg() {
return this.msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public T getData() {
return this.data;
}

public void setData(T data) {
this.data = data;
}

public Map<String,Object> toJsonMap(){
Map<String,Object> map = new HashMap<>();
map.put("data",this.data);
map.put("msg",this.msg);
map.put("code",this.code);
return map;
}

}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、


 
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;


public class BaseDomain implements Serializable {
public BaseDomain() {
}

public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}



转载于:https://www.cnblogs.com/YuyuanNo1/p/9767523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值