Skip to content

Commit

Permalink
refactor: logic for subList method when retrieving all data (#5235)
Browse files Browse the repository at this point in the history
#### 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
```
  • Loading branch information
guqing authored Jan 23, 2024
1 parent 9d9b152 commit 8523a67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/src/main/java/run/halo/app/extension/ListResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public static <T> ListResult<T> emptyResult() {
*/
public static <T> List<T> subList(List<T> list, int page, int size) {
if (page < 1) {
page = 1;
}
if (size < 1) {
return list;
}
List<T> listSort = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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));
}
}

0 comments on commit 8523a67

Please sign in to comment.