@SuppressWarnings({"unchecked", "rawtypes"})
protected Object createPage(Iterable<?> result, Pageable pageable, Long count, boolean isPageQuery) {
final List resultList = IteratorUtil.addToCollection(result, new ArrayList());
if (pageable==null) {
return isPageQuery ? new PageImpl(resultList) : new SliceImpl(resultList);
}
int pageSize = pageable.getPageSize();
int requestedCountStart = pageable.getOffset();
int resultSize = resultList.size();
int currentTotal;
if (count!=null) {
currentTotal = count.intValue();
} else {
if (resultSize == pageSize) currentTotal = requestedCountStart + pageSize;
else currentTotal = requestedCountStart + resultSize;
}
int resultWindowSize = Math.min(resultSize, pageSize);
boolean hasNext = resultWindowSize < resultSize;
List resultListPage = resultList.subList(0, resultWindowSize);
return isPageQuery ?
new PageImpl(resultListPage, pageable, currentTotal) :
new SliceImpl(resultListPage,pageable, hasNext);
}