I'm having trouble adding generics to a piece of code that I'm working on, I've searched around and none of the examples I've seen so far quite capture what I've trying to do so I'm reaching out for help.
Currently I have this code which processes a list of UserDTO objects that have only their ID's populated and gets the full details of each User from a restful web-service:
//userIds and userDetails declared previously
for (UserDTO user : userIds) {
UserDTO populatedUser = webResource.path(REST_USER_PATH).path(user.getId().
toString()).type(MediaType.APPLICATION_XML).get(ClientResponse.class).
getEntity(UserDTO.class);
userDetails.add(populatedUser);
}
Now this is going to be a pattern for the piece of work I'm currently undertaking, I'll need to be able to convert lists of DTO objects which have just the Id's populated, to lists of fully populated DTO objects by calling a web service. What I'd like to do is create a generic method to let me do this.
All the different DTO objects that I need to do this for extend our BaseDTO so I came up with the following, unfortunately it does not compile but hopefully it will show what I'm trying to accomplish:
public <T extends BaseDTO> getListOfPopulatedDTOs(
List <T extends BaseDTO> unpopulatedDTOs, String restPath) {
List<BaseDTO> populatedDTOs = new ArrayList<BaseDTO>();
for (BaseDTO unpopulatedDTO : unpopulatedDTOs) {
BaseDTO populatedDTO = webResource.path(restPath).path(
unpopulatedDTO.getId().toString()).type(MediaType.APPLICATION_XML).
get(ClientResponse.class).getEntity(T.class);
populatedDTOs.add(populatedDTO);
}
return populatedDTOs;
}
Any help or advice would be gratefully received. Many thanks in advance :)