AbstractCacheRequestResponse.java 1.31 KB
package com.yoho.search.cache.model;

import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.Transfer;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class AbstractCacheRequestResponse<Request extends ICacheRequest,Response> extends AbstractCacheResponse<Response> {

    private Request request;
    private Response response;
    private boolean needRecache = false;

    public AbstractCacheRequestResponse(Request request){
        this.request = request;
    }

    public Request getRequest() {
        return request;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response,boolean needRecache) {
        this.response = response;
        this.needRecache = needRecache;
    }

    public boolean isNeedRecache() {
        return needRecache;
    }

    public Transfer<String, Response> getToResponseTransfer(){
        return v->{
            Type superClass = getClass().getGenericSuperclass();
            Type type = ((ParameterizedType) superClass).getActualTypeArguments()[1];
            return JSON.parseObject(v,type);
        };
    }

    public Transfer<Response, String> getFromResponseTransfer(){
        return (v)-> JSON.toJSONString(v);
    }

}