Java Generics: Dealing with an intermediate unknown type
- by Matt
I am trying to figure out a way to deal with a particular problem in a type safe manner, or to put it more specifically without any explicit casts. I have a class that takes in a generic request and returns a generic response like such:
public class RetrievalProcessor<Req extends Request, Resp> implements RequestProcessor<Req, Resp>{
private Dao<Req> dao;
private RawResponseTransformer<Resp> transformer;
@Override
public Resp process(Req request) {
return transformer.transformResponse(dao.retrieveRawResponse(request));
}
}
My problem is the following. My Dao object can be many different things REST, JDBC, some other proprietary object. I can't be certain of the type of object that the Dao will return. I do know the type of object my caller would like and that is the Resp type on the generic, and the job of the RawResponseTransformer is to transform that Dao response into something that the caller can consume. The problem I have is I can't figure out a way that feels clean to do that.
I have considered putting the intermediate type as part of the definition of the class, but it doesn't seem like the caller should know, or really care, what the intermediate form is.
Hoping someone might have a good clean idea for handling this.