com.careerly.common.support.response.StandardJsonObject.java Source code

Java tutorial

Introduction

Here is the source code for com.careerly.common.support.response.StandardJsonObject.java

Source

/*
 * Copyright 2013 Qunar.com All right reserved. This software is the confidential and proprietary information of
 * Qunar.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with Qunar.com.
 */
package com.careerly.common.support.response;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.codehaus.jackson.map.annotate.JsonSerialize;

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

/**
 * ??QUNAR?JSON?
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@SuppressWarnings({ "rawtypes", "unchecked" })
public class StandardJsonObject implements Serializable {

    private static final Boolean CORRECT_RET = Boolean.TRUE;

    private static final String DATA_DEFAULT_MESSAGE = "message";
    private static final int DEFAULT_ERR_CODE = 0;
    private static final Boolean ERR_RET = Boolean.FALSE;
    private static final long serialVersionUID = 1L;

    /**
     * ?{"ret": true}
     * 
     * @return ?data
     */
    public static StandardJsonObject newCorrectJsonObject() {
        StandardJsonObject correctJsonObject = new StandardJsonObject();
        correctJsonObject.setRet(StandardJsonObject.CORRECT_RET);
        return correctJsonObject;
    }

    /**
     * ?DTO{"ret": true, "data": [{JsonStringOfObject}]}
     * 
     * @param object ?<Map>
     * @return
     */
    public static StandardJsonObject newCorrectJsonObject(Object object) {
        StandardJsonObject correctJsonObject = StandardJsonObject.newCorrectJsonObject();
        if (object instanceof List) {
            correctJsonObject.setData((List) object);
        } else {
            correctJsonObject.putData(StandardJsonObject.DATA_DEFAULT_MESSAGE, object);
        }

        return correctJsonObject;
    }

    /**
     * ?DTO{"ret": false,"errcode": code,"errmsg" : "msg"}
     * 
     * @param code
     * @param msg
     * @return
     */
    public static StandardJsonObject newErrorJsonObject(Integer code, String msg) {
        StandardJsonObject errorReturnObject = new StandardJsonObject();
        errorReturnObject.setRet(StandardJsonObject.ERR_RET);
        errorReturnObject.setErrcode(code);
        errorReturnObject.setErrmsg(msg);
        return errorReturnObject;
    }

    /**
     * ?DTO{"ret": false,"errmsg" : "msg"}
     * 
     * @param msg
     * @return
     */
    public static StandardJsonObject newErrorJsonObject(String msg) {
        return StandardJsonObject.newErrorJsonObject(StandardJsonObject.DEFAULT_ERR_CODE, msg);
    }

    /** ? */
    private List<Object> data;

    /** ?   */
    private Integer errcode;

    /** ? ?? */
    private String errmsg;

    /** ???truefalse */
    private Boolean ret;

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        StandardJsonObject other = (StandardJsonObject) obj;
        if (data == null) {
            if (other.data != null) {
                return false;
            }
        } else if (!data.equals(other.data)) {
            return false;
        }
        if (errcode == null) {
            if (other.errcode != null) {
                return false;
            }
        } else if (!errcode.equals(other.errcode)) {
            return false;
        }
        if (errmsg == null) {
            if (other.errmsg != null) {
                return false;
            }
        } else if (!errmsg.equals(other.errmsg)) {
            return false;
        }
        if (ret == null) {
            if (other.ret != null) {
                return false;
            }
        } else if (!ret.equals(other.ret)) {
            return false;
        }
        return true;
    }

    /**
     * @return the data
     */
    public List<Object> getData() {
        return data;
    }

    /**
     * @return the errcode
     */
    public Integer getErrcode() {
        return errcode;
    }

    /** -----------------setter/getter--------------------- **/

    /**
     * @return the errmsg
     */
    public String getErrmsg() {
        return errmsg;
    }

    /**
     * @return the ret
     */
    public Boolean getRet() {
        return ret;
    }

    /**
     * key
     * 
     * @param key
     * @return
     */
    public Object getValueDate(String key) {
        if (this.data == null) {
            return null;
        }
        Object firstObject = data.get(0);

        if (firstObject instanceof Map) {
            return ((Map<String, Object>) firstObject).get(key);
        } else {
            throw new RuntimeException("the value object is not-map object");
        }

    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (data == null ? 0 : data.hashCode());
        result = prime * result + (errcode == null ? 0 : errcode.hashCode());
        result = prime * result + (errmsg == null ? 0 : errmsg.hashCode());
        result = prime * result + (ret == null ? 0 : ret.hashCode());
        return result;
    }

    /**
     * List<Object>??
     * 
     * @param object
     * @return
     */
    public StandardJsonObject putData(Object object) {
        if (object == null) {
            return this;
        }

        if (this.data == null) {
            this.data = new ArrayList<Object>();
        }
        data.add(object);
        return this;
    }

    /**
     * HashMap??
     * 
     * @param key
     * @param value
     */
    public StandardJsonObject putData(String key, Object value) {
        if (StringUtils.isBlank(key)) {
            return this;
        }

        if (this.data == null) {
            this.data = new ArrayList<Object>();
            data.add(new HashMap<String, Object>());
        }

        Object firstObject = data.get(0);

        if (firstObject instanceof Map) {
            ((Map<String, Object>) firstObject).put(key, value);
        } else {
            throw new RuntimeException("Can't put k-v into not-map object");
        }
        return this;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<Object> data) {
        this.data = data;
    }

    /**
     * @param errcode the errcode to set
     */
    public void setErrcode(Integer errcode) {
        this.errcode = errcode;
    }

    /**
     * @param errmsg the errmsg to set
     */
    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    /**
     * @param ret the ret to set
     */
    public void setRet(Boolean ret) {
        this.ret = ret;
    }

    /** -----------------equals/hashCode--------------------- **/

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}