From c4a91615b4537378d4bc2691d62d3b85e426d6da Mon Sep 17 00:00:00 2001 From: "haochen.li" Date: Wed, 21 Oct 2020 21:38:43 +0800 Subject: [PATCH] =?UTF-8?q?ListUtil=E7=9A=84page=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8PageUtil.setFirstPageNo(),=E5=A6=82=E6=9E=9Cf?= =?UTF-8?q?irstPageNo=E4=B8=8D=E6=98=AF=E4=BB=8E0=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E4=BC=9A=E5=8F=91=E7=94=9F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/ListUtil.java | 2 +- .../hutool/core/collection/ListUtilTest.java | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index 614d40ca7..73641248a 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/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); } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index d0d8eae85..c8d3a0931 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/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 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); + } }