浏览代码

ListUtil的page方法
调用PageUtil.setFirstPageNo(),如果firstPageNo不是从0开始计算会发生错误

haochen.li 5 年之前
父节点
当前提交
c4a91615b4

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java

@@ -253,7 +253,7 @@ public class ListUtil {
 			}
 		}
 		// 相乘可能会导致越界 临时用long
-		if (((long) pageNo * pageSize) > resultSize) {
+		if (((long) (pageNo-PageUtil.getFirstPageNo()) * pageSize) > resultSize) {
 			// 越界直接返回空
 			return new ArrayList<>(0);
 		}

+ 45 - 0
hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java

@@ -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,48 @@ 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);
+	}
 }