Skip to content

Commit

Permalink
refactor: logic for subList method when retrieving all data
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Jan 23, 2024
1 parent 9d9b152 commit 5068f6c
Show file tree
Hide file tree
Showing 2 changed files with 26 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,27 @@ 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, 0, 0);
assertEquals(list, result);

result = ListResult.subList(list, 1, 0);
assertEquals(list, result);
}
}

0 comments on commit 5068f6c

Please sign in to comment.