package com.wechat.common.utils;

import java.util.Collections;
import java.util.List;

/**
 * 集合分页工具类
 * 
 * @author zhouyd
 *
 */
public class ListPageUtil<T> {

	private List<T> data; // 原集合

	private int nowPage; // 当前页

	private int pageSize; // 每页条数

	private int totalPage; // 总页数

	private int totalCount; // 总条数

	public ListPageUtil(List<T> data, int nowPage, int pageSize) {
		this.data = data;
		this.pageSize = pageSize;
		this.nowPage = nowPage;
		this.totalCount = data.size();
		this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
	}

	/**
	 * 得到分页后的数据
	 *
	 * @param pageNum
	 *            页码
	 * @return 分页后结果
	 */
	public List<T> getPagedList() {
		int fromIndex = (nowPage - 1) * pageSize;
		if (fromIndex >= data.size()) {
			return Collections.emptyList();// 空数组
		}
		if (fromIndex < 0) {
			return Collections.emptyList();// 空数组
		}
		int toIndex = nowPage * pageSize;
		if (toIndex >= data.size()) {
			toIndex = data.size();
		}
		return data.subList(fromIndex, toIndex);
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

}
