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

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

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

public abstract class AbstractCacheRequestResponse<Request extends ICacheRequest,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 ISearchTransfer<String, Response> getToResponseTransfer(){
        return v->{
            Type superClass = getClass().getGenericSuperclass();
            Type type = ((ParameterizedType) superClass).getActualTypeArguments()[1];
            return JSON.parseObject(v,type);
        };
    }

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

}