/** * @return True if results are from the first available page. */ public boolean isOnFirstPage() { return page.getStartIndex() == 0; }
/** * Counted against total number of results available in a data source. * Counting starts from 1 not 0. * * @return A page number of current loaded paged results. */ public static int getCurrentPageNumber(Page page) { return page.getStartIndex() / page.getPageSize() + 1; }
@Override protected void fetch(Page page) { list.get(page.getStartIndex(), callback); }
/** * @return Index of the last result in cached paged results. */ protected int getEndIndex() { return pagedResult.getPage().getStartIndex() + pagedResult.getResult().size() - 1; }
/** * @return True if results are from the last available page. */ public boolean isOnLastPage() { int pageSize = page.getPageSize(); int lastIndex = totalCount - 1; return PagedList.getNearestIndexToPageSize(lastIndex, pageSize) == page.getStartIndex(); }
/** * @param index * Index of requested result within range of total available * number of results in data source. * @return True if index is out of cached paged result range. */ protected boolean isOutOfCachedPagedResult(int index) { return !(index >= getPagedResult().getPage().getStartIndex() && index <= getEndIndex()); }
/** * Go to next page. */ public void next() { int currentIndex = page.getStartIndex(); fetch(new Page(currentIndex + resultSize, page.getPageSize())); }
/** * Go to previous page. */ public void previous() { int currentIndex = page.getStartIndex(); currentIndex -= page.getPageSize(); currentIndex = (currentIndex < 0 ? 0 : currentIndex); fetch(new Page(currentIndex, page.getPageSize())); }
private RootFindCommand<? extends T> applyPageSize(RootFindCommand<? extends T> findCommand, Page page) { if (page.getPageSize() != Page.ALL_RESULTS) { findCommand.startFrom(page.getStartIndex()); findCommand.maximumResults(page.getPageSize()); } return findCommand; }
/** * Returns element directly if it is within fetched (cached) page, else throw exception. * * @see java.util.List#get(int) */ public E get(int index) throws IndexOutOfBoundsException { if (index < 0) throw new IndexOutOfBoundsException("Index (" + index + ") shouldn't be less than 0"); if (isOutOfCachedPagedResult(index)) { throw new IndexOutOfBoundsException( "Index should be within range <" + getPagedResult().getPage().getStartIndex() + " ; " + getEndIndex() + ">"); } int fetchIndex = index % getPagedResult().getPage().getPageSize(); // System.out.println("get " + index); return getPagedResult().getResult().get(fetchIndex); }
@SuppressWarnings("unchecked") private List<T> doFindByCriteria(DetachedCriteria criteria, Page page, Set<String> existingAliases, boolean addFilterables, boolean cacheable) { Criteria executable = criteria.getExecutableCriteria((Session) entityManager.getDelegate()); if (existingAliases != null) { if (addFilterables) { enrichCriteriaWithFilterables(page, executable, existingAliases); } // projectables are needed only forexecutable final select, not for // count enrichCriteriaWithProjectables(page, executable, existingAliases); } enrichCriteriaWithSortables(page, executable, existingAliases); executable.setFirstResult(page.getStartIndex()); if (!retrieveAllResults(page)) { // Restrict the selection only when page size has meaningful value. executable.setMaxResults(page.getPageSize()); } executable.setCacheable(cacheable); if (cacheable) { executable.setCacheMode(CacheMode.NORMAL); } List<T> list = setSpecialSortCriteria(executable); if (list != null){ return list; } return executable.list(); }