package com.yida.common;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* 将集合中的数据分页显示
*
* @author Administrator
*
*/
@SuppressWarnings("unchecked")
public class CollectionToPatination<T>
{
private Integer pageCount;
private Integer currentPage;
private Integer size;
protected Integer getSize(Integer size)
{
if (null==size || size.intValue() < 1)
{
this.size = 1;
}
else
{
this.size = size;
}
return this.size;
}
public Integer getCurrentPage()
{
return currentPage;
}
private void setCurrentPage(Integer page)
{
// 判断是否在合法范围内,如果不在自动指定
if (page.intValue() > pageCount.intValue())
{
this.currentPage = pageCount;
}
else if (page.intValue() < 1)
{
this.currentPage = 1;
}
else
{
this.currentPage = page;
}
}
public Integer getPageCount()
{
return pageCount;
}
private void setPageCount(Collection coll)
{
this.pageCount = coll.size() % size.intValue() == 0 ? coll.size()
/ size : coll.size() / size + 1;
}
/**
* 根据传来的数据来分页显示数量,使用截取
*
* @param lst
* @param size
* @param page
* 这个必须为正整数
* @return
*/
@SuppressWarnings("unused")
public List<T> parseCollectionToPatination(Collection coll, Integer size,
Integer page)
{
// 不能放乱
this.getSize(size);
this.setPageCount(coll);
this.setCurrentPage(page);
System.out.println("PD"+(page.intValue()>currentPage.intValue())+" cur:"+this.getCurrentPage());
List l = new ArrayList();
// 取得要得到的开始与结束下标
int start = (this.getCurrentPage().intValue() - 1) * size.intValue()
+ 1;
int end = start + size - 1;
int s = coll.size();
// 遍历集合,查出要取得的数量
for (int h = start; h <= end; h++)
{
int idex = h - 1;
try
{
l.add(coll.toArray()[idex]);
}
catch (IndexOutOfBoundsException ie)
{
}
}
return ((List<T>) l);
}
public static void main(String[] args)
{
List l = new ArrayList();
// Set l=new TreeSet();
l.add("唐亮1");
l.add("唐亮4");
l.add("唐亮5");
l.add("唐亮2");
l.add("唐亮6");
l.add("唐亮3");
Collections.sort(l);
System.out.println(l.size());
for (Object o : new CollectionToPatination<Object>()
.parseCollectionToPatination(l, 2, 3))
{
System.out.println(o);
}
}
}