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.litongjava.tio.utils.page.PageUtils Maven / Gradle / Ivy
package com.litongjava.tio.utils.page;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.litongjava.tio.utils.convert.Converter;
import com.litongjava.tio.utils.lock.SetWithLock;
/**
* @author tanyaowu
* 2017年5月10日 下午1:14:15
*/
public class PageUtils {
@SuppressWarnings("unused")
private static Logger log = LoggerFactory.getLogger(PageUtils.class);
@SuppressWarnings("unchecked")
public static Page fromList(List list, int pageNumber, int pageSize) {
// if (list == null) {
// return null;
// }
//
// Page page = pre(list, pageNumber, pageSize);
//
// List pageData = page.getList();
// if (pageData == null) {
// return (Page)page;
// }
//
// int startIndex = Math.min((page.getPageNumber() - 1) * page.getPageSize(), list.size());
// int endIndex = Math.min(page.getPageNumber() * page.getPageSize(), list.size());
//
// for (int i = startIndex; i < endIndex; i++) {
// pageData.add(list.get(i));
// }
// page.setList(pageData);
// return (Page)page;
return fromList((List) list, pageNumber, pageSize, (Converter) (null));
}
@SuppressWarnings("unchecked")
public static Page fromList(List> list, int pageNumber, int pageSize, Converter converter) {
if (list == null) {
return null;
}
Page page = pre(list, pageNumber, pageSize);
List pageData = page.getList();
if (pageData == null) {
return (Page) page;
}
int startIndex = Math.min((page.getPageNumber() - 1) * page.getPageSize(), list.size());
int endIndex = Math.min(page.getPageNumber() * page.getPageSize(), list.size());
for (int i = startIndex; i < endIndex; i++) {
if (converter != null) {
pageData.add(converter.convert(list.get(i)));
} else {
pageData.add(list.get(i));
}
}
page.setList(pageData);
return (Page) page;
}
@SuppressWarnings("unchecked")
public static Page fromSet(Set set, int pageNumber, int pageSize) {
// if (set == null) {
// return null;
// }
//
// Page page = pre(set, pageNumber, pageSize);
//
// List pageData = page.getList();
// if (pageData == null) {
// return page;
// }
//
// int startIndex = Math.min((page.getPageNumber() - 1) * page.getPageSize(), set.size());
// int endIndex = Math.min(page.getPageNumber() * page.getPageSize(), set.size());
//
// int i = 0;
// for (T t : set) {
// if (i >= endIndex) {
// break;
// }
// if (i < startIndex) {
// i++;
// continue;
// }
//
// pageData.add(t);
// i++;
// continue;
// }
// page.setList(pageData);
// return page;
return fromSet((Set) set, pageNumber, pageSize, (Converter) null);
}
@SuppressWarnings("unchecked")
public static Page fromSet(Set> set, int pageNumber, int pageSize, Converter converter) {
if (set == null) {
return null;
}
Page page = pre(set, pageNumber, pageSize);
List pageData = page.getList();
if (pageData == null) {
return (Page) page;
}
int startIndex = Math.min((page.getPageNumber() - 1) * page.getPageSize(), set.size());
int endIndex = Math.min(page.getPageNumber() * page.getPageSize(), set.size());
int i = 0;
for (Object t : set) {
if (i >= endIndex) {
break;
}
if (i < startIndex) {
i++;
continue;
}
if (converter != null) {
pageData.add(converter.convert(t));
} else {
pageData.add(t);
}
i++;
continue;
}
page.setList(pageData);
return (Page) page;
}
@SuppressWarnings("unchecked")
public static Page fromSetWithLock(SetWithLock setWithLock, int pageNumber, int pageSize) {
// if (setWithLock == null) {
// return null;
// }
// Lock lock = setWithLock.readLock();
// lock.lock();
// try {
// Set set = setWithLock.getObj();
// return fromSet(set, pageNumber, pageSize);
// } finally {
// lock.unlock();
// }
return fromSetWithLock((SetWithLock) setWithLock, pageNumber, pageSize, (Converter) null);
}
public static Page fromSetWithLock(SetWithLock> setWithLock, int pageNumber, int pageSize,
Converter converter) {
if (setWithLock == null) {
return null;
}
Lock lock = setWithLock.readLock();
lock.lock();
try {
@SuppressWarnings("unchecked")
Set set = (Set) setWithLock.getObj();
return fromSet(set, pageNumber, pageSize, converter);
} finally {
lock.unlock();
}
}
private static Page pre(java.util.Collection> allList, int pageNumber, int pageSize) {
if (allList == null) {
return new Page<>(null, pageNumber, pageSize, 0);
}
pageSize = processPageSize(pageSize);
pageNumber = processpageNumber(pageNumber);
int recordCount = allList.size();
if (pageSize > recordCount) {
pageSize = recordCount;
}
List pageData = new ArrayList<>(pageSize);
Page ret = new Page<>(pageData, pageNumber, pageSize, recordCount);
return ret;
}
// private static Page pre(java.util.Collection> list, int pageNumber, int pageSize, Converter converter) {
// if (list == null) {
// return new Page<>(null, pageNumber, pageSize, 0, converter);
// }
//
// pageSize = processPageSize(pageSize);
// pageNumber = processpageNumber(pageNumber);
//
// int recordCount = list.size();
// if (pageSize > recordCount) {
// pageSize = recordCount;
// }
//
// List pageData = new ArrayList<>(pageSize);
// Page ret = new Page<>(pageData, pageNumber, pageSize, recordCount, converter);
// return ret;
// }
private static int processpageNumber(int pageNumber) {
return pageNumber <= 0 ? 1 : pageNumber;
}
private static int processPageSize(int pageSize) {
return pageSize <= 0 ? Integer.MAX_VALUE : pageSize;
}
}