package cn.wanda.giffen.model; import java.io.Serializable; import java.util.List; public class ResultPage<T> implements Serializable { private final static int DEFAULT_NAVIGATOR_SIZE = 10 ; // 当前页 private int currentPage = 1 ; // 每页显示数量 private int pageSize = 20 ; //总页数 private int pageCount = 1 ; // 总条数 private int totalCount; private boolean havaNextPage; private boolean havePrePage; private int navigatorSize; // 存放查询结果用的list private List<T> items; public ResultPage() { } public ResultPage( int totalCount, int pageSize, int currentPage) { this (totalCount, pageSize, currentPage, DEFAULT_NAVIGATOR_SIZE); } public ResultPage( int totalCount, int pageSize, int currentPage, int navigatorSize) { this .totalCount = totalCount; this .pageSize = pageSize; this .currentPage = currentPage; this .pageCount = operatorPageCount(); this .navigatorSize = navigatorSize; } public ResultPage( int totalCount, int pageSize, int currentPage, List<T> items) { this .totalCount = totalCount; this .pageSize = pageSize; this .currentPage = currentPage; this .pageCount = operatorPageCount(); this .items = items; } /** * 计算总页数 * @return */ public int operatorPageCount() { int pageCount = 0 ; if (pageSize != 0 ) { pageCount = totalCount / pageSize; if (totalCount % pageSize != 0 ) pageCount++; } return pageCount; } public int getCurrentPage() { currentPage = currentPage < pageCount ? currentPage : pageCount; currentPage = currentPage < 1 ? 1 : currentPage; return currentPage; } public int getPageCount() { return pageCount; } public int getPageSize() { return pageSize; } public int getTotalCount() { return totalCount; } public boolean isHaveNextPage() { havaNextPage = false ; if ((pageCount > 1 ) && (pageCount> getCurrentPage())) havaNextPage = true ; return havaNextPage; } public boolean isHavePrePage() { havePrePage = false ; if ((pageCount > 1 ) && (currentPage > 1 )) havePrePage = true ; return havePrePage; } private int getNavigatorIndex( boolean isBegin) { int beginNavigatorIndex = getCurrentPage() - navigatorSize / 2 ; int endNavigatorIndex = getCurrentPage() + navigatorSize / 2 ; beginNavigatorIndex = beginNavigatorIndex < 1 ? 1 : beginNavigatorIndex; endNavigatorIndex = endNavigatorIndex < pageCount ? endNavigatorIndex : pageCount; while ((endNavigatorIndex - beginNavigatorIndex) < navigatorSize && (beginNavigatorIndex != 1 || endNavigatorIndex != pageCount)) { if (beginNavigatorIndex > 1 ) beginNavigatorIndex--; else if (endNavigatorIndex < pageCount) endNavigatorIndex++; } if (isBegin) return beginNavigatorIndex; else return endNavigatorIndex; } public int getBeginNavigatorIndex() { return getNavigatorIndex( true ); } public int getEndNavigatorIndex() { return getNavigatorIndex( false ); } public List<T> getItems() { return items; } public void setItems(List<T> items) { this .items = items; } public void setCurrentPage( int currentPage) { this .currentPage = currentPage; } public void setPageSize( int pageSize) { this .pageSize = pageSize; } public void setTotalCount( int totalCount) { this .totalCount = totalCount; } public void setPageCount( int pageCount) { this .pageCount = pageCount; } } |