⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagedlistholder.java

📁 这是一个网上书店
💻 JAVA
字号:
package com.ebookstore.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PagedListHolder implements Serializable {

	public static final int DEFAULT_PAGE_SIZE = 10;// 默认页面大小

	private List source;// 原始数据

	private Date refreshDate;// 数据更新的时间

	private int pageSize = DEFAULT_PAGE_SIZE;

	private int page = 0;// 当前页数

	private boolean newPageSet;// 是否有新页面

	public PagedListHolder() {
		this(new ArrayList(0));
	}

	public PagedListHolder(List source) {
		setSource(source);
	}

	public void setSource(List source) {
		this.source = source;
		this.refreshDate = new Date();
	}

	public List getSource() {
		return source;
	}

	public Date getRefreshDate() {
		return refreshDate;
	}

	/**
	 * 设置页面大小
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		if (pageSize != this.pageSize) {
			this.pageSize = pageSize;
			if (!this.newPageSet) {
				this.page = 0;
			}
		}
	}

	/**
	 * 页面大小
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置当前显示的页面
	 * 
	 * @param page
	 */
	public void setPage(int page) {
		this.page = page;
		this.newPageSet = true;
	}

	/**
	 * 当前页
	 * 
	 * @return
	 */
	public int getPage() {
		this.newPageSet = false;
		if (this.page >= getPageCount()) {
			this.page = getPageCount() - 1;
		}
		return this.page;
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public int getPageCount() {
		float nrOfPages = (float) getSource().size() / getPageSize();
		return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1
				: nrOfPages);
	}

	/**
	 * 是否是首页
	 * 
	 * @return
	 */
	public boolean isFirstPage() {
		return getPage() == 0;
	}

	/**
	 * 是否是尾页
	 * 
	 * @return
	 */
	public boolean isLastPage() {
		return getPage() == getPageCount() - 1;
	}

	/**
	 * 上一页
	 * 
	 */
	public void previousPage() {
		if (!isFirstPage()) {
			this.page--;
		}
	}

	/**
	 * 下一页
	 * 
	 */
	public void nextPage() {
		if (!isLastPage()) {
			this.page++;
		}
	}

	/**
	 * 总记录数
	 * 
	 * @return
	 */
	public int getNrOfElements() {
		return getSource().size();
	}

	/**
	 * 当前页的第一个记录数
	 * 
	 * @return
	 */
	public int getFirstElementOnPage() {
		return (getPageSize() * getPage());
	}

	/**
	 * 当前页最后一个数据数
	 * 
	 * @return
	 */
	public int getLastElementOnPage() {
		int endIndex = getPageSize() * (getPage() + 1);
		return (endIndex > getSource().size() ? getSource().size() : endIndex) - 1;
	}

	/**
	 * 当前页数据
	 * 
	 * @return
	 */
	public List getPageList() {
		return getSource().subList(getFirstElementOnPage(),
				getLastElementOnPage() + 1);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -