Result.java 2.14 KB
package com.glsx.model;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: DemoVO.java
 * @Description: 
 * @author deployer name  
 * @version V1.0  
 * @Company: Didihu.com.cn
 * @Copyright Copyright (c) 2017
 */
public class Result extends HashMap<String,Object>{
    private static final long serialVersionUID = 1L;

    public static final String CODE_KEY = "code";
    public static final String SUCCESS_KEY = "success";
    public static final String MSG_KEY = "msg";
    private static final String RESULT_KEY = "results";

    public static final Integer SUCCESS = 0;
    public static final Integer ERROR = 9999;

    public Result() {
        this.put(CODE_KEY, SUCCESS);
        this.put(SUCCESS_KEY, true);
        this.put(MSG_KEY, "");
    }

    public static Result error() {
        return error(ERROR, "未知错误");
    }

    public static Result error(String msg) {
        return error(ERROR, msg);
    }

    public static Result error(int code, String msg) {
        Result r = new Result();
        r.put(CODE_KEY, code);
        r.put(MSG_KEY, msg);
        r.put(SUCCESS_KEY,false);
        return r;
    }

    public static Result ok(String msg) {
        Result r = new Result();
        r.put(MSG_KEY, msg);
        return r;
    }

    public static Result ok(Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }

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

    public Result data(Object o) {
        super.put(RESULT_KEY, o);
        return this;
    }

    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public void setMessage(String formatMessage) {
        this.put(MSG_KEY, formatMessage);
    }

    public String getMessage() {
        return (String)this.get(MSG_KEY);
    }

    public void setCode(int code) {
        this.put(CODE_KEY, code);
    }

    public int getCode() {
        return (Integer)this.get(CODE_KEY);
    }

    public Object getData() {
        return this.get(RESULT_KEY);
    }

    public boolean isSuccess() {
        return (boolean) this.get(SUCCESS_KEY);
    }

}