All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.aidensuen.mongo.pagehelper.Page Maven / Gradle / Ivy
package com.github.aidensuen.mongo.pagehelper;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* thanks https://github.com/pagehelper/
*/
public class Page extends PageRequest implements Closeable {
private static final long serialVersionUID = -1376111841926300921L;
/**
* default sort field
*/
private String ID_NAME = "_id";
/**
* it will skip(0) when this sort field is not null and use ID_NAME to sort
*/
private Object lastId;
/**
* first line
*/
private long startRow;
/**
* last line
*/
private long endRow;
/**
* whether count
*/
private boolean count = true;
/**
* total
*/
private long total;
/**
* total pages
*/
private int pages;
private List result;
private Page(int pageNum, int pageSize, Sort sort) {
super(pageNum, pageSize, sort);
result = new ArrayList<>(0);
}
public static Page of(int[] offset, boolean count) {
return of(offset, count, Sort.unsorted());
}
public static Page of(int[] offset, boolean count, Sort sort) {
int pageNum = 0;
int pageSize;
if (offset[0] == 0 && offset[1] == Integer.MAX_VALUE) {
pageSize = 1;
} else {
pageSize = offset[1];
pageNum = offset[1] != 0 ? (int) (Math.ceil(((double) offset[0] + offset[1]) / offset[1]) - 1) : 0;
}
Page page = of(pageNum, pageSize, count, sort);
return page;
}
public static Page of(int pageNum, int pageSize) {
return of(pageNum, pageSize, true, Sort.unsorted());
}
public static Page of(int pageNum, int pageSize, boolean count) {
return of(pageNum, pageSize, count, Sort.unsorted());
}
public static Page of(int pageNum, int pageSize, Sort sort) {
return of(pageNum, pageSize, true, sort);
}
public static Page of(int pageNum, int pageSize, Object lastId) {
return of(pageNum, pageSize, true, lastId);
}
public static Page of(int pageNum, int pageSize, boolean count, Object lastId) {
Page page = of(pageNum, pageSize, count, Sort.unsorted());
page.setLastId(lastId);
return page;
}
public static Page of(int pageNum, int pageSize, boolean count, Sort sort) {
if (pageNum == 0 && pageSize == Integer.MAX_VALUE) {
pageSize = 1;
}
Page page = new Page(pageNum, pageSize, sort);
page.setCount(count);
page.calculateStartAndEndRow();
return page;
}
private Page calculateStartAndEndRow() {
this.startRow = this.getOffset();
this.endRow = this.startRow + this.getPageSize();
return this;
}
public String getID_NAME() {
return ID_NAME;
}
public Page setID_NAME(String ID_NAME) {
this.ID_NAME = ID_NAME;
return this;
}
public Object getLastId() {
return lastId;
}
public Page setLastId(Object lastId) {
this.lastId = lastId;
return this;
}
public List getResult() {
return result;
}
public Page setResult(List result) {
this.result = result;
return this;
}
public boolean isCount() {
return count;
}
public Page setCount(boolean count) {
this.count = count;
return this;
}
public long getStartRow() {
return startRow;
}
public Page setStartRow(long startRow) {
this.startRow = startRow;
return this;
}
public long getEndRow() {
return endRow;
}
public Page setEndRow(long endRow) {
this.endRow = endRow;
return this;
}
public long getTotal() {
return total;
}
public Page setTotal(long total) {
this.total = total;
if (total == -1) {
pages = 1;
return this;
}
if (getPageSize() > 0) {
pages = (int) (total / getPageSize() + ((total % getPageSize() == 0) ? 0 : 1));
} else {
pages = 0;
}
if (getPageNumber() > pages) {
if (pages != 0) {
Page page = of(pages, getPageSize(), isCount(), getSort());
page.setPages(pages).setTotal(total).setID_NAME(getID_NAME()).setLastId(getLastId()).setResult(getResult()).calculateStartAndEndRow();
return page;
}
return this;
} else {
return this;
}
}
public int getPages() {
return pages;
}
public Page setPages(int pages) {
this.pages = pages;
return this;
}
public PageInfo toPageInfo() {
PageInfo pageInfo = new PageInfo(this.getResult(), this, getTotal());
return pageInfo;
}
@Override
public void close() {
PageHelper.clearPage();
}
}