分頁物件Page
阿新 • • 發佈:2019-01-29
import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * 分頁資訊 第一頁從1開始 * */ public class Page<T> implements Serializable, Iterable<T> { protected List<T> result; protected int pageSize=10; protected int pageNumber=1; /** * 總記錄數 */ protected long totalCount = 0; protected boolean isPagination = true; private int pageNum;//總頁數 private int startIndex; private int limit; public int getPageNum() { int totalPageNum = (int) ((totalCount % pageSize == 0) ? totalCount / pageSize : totalCount / pageSize + 1); return totalPageNum; } public Page() { } public Page(int pageNumber, int pageSize, long totalCount) { this(pageNumber, pageSize, totalCount, new ArrayList(0)); } public Page(int pageNumber, int pageSize, long totalCount, List<T> result) { if (pageSize <= 0) throw new IllegalArgumentException("[pageSize] must great than zero"); this.pageSize = pageSize; this.pageNumber = PageUtils.computePageNumber(pageNumber, pageSize, totalCount); this.totalCount = totalCount; setResult(result); } public void setResult(List<T> elements) { if (elements == null) throw new IllegalArgumentException("'result' must be not null"); this.result = elements; } /** * 當前頁包含的資料 * * @return 當前頁資料來源 */ public List<T> getResult() { return result; } /** * 是否是首頁(第一頁),第一頁頁碼為1 * * @return 首頁標識 */ public boolean isFirstPage() { return getThisPageNumber() == 1; } /** * 是否是最後一頁 * * @return 末頁標識 */ public boolean isLastPage() { return getThisPageNumber() >= getLastPageNumber(); } /** * 是否有下一頁 * * @return 下一頁標識 */ public boolean isHasNextPage() { return getLastPageNumber() > getThisPageNumber(); } /** * 是否有上一頁 * * @return 上一頁標識 */ public boolean isHasPreviousPage() { return getThisPageNumber() > 1; } /** * 獲取最後一頁頁碼,也就是總頁數 * * @return 最後一頁頁碼 */ public int getLastPageNumber() { return PageUtils.computeLastPageNumber(totalCount, pageSize); } /** * 總的資料條目數量,0表示沒有資料 * * @return 總數量 */ public long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; } /** * 獲取當前頁的首條資料的行編碼 * * @return 當前頁的首條資料的行編碼 */ public int getThisPageFirstElementNumber() { return (getThisPageNumber() - 1) * getPageSize() + 1; } /** * 獲取當前頁的末條資料的行編碼 * * @return 當前頁的末條資料的行編碼 */ public long getThisPageLastElementNumber() { int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1; return getTotalCount() < fullPage ? getTotalCount() : fullPage; } /** * 獲取下一頁編碼 * * @return 下一頁編碼 */ public int getNextPageNumber() { return getThisPageNumber() + 1; } /** * 獲取上一頁編碼 * * @return 上一頁編碼 */ public int getPreviousPageNumber() { return getThisPageNumber() - 1; } /** * 每一頁顯示的條目數 * * @return 每一頁顯示的條目數 */ public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 當前頁的頁碼 * * @return 當前頁的頁碼 */ public int getThisPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } /** * 得到用於多頁跳轉的頁碼 * * @return */ public List<Integer> getLinkPageNumbers() { return PageUtils.generateLinkPageNumbers(getThisPageNumber(), getLastPageNumber(), 10); } /** * 得到資料庫的第一條記錄號 * * @return */ public int getFirstResult() { return PageUtils.getFirstResult(pageNumber, pageSize); } public Iterator<T> iterator() { return (Iterator<T>) (result == null ? Collections.emptyList().iterator() : result.iterator()); } public boolean isPagination() { return isPagination; } public void setPagination(boolean isPagination) { this.isPagination = isPagination; } }
import java.util.List; public class PageUtils { private PageUtils() { } public static int getFirstResult(int pageNumber, int pageSize) { if (pageSize <= 0) throw new IllegalArgumentException("[pageSize] must great than zero"); return (pageNumber - 1) * pageSize; } public static List<Integer> generateLinkPageNumbers(int currentPageNumber, int lastPageNumber, int count) { int avg = count / 2; int startPageNumber = currentPageNumber - avg; if (startPageNumber <= 0) { startPageNumber = 1; } int endPageNumber = startPageNumber + count - 1; if (endPageNumber > lastPageNumber) { endPageNumber = lastPageNumber; } if (endPageNumber - startPageNumber < count) { startPageNumber = endPageNumber - count; if (startPageNumber <= 0) { startPageNumber = 1; } } java.util.List<Integer> result = new java.util.ArrayList(); for (int i = startPageNumber; i <= endPageNumber; i++) { result.add(new Integer(i)); } return result; } public static int computeLastPageNumber(long totalElements, int pageSize) { int result = (int) (totalElements % pageSize == 0 ? totalElements / pageSize : totalElements / pageSize + 1); if (result <= 1) result = 1; return result; } public static int computePageNumber(int pageNumber, int pageSize, long totalElements) { if (pageNumber <= 1) { return 1; } if (Integer.MAX_VALUE == pageNumber || pageNumber > computeLastPageNumber(totalElements, pageSize)) { // last page return computeLastPageNumber(totalElements, pageSize); } return pageNumber; } }