/** * Checks if it is the last possible page. * @return true if doing a request with an incremented offset parameter would cause an empty result otherwise false. */ default boolean isLast() { if (getOffset() == null || getTotal() == null) { throw new UnsupportedOperationException("Can only be used if the offset & total are known."); } //currently counting the total amount is performed in a second database call, so it is possible //that the left side can be greater than total return getOffset() + getCount() >= getTotal(); } }
/** * Calculates the page number of the result, the pages are indexed staring 0, which means that {@code getPageIndex()} * returns a value in [0,n) , given 'n' is the total number of pages * * @return the page number of the result */ default Long getPageIndex() { if (getTotal() == null || getLimit() == null || getLimit() == 0) { throw new UnsupportedOperationException("Can only be used if the limit & total are known and limit is non-zero."); } return (long) Math.floor(getOffset() / getLimit().doubleValue()); }
/** * Calculates the total number of pages matching the request. * * @return the total number of pages */ default Long getTotalPages() { if (getTotal() == null || getLimit() == null || getLimit() == 0) { throw new UnsupportedOperationException("Can only be used if the limit & total are known and limit is non-zero."); } return (long) Math.ceil(getTotal() / getLimit().doubleValue()); }
/** * Tries to access the first element of the result list. * Use case: query by slug which should contain zero or one element in the result list. * @return the first value or absent */ default Optional<T> head() { final List<T> results = getResults(); return results.isEmpty() ? Optional.empty() : Optional.of(results.get(0)); }
protected void fillPages(final PaginationBean bean, final PagedResult<?> searchResult, final Pagination pagination, final int pageSize) { final int displayedPages = configuration.getInt("pop.pagination.displayedPages", 6); final long totalPages = calculateTotalPages(searchResult.getTotal(), pageSize); final long thresholdLeft = displayedPages - 1; final long thresholdRight = totalPages - displayedPages + 2; if (totalPages <= displayedPages) { fillWithAllPages(bean, totalPages, pagination); } else if (pagination.getPage() < thresholdLeft) { fillWithLastAndRestPages(bean, totalPages, thresholdLeft, pagination); } else if (pagination.getPage() > thresholdRight) { fillWithFirstAndRestPages(bean, totalPages, thresholdRight, pagination); } else { fillWithFirstLastAndMiddlePages(bean, totalPages, pagination); } }
/** * Checks if this is the first page of a result. * @return true if offset is 0 otherwise false */ public boolean isFirst() { return getOffset() == 0; }
protected void fillNextUrl(final PaginationBean bean, final PagedResult<?> searchResult, final Pagination pagination) { if (!searchResult.isLast()) { bean.setNextUrl(buildUrlWithPage(pagination.getKey(), pagination.getPage() + 1)); } }
protected void fillPreviousUrl(final PaginationBean bean, final PagedResult<?> searchResult, final Pagination pagination) { if (!searchResult.isFirst()) { bean.setPreviousUrl(buildUrlWithPage(pagination.getKey(), pagination.getPage() - 1)); } }
/** * Tries to access the first element of the result list. * Use case: query by slug which should contain zero or one element in the result list. * @return the first value or absent */ public Optional<T> head() { return ListUtils.headOption(getResults()); }
/** * Gets from a {@link PagedResult} the last ID if the result is not the last page. * This does only work correctly if it was sorted by ID in ascending order. * @param pagedResult the result to extract the id * @return the last ID, if present */ private <T extends Identifiable<T>> String getIdForNextQuery(final PagedResult<T> pagedResult) { final List<T> results = pagedResult.getResults(); final int indexLastElement = results.size() - 1; return results.get(indexLastElement).getId(); } }