support edge

This commit is contained in:
Looly
2020-03-24 17:09:04 +08:00
parent df739bae6a
commit e344c4ef54
8 changed files with 166 additions and 52 deletions

View File

@@ -2128,7 +2128,7 @@ public class CollUtil {
int resultSize = list.size();
// 每页条目数大于总数直接返回所有
if (resultSize <= pageSize) {
if (pageNo <= 1) {
if (pageNo < 1) {
return Collections.unmodifiableList(list);
} else {
// 越界直接返回空

View File

@@ -2,69 +2,144 @@ package cn.hutool.core.util;
/**
* 分页工具类
*
*
* @author xiaoleilu
*
*/
public class PageUtil {
private static int firstPageNo = 0;
/**
* 获得首页的页码可以为0或者1
*
* @return 首页页码
*/
public static int getFirstPageNo() {
return firstPageNo;
}
/**
* 设置首页页码可以为0或者1
*
* <pre>
* 当设置为0时页码0表示第一页开始位置为0
* 当设置为1时页码1表示第一页开始位置为0
* </pre>
*
* @param customFirstPageNo 自定义的首页页码为0或者1
*/
public static void setFirstPageNo(int customFirstPageNo) {
firstPageNo = customFirstPageNo;
}
/**
* 设置首页页码为1
*
* <pre>
* 当设置为1时页码1表示第一页开始位置为0
* </pre>
*/
public static void setOneAsFirstPageNo() {
setFirstPageNo(1);
}
/**
* 将页数和每页条目数转换为开始位置<br>
* 此方法用于不包括结束位置的分页方法<br>
* 例如:
*
*
* <pre>
* 页码0每页10 =》 0
* 页码1每页10 =》 10
* ……
* </pre>
*
* @param pageNo 页码从0计数
*
* <p>
* 当{@link #setFirstPageNo(int)}设置为1时
* <pre>
* 页码1每页10 =》 0
* 页码2每页10 =》 10
* ……
* </pre>
*
* @param pageNo 页码从0计数
* @param pageSize 每页条目数
* @return 开始位置
*/
public static int getStart(int pageNo, int pageSize) {
if (pageNo < 0) {
pageNo = 0;
if (pageNo < firstPageNo) {
pageNo = firstPageNo;
}
if (pageSize < 1) {
pageSize = 0;
}
return pageNo * pageSize;
return (pageNo - firstPageNo) * pageSize;
}
/**
* 将页数和每页条目数转换为结束位置<br>
* 此方法用于不包括结束位置的分页方法<br>
* 例如:
*
* <pre>
* 页码0每页10 =》 9
* 页码1每页10 =》 19
* ……
* </pre>
*
* <p>
* 当{@link #setFirstPageNo(int)}设置为1时
* <pre>
* 页码1每页10 =》 9
* 页码2每页10 =》 19
* ……
* </pre>
*
* @param pageNo 页码从0计数
* @param pageSize 每页条目数
* @return 开始位置
* @since 5.2.5
*/
public static int getEnd(int pageNo, int pageSize) {
final int start = getStart(pageNo, pageSize);
return getEndByStart(start, pageSize);
}
/**
* 将页数和每页条目数转换为开始位置和结束位置<br>
* 此方法用于包括结束位置的分页方法<br>
* 例如:
*
*
* <pre>
* 页码0每页10 =》 [0, 10]
* 页码1每页10 =》 [10, 20]
* ……
* </pre>
*
* @param pageNo 页码从0计数
*
* <p>
* 当{@link #setFirstPageNo(int)}设置为1时
* <pre>
* 页码1每页10 =》 [0, 10]
* 页码2每页10 =》 [10, 20]
* ……
* </pre>
*
* @param pageNo 页码从0计数
* @param pageSize 每页条目数
* @return 第一个数为开始位置,第二个数为结束位置
*/
public static int[] transToStartEnd(int pageNo, int pageSize) {
final int start = getStart(pageNo, pageSize);
if (pageSize < 1) {
pageSize = 0;
}
final int end = start + pageSize;
return new int[] { start, end };
return new int[]{start, getEndByStart(start, pageSize)};
}
/**
* 根据总数计算总页数
*
*
* @param totalCount 总数
* @param pageSize 每页数
* @param pageSize 每页数
* @return 总页数
*/
public static int totalPage(int totalCount, int pageSize) {
@@ -78,13 +153,13 @@ public class PageUtil {
* 分页彩虹算法<br>
* 来自https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java<br>
* 通过传入的信息,生成一个分页列表显示
*
* @param currentPage 当前页
* @param pageCount 总页数
*
* @param pageNo 当前页
* @param totalPage 总页数
* @param displayCount 每屏展示的页数
* @return 分页条
*/
public static int[] rainbow(int currentPage, int pageCount, int displayCount) {
public static int[] rainbow(int pageNo, int totalPage, int displayCount) {
boolean isEven = displayCount % 2 == 0;
int left = displayCount / 2;
int right = displayCount / 2;
@@ -93,22 +168,22 @@ public class PageUtil {
if (isEven) {
right++;
}
if (pageCount < displayCount) {
length = pageCount;
if (totalPage < displayCount) {
length = totalPage;
}
int[] result = new int[length];
if (pageCount >= displayCount) {
if (currentPage <= left) {
if (totalPage >= displayCount) {
if (pageNo <= left) {
for (int i = 0; i < result.length; i++) {
result[i] = i + 1;
}
} else if (currentPage > pageCount - right) {
} else if (pageNo > totalPage - right) {
for (int i = 0; i < result.length; i++) {
result[i] = i + pageCount - displayCount + 1;
result[i] = i + totalPage - displayCount + 1;
}
} else {
for (int i = 0; i < result.length; i++) {
result[i] = i + currentPage - left + (isEven ? 1 : 0);
result[i] = i + pageNo - left + (isEven ? 1 : 0);
}
}
} else {
@@ -123,12 +198,30 @@ public class PageUtil {
/**
* 分页彩虹算法(默认展示10页)<br>
* 来自https://github.com/iceroot/iceroot/blob/master/src/main/java/com/icexxx/util/IceUtil.java
*
*
* @param currentPage 当前页
* @param pageCount 总页数
* @param pageCount 总页数
* @return 分页条
*/
public static int[] rainbow(int currentPage, int pageCount) {
return rainbow(currentPage, pageCount, 10);
}
//------------------------------------------------------------------------- Private method start
/**
* 根据起始位置获取结束位置
*
* @param start 起始位置
* @param pageSize 每页条目数
* @return 结束位置
*/
private static int getEndByStart(int start, int pageSize) {
if (pageSize < 1) {
pageSize = 0;
}
return start + pageSize;
}
//------------------------------------------------------------------------- Private method end
}