import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
//Iterable 迭代器 Serializable 序列化
public class Page<T> implements Iterable<T>, Serializable {
private static final long serialVersionUID = -3720998571176536865L;//表明类的不同版本间的兼容性
private List<T> content = new ArrayList<>();//声明和实例化ArrayList对象,并指定其类型为泛型。
private long total;//total记录总数=pageSize*totalPages
private int pageNo;//第几页
private int pageSize;//页的大小,即一页能放多少数据
private int totalPages;//总页数
public Page() {//定义构造函数
}
public Page(List<T> content, long total, int pageNo, int pageSize) {
this.content = content;
this.total = total;
this.pageNo = pageNo;
this.pageSize = pageSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
/**
* Returns if there is a previous page.
*
* @return if there is a previous page.
*/
public boolean hasPrevious() {//判断前一页是否存在,存在返回true,反之返回false
return getPageNo() > 0;
}
/**
* Returns if there is a next page.
*
* @return if there is a next page.
*/
public boolean hasNext() {//判断后一页是否存在,存在返回true,反之返回false
return getPageNo() + 1 < getTotalPage();
}
/**
* Returns whether the current page is the first one.
*
* @return whether the current page is the first one.
*/
public boolean isFirst() {//判断当前页是否是第一页
return !hasPrevious();
}
/**
* Returns whether the current page is the last one.
*
* @return whether the current page is the last one.
*/
boolean isLast() {//判断当前页是否是最后一页
return !hasNext();
}
/**
* Returns the total amount of elements of all pages.
*
* @return the total amount of elements of all pages.
*/
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
/**
* Returns the number of total pages.
*
* @return the number of total pages
*/
public int getTotalPage() {//返回总页数
return getPageSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getPageSize());//判断页大小是否等于零,是返回总页数为一,否返回
总页数为总记录数除以页大小;ceil()函数返回大于或等于一个给定数字的最小整数,向上取整。
}
/**
* Returns the page content as unmodifiable {@link List}.
*
* @return Returns the page content as unmodifiable {@link List}
*/
public List<T> getContent() {
return Collections.unmodifiableList(content);
}
public void setContent(List<T> content) {
this.content = content;
}
/**
* Returns whether the current page has content.
*
* @return whether the current page has content.
*/
public boolean hasContent() {//返回当前页面是否包含内容
return getContentSize() > 0;
}
/**
* Returns the number of elements on current page.
*
* @return the number of elements on current page.
*/
public int getContentSize() {
return content.size();
}
/**
* Returns the number of items of each page.
*
* @return the number of items of each page
*/
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* Returns the number of current page. (Zero-based numbering.)
*
* @return the number of current page.
*/
public int getPageNo() {
return pageNo;
}
/**
* Set the number of current page. (Zero-based numbering.)
*/
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
@Override
public Iterator<T> iterator() {
return getContent().iterator();
}
}