功能:
分页接口
源码:
package com.dotions.utils;
/**
* <p> 分页接口 </p>
* @author Dotions 2017-05-02
*/
public interface Pageable {
/**
* 页号
* */
int getPageNumber();
/**
* 页大小
* */
int getPageSize();
/**
* 总页数
* */
int getTotalPages();
/**
* 总元素数
* */
int getTotalElements();
/**
* 是否有下一页
* */
boolean hasNext();
/**
* 下一页
* */
Pageable next();
}
功能:
分页对象
源码:
package com.dotions.utils;
import java.util.Collections;
import java.util.List;
/**
* <p>
* 分页
* </p>
*
* @author dotions 2017-05-02
*/
public class Page<T> implements Pageable {
private int pageNo = 1;
private int pageSize = 50;
private int totalElements = 0;
private List<T> elements = Collections.emptyList();
public Page() {
super();
}
public Page(int totalElements) {
super();
this.totalElements = totalElements;
}
@Override
public int getPageNumber() {
return pageNo;
}
@Override
public int getPageSize() {
return pageSize;
}
@Override
public int getTotalPages() {
return totalElements / pageSize + 1;
}
@Override
public int getTotalElements() {
return totalElements;
}
public int getStart() {
return (getPageNumber() - 1) * getPageSize();
}
public int getEnd() {
return getPageNumber() * getPageSize();
}
public void setPageNo(int pageNo) {
if (pageNo > 0 && pageNo <= getTotalPages()) {
this.pageNo = pageNo;
}
// 设置的页号大于总页数,则将页号设置为最后一页
if (pageNo > 0 && pageNo > getTotalPages()) {
this.pageNo = getTotalPages();
}
}
public void setPageSize(int pageSize) {
if (pageSize > 0) {
this.pageSize = pageSize;
}
}
public void setTotalElements(int totalElements) {
if (totalElements >= 0) {
this.totalElements = totalElements;
}
}
public void setElements(List<T> elements) {
this.elements = elements;
}
public List<T> getElements() {
return elements;
}
@Override
public boolean hasNext() {
return getTotalElements() - getPageNumber() * getPageSize() > 0;
}
@Override
public Page<T> next() {
this.setPageNo(pageNo + 1);
this.elements = Collections.emptyList();
return this;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Page [pageNo=");
builder.append(pageNo);
builder.append(", pageSize=");
builder.append(pageSize);
builder.append(", totalElements=");
builder.append(totalElements);
builder.append(", elements=");
builder.append(elements);
builder.append(", hasNext()=");
builder.append(hasNext());
builder.append("]");
return builder.toString();
}
}
网友评论