From 8523a67e06ac042446ba4a4c6f45786fd9cc058e Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:19:19 +0800 Subject: [PATCH] refactor: logic for subList method when retrieving all data (#5235) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.12.x #### What this PR does / why we need it: 重构 ListResult.subList 方法在获取所有数据时的处理逻辑,只要 size 为 0 就返回所有数据 #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../run/halo/app/extension/ListResult.java | 3 +++ .../halo/app/extension/ListResultTest.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/api/src/main/java/run/halo/app/extension/ListResult.java b/api/src/main/java/run/halo/app/extension/ListResult.java index 3a487959f3..cc64ef9170 100644 --- a/api/src/main/java/run/halo/app/extension/ListResult.java +++ b/api/src/main/java/run/halo/app/extension/ListResult.java @@ -129,6 +129,9 @@ public static ListResult emptyResult() { */ public static List subList(List list, int page, int size) { if (page < 1) { + page = 1; + } + if (size < 1) { return list; } List listSort = new ArrayList<>(); diff --git a/application/src/test/java/run/halo/app/extension/ListResultTest.java b/application/src/test/java/run/halo/app/extension/ListResultTest.java index f707dc8b0b..98a118ec5d 100644 --- a/application/src/test/java/run/halo/app/extension/ListResultTest.java +++ b/application/src/test/java/run/halo/app/extension/ListResultTest.java @@ -43,4 +43,26 @@ void totalPages() { listResult = new ListResult<>(1, 0, 100, List.of()); assertEquals(1, listResult.getTotalPages()); } + + @Test + void subListWhenSizeIsZero() { + var list = List.of(1, 2, 3, 4, 5); + assertSubList(list); + + list = List.of(1); + assertSubList(list); + } + + private void assertSubList(List list) { + var result = ListResult.subList(list, 0, 0); + assertEquals(list, result); + + result = ListResult.subList(list, 0, 1); + assertEquals(list.subList(0, 1), result); + + result = ListResult.subList(list, 1, 0); + assertEquals(list, result); + + assertEquals(list.subList(0, 1), ListResult.subList(list, -1, 1)); + } } \ No newline at end of file