mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
5db99b05cb
@ -245,7 +245,7 @@ public class ListUtil {
|
||||
int resultSize = list.size();
|
||||
// 每页条目数大于总数直接返回所有
|
||||
if (resultSize <= pageSize) {
|
||||
if (pageNo < 1) {
|
||||
if (pageNo < (PageUtil.getFirstPageNo()+1)) {
|
||||
return Collections.unmodifiableList(list);
|
||||
} else {
|
||||
// 越界直接返回空
|
||||
@ -253,7 +253,7 @@ public class ListUtil {
|
||||
}
|
||||
}
|
||||
// 相乘可能会导致越界 临时用long
|
||||
if (((long) pageNo * pageSize) > resultSize) {
|
||||
if (((long) (pageNo-PageUtil.getFirstPageNo()) * pageSize) > resultSize) {
|
||||
// 越界直接返回空
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.date.StopWatch;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.PageUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@ -53,4 +54,53 @@ public class ListUtilTest {
|
||||
final int[] indexArray2 = ListUtil.indexOfAll(a, "1"::equals);
|
||||
Assert.assertArrayEquals(new int[]{0,6}, indexArray2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pageTest(){
|
||||
|
||||
|
||||
List<Integer> a = ListUtil.toLinkedList(1, 2, 3,4,5);
|
||||
|
||||
PageUtil.setFirstPageNo(1);
|
||||
int[] a_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] a1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] a2 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] a3 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] a4 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
Assert.assertArrayEquals(new int[]{1,2},a_1);
|
||||
Assert.assertArrayEquals(new int[]{1,2},a1);
|
||||
Assert.assertArrayEquals(new int[]{3,4},a2);
|
||||
Assert.assertArrayEquals(new int[]{5},a3);
|
||||
Assert.assertArrayEquals(new int[]{},a4);
|
||||
|
||||
|
||||
PageUtil.setFirstPageNo(2);
|
||||
int[] b_1 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] b1 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] b2 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] b3 = ListUtil.page(4,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] b4 = ListUtil.page(5,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
Assert.assertArrayEquals(new int[]{1,2},b_1);
|
||||
Assert.assertArrayEquals(new int[]{1,2},b1);
|
||||
Assert.assertArrayEquals(new int[]{3,4},b2);
|
||||
Assert.assertArrayEquals(new int[]{5},b3);
|
||||
Assert.assertArrayEquals(new int[]{},b4);
|
||||
|
||||
PageUtil.setFirstPageNo(0);
|
||||
int[] c_1 = ListUtil.page(-1,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] c1 = ListUtil.page(0,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] c2 = ListUtil.page(1,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] c3 = ListUtil.page(2,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
int[] c4 = ListUtil.page(3,2,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
Assert.assertArrayEquals(new int[]{1,2},c_1);
|
||||
Assert.assertArrayEquals(new int[]{1,2},c1);
|
||||
Assert.assertArrayEquals(new int[]{3,4},c2);
|
||||
Assert.assertArrayEquals(new int[]{5},c3);
|
||||
Assert.assertArrayEquals(new int[]{},c4);
|
||||
|
||||
|
||||
PageUtil.setFirstPageNo(1);
|
||||
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user