com.soento.core.lang.RespData Maven / Gradle / Ivy
package com.soento.core.lang;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.soento.core.consts.BaseCode;
import com.soento.core.util.JsonUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author yantao.zeng
*/
public class RespData extends BaseObject {
private String code;
private String message;
private Object data;
@JsonIgnore
public boolean isSuccess() {
return BaseCode.SUCCESS.equals(code);
}
@JsonIgnore
public T getData(Class clazz) {
if (data == null) {
return null;
}
String json = JsonUtil.toJson(data);
return JsonUtil.toObject(json, clazz);
}
@JsonIgnore
public List getDataList(Class clazz) {
if (data == null) {
return null;
}
List resultList = new ArrayList<>();
for (Object row : (List) data) {
String json = JsonUtil.toJson(row);
resultList.add(JsonUtil.toObject(json, clazz));
}
return resultList;
}
@JsonIgnore
public String getDataString() {
if (data == null) {
return null;
}
if (data instanceof BigDecimal) {
return ((BigDecimal) data).toPlainString();
}
return data.toString();
}
@JsonIgnore
public Integer getDataInteger() {
if (data == null) {
return null;
}
return Integer.parseInt(getDataString());
}
@JsonIgnore
public Long getDataLong() {
if (data == null) {
return null;
}
return Long.parseLong(getDataString());
}
@JsonIgnore
public Double getDataDouble() {
if (data == null) {
return null;
}
return Double.parseDouble(getDataString());
}
@JsonIgnore
public Float getDataFloat() {
if (data == null) {
return null;
}
return Float.parseFloat(getDataString());
}
@JsonIgnore
public BigDecimal getDataBigDecimal() {
if (data == null) {
return null;
}
if (data instanceof BigDecimal) {
return (BigDecimal) data;
}
return new BigDecimal(getDataString());
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}