add Segment

This commit is contained in:
Looly 2020-12-08 05:03:48 +08:00
parent 850c766213
commit 7633733cfe
3 changed files with 47 additions and 1 deletions

View File

@ -13,6 +13,7 @@
* 【cache 】 CacheObj的isExpired()逻辑修改issue#1295@Github
* 【json 】 JSONStrFormater改为JSONStrFormatter
* 【dfa 】 增加FoundWordpr#1290@Github
* 【core 】 增加Segmentpr#1290@Github
### Bug修复
* 【cache 】 修复Cache中get重复misCount计数问题issue#1281@Github

View File

@ -1,5 +1,8 @@
package cn.hutool.core.util;
import cn.hutool.core.lang.DefaultSegment;
import cn.hutool.core.lang.Segment;
/**
* 分页工具类
*
@ -135,6 +138,35 @@ public class PageUtil {
return new int[]{start, getEndByStart(start, pageSize)};
}
/**
* 将页数和每页条目数转换为开始位置和结束位置<br>
* 此方法用于包括结束位置的分页方法<br>
* 例如
*
* <pre>
* 页码0每页10 = [0, 10]
* 页码1每页10 = [10, 20]
*
* </pre>
*
* <p>
* {@link #setFirstPageNo(int)}设置为1时
* <pre>
* 页码1每页10 = [0, 10]
* 页码2每页10 = [10, 20]
*
* </pre>
*
* @param pageNo 页码从0计数
* @param pageSize 每页条目数
* @return {@link Segment}
* @since 5.5.3
*/
public static Segment<Integer> toSegment(int pageNo, int pageSize) {
final int[] startEnd = transToStartEnd(pageNo, pageSize);
return new DefaultSegment<>(startEnd[0], startEnd[1]);
}
/**
* 根据总数计算总页数
*

View File

@ -1,5 +1,6 @@
package cn.hutool.db;
import cn.hutool.core.lang.Segment;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.db.sql.Order;
@ -12,7 +13,7 @@ import java.util.Arrays;
*
* @author Looly
*/
public class Page implements Serializable {
public class Page implements Segment<Integer>, Serializable {
private static final long serialVersionUID = 97792549823353462L;
public static final int DEFAULT_PAGE_SIZE = 20;
@ -159,15 +160,27 @@ public class Page implements Serializable {
/**
* @return 开始位置
* @see #getStartIndex()
*/
public int getStartPosition() {
return getStartIndex();
}
@Override
public Integer getStartIndex() {
return PageUtil.getStart(this.pageNumber, this.pageSize);
}
/**
* @return 结束位置
* @see #getEndIndex()
*/
public int getEndPosition() {
return getEndIndex();
}
@Override
public Integer getEndIndex() {
return PageUtil.getEnd(this.pageNumber, this.pageSize);
}