I have a function defined which returns a generic list of whatever objects which are implementing PaginationData interface
private <T> List<T> paginate(int pageNumber, int pageSize, int totalAvailable, PaginatedResponse paginatedResponse, String url) {
List<T> listOfObjects = new ArrayList();
while (pageNumber * pageSize <= totalAvailable) {
paginatedResponse = sendRequest(url + "?pageNumber=" + pageNumber, paginatedResponse.getClass());
listOfObjects.addAll(paginatedResponse
.getPaginationData()
.getPaginatedObjectsList());
pageNumber++;
}
return listOfObjects;
}
and PaginatedResponse defined like this
public interface PaginatedResponse {
Pagination getPagination();
PaginationData getPaginationData();
}
Where PaginationData is an interface defined
public interface PaginationData<T> {
List<T> getPaginatedObjectsList();
}
sendRequest method looks like this
private <T> T sendRequest(String url, Class<T> clazz) {
HttpResponse httpResponse = httpClient.send(url);
//Jackson's object mapper to transform the result
// but this result can be also other type than the PaginatedResponse
try {
return objectMapper.readValue(httpResponse.getEntity(), clazz);
} catch (IOException e) {
throw new ParsingException(e);
}
}
Everything works fine I'm just getting a warning when invoking .addAll() method on the listOfObjects variable. This warning states : Unchecked assignment, java.util.List to java.util.Collection<? extends T> Reason, paginatedResponse.getPaginationData() has raw type so the result of getPaginatedObjectList() is erased.
So the question is how to get rid of the warning when I'm just returning generic objects wrapped in the list and I'm getting them from the generic paginated response which can be of whatever type which implements PaginatedResponse.