package com.doctor.beaver.domain.result;

/**
 * 接口返回带数据的返回值，如果接口调用成功，success为true而data可以为null或非null，业务的成功与否和T关系密切。
 * 
 * @author sdcuike
 *         <p>
 *         Created on 2016年9月29日
 *         <p>
 */
public class ModelResult<T> extends BaseResult {

    private static final long serialVersionUID = -1645782922380055867L;

    /**
     * success为true时，data可能为null
     */
    private T                 data;
    private boolean           readFromCache    = false;

    public ModelResult() {
    }

    @SuppressWarnings("unchecked")
    public <SubClass extends ModelResult> SubClass withModel(T model) {
        data = model;
        return (SubClass) this;
    }

    @SuppressWarnings("unchecked")
    public <SubClass extends ModelResult> SubClass withModelFromCache(T model) {
        data = model;
        readFromCache = true;
        return (SubClass) this;
    }

    @SuppressWarnings("unchecked")
    public <SubClass extends ModelResult> SubClass withModelFromDb(T model) {
        data = model;
        return (SubClass) this;
    }

    public T getData() {
        return data;
    }

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

    public boolean isReadFromCache() {
        return readFromCache;
    }

    public void setReadFromCache(boolean readFromCache) {
        this.readFromCache = readFromCache;
    }

    @Override
    public String toString() {
        return "ModelResult [data=" + data + ", readFromCache=" + readFromCache
                + ", returnCode=" + getReturnCode() + ", returnMsg=" + getReturnMsg()
                + ", inputParamWhereFalse=" + getInputParamWhereFalse() + "]";
    }

}

@@ -206,16 +206,22 @@
 %0Apublic 
+final 
 class Mo
@@ -512,89 +512,29 @@
     
-@SuppressWarnings(%22unchecked%22)%0A    public %3CSubClass extends ModelResult%3E SubClass
+public ModelResult%3CT%3E
  wit
@@ -583,35 +583,24 @@
       return
- (SubClass)
  this;%0A    %7D
@@ -609,67 +609,14 @@
     
-@SuppressWarnings(%22unchecked%22)%0A    public %3CSubClass extends
+public
  Mod
@@ -619,34 +619,27 @@
  ModelResult
-%3E SubClass
+%3CT%3E
  withModelFr
@@ -719,35 +719,24 @@
       return
- (SubClass)
  this;%0A    %7D
@@ -745,67 +745,14 @@
     
-@SuppressWarnings(%22unchecked%22)%0A    public %3CSubClass extends
+public
  Mod
@@ -763,18 +763,11 @@
 sult
-%3E SubClass
+%3CT%3E
  wit
@@ -830,19 +830,8 @@
 turn
- (SubClass)
  thi
@@ -1442,10 +1442,180 @@
 %0A    %7D%0A%0A
+    public static void main(String%5B%5D args) %7B%0A        ModelResult%3CObject%3E modelResult = new ModelResult%3C%3E();%0A        modelResult.withReturnCodeAndReturnMsg(%22%22, %22%22);%0A    %7D%0A
 %7D%0A

优化 ModelResult类