private static void setAnchor(List<Map.Entry> list, PagingPredicate pagingPredicate, int nearestPage) { if (list.isEmpty()) { return; } int size = list.size(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); for (int i = pageSize; i <= size && nearestPage < page; i += pageSize) { Map.Entry anchor = list.get(i - 1); nearestPage++; PagingPredicateAccessor.setAnchor(pagingPredicate, nearestPage, anchor); } }
private static void setAnchor(List<Map.Entry> list, PagingPredicate pagingPredicate, int nearestPage) { if (list.isEmpty()) { return; } int size = list.size(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); for (int i = pageSize; i <= size && nearestPage < page; i += pageSize) { Map.Entry anchor = list.get(i - 1); nearestPage++; PagingPredicateAccessor.setAnchor(pagingPredicate, nearestPage, anchor); } }
public static List<QueryableEntry> getSortedSubList(List<QueryableEntry> list, PagingPredicate pagingPredicate, Map.Entry<Integer, Map.Entry> nearestAnchorEntry) { if (pagingPredicate == null || list.isEmpty()) { return list; } Comparator<QueryableEntry> comparator = newComparator(pagingPredicate); Collections.sort(list, comparator); int nearestPage = nearestAnchorEntry.getKey(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); long totalSize = pageSize * ((long) page - nearestPage); if (list.size() > totalSize) { // it's safe to cast totalSize back to int here since it's limited by the list size list = list.subList(0, (int) totalSize); } return list; }
public static List<QueryableEntry> getSortedSubList(List<QueryableEntry> list, PagingPredicate pagingPredicate, Map.Entry<Integer, Map.Entry> nearestAnchorEntry) { if (pagingPredicate == null || list.isEmpty()) { return list; } Comparator<QueryableEntry> comparator = newComparator(pagingPredicate); Collections.sort(list, comparator); int nearestPage = nearestAnchorEntry.getKey(); int pageSize = pagingPredicate.getPageSize(); int page = pagingPredicate.getPage(); long totalSize = pageSize * ((long) page - nearestPage); if (list.size() > totalSize) { // it's safe to cast totalSize back to int here since it's limited by the list size list = list.subList(0, (int) totalSize); } return list; }
@SuppressWarnings("unchecked") public static ResultSet getSortedQueryResultSet(List<Map.Entry> list, PagingPredicate pagingPredicate, IterationType iterationType) { if (list.isEmpty()) { return new ResultSet(); } Comparator<Map.Entry> comparator = SortingUtil.newComparator(pagingPredicate.getComparator(), iterationType); Collections.sort(list, comparator); Map.Entry<Integer, Map.Entry> nearestAnchorEntry = getNearestAnchorEntry(pagingPredicate); int nearestPage = nearestAnchorEntry.getKey(); int page = pagingPredicate.getPage(); int pageSize = pagingPredicate.getPageSize(); long begin = pageSize * ((long) page - nearestPage - 1); int size = list.size(); if (begin > size) { return new ResultSet(); } long end = begin + pageSize; if (end > size) { end = size; } setAnchor(list, pagingPredicate, nearestPage); // it's safe to cast begin and end back to int here since they are limited by the list size List<Map.Entry> subList = list.subList((int) begin, (int) end); return new ResultSet(subList, iterationType); }
@SuppressWarnings("unchecked") public static ResultSet getSortedQueryResultSet(List<Map.Entry> list, PagingPredicate pagingPredicate, IterationType iterationType) { if (list.isEmpty()) { return new ResultSet(); } Comparator<Map.Entry> comparator = SortingUtil.newComparator(pagingPredicate.getComparator(), iterationType); Collections.sort(list, comparator); Map.Entry<Integer, Map.Entry> nearestAnchorEntry = getNearestAnchorEntry(pagingPredicate); int nearestPage = nearestAnchorEntry.getKey(); int page = pagingPredicate.getPage(); int pageSize = pagingPredicate.getPageSize(); long begin = pageSize * ((long) page - nearestPage - 1); int size = list.size(); if (begin > size) { return new ResultSet(); } long end = begin + pageSize; if (end > size) { end = size; } setAnchor(list, pagingPredicate, nearestPage); // it's safe to cast begin and end back to int here since they are limited by the list size List<Map.Entry> subList = list.subList((int) begin, (int) end); return new ResultSet(subList, iterationType); }