For Lists.partition() fix for empty list input

Fix such that empty list input produces an empty list
This commit is contained in:
Rob Bygrave
2023-08-21 20:19:59 +12:00
parent e09265fbc5
commit cb06052c86
2 changed files with 5 additions and 3 deletions
+4 -1
View File
@@ -1,6 +1,7 @@
package io.ebean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@@ -24,7 +25,9 @@ public final class Lists {
*/
public static <T> List<List<T>> partition(List<T> source, int max) {
final int totalCount = source.size();
if (totalCount <= max) {
if (totalCount == 0) {
return Collections.emptyList();
} else if (totalCount <= max) {
return List.of(source);
}
final int numOfPartitions = (totalCount + max - 1) / max; // round up
@@ -11,8 +11,7 @@ class ListsTest {
@Test
void partition_empty() {
List<List<Integer>> partitions = Lists.partition(List.of(), 5);
assertThat(partitions).hasSize(1);
assertThat(partitions.get(0)).hasSize(0);
assertThat(partitions).isEmpty();
}
@Test