Skip to content

Commit 5bb0420

Browse files
committed
Add private constructor and null validation to CollectionUtils.
Add private constructor to prevent instantiation of utility class. Add Assert.notNull validation to enforce method contract documented in Javadoc. Standardize assertion message capitalization for consistency. Signed-off-by: qkrtkdwns3410 <[email protected]>
1 parent dcf45d1 commit 5bb0420

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/CollectionUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
import java.util.List;
1919

20+
import org.springframework.util.Assert;
21+
2022
/**
2123
* Utility methods to obtain sublists.
2224
*
2325
* @author Mark Paluch
26+
* @author qkrtkdwns3410
2427
*/
2528
class CollectionUtils {
2629

30+
private CollectionUtils() {
31+
// prevent instantiation
32+
}
33+
2734
/**
2835
* Return the first {@code count} items from the list.
2936
*
@@ -33,6 +40,7 @@ class CollectionUtils {
3340
* @param <T> the element type of the lists.
3441
*/
3542
public static <T> List<T> getFirst(int count, List<T> list) {
43+
Assert.notNull(list, "List must not be null");
3644

3745
if (count > 0 && list.size() > count) {
3846
return list.subList(0, count);
@@ -50,9 +58,10 @@ public static <T> List<T> getFirst(int count, List<T> list) {
5058
* @param <T> the element type of the lists.
5159
*/
5260
public static <T> List<T> getLast(int count, List<T> list) {
61+
Assert.notNull(list, "List must not be null");
5362

5463
if (count > 0 && list.size() > count) {
55-
return list.subList(list.size() - (count), list.size());
64+
return list.subList(list.size() - count, list.size());
5665
}
5766

5867
return list;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/CollectionUtilsUnitTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Unit tests for {@link CollectionUtils}.
2626
*
2727
* @author Mark Paluch
28+
* @author qkrtkdwns3410
2829
*/
2930
class CollectionUtilsUnitTests {
3031

@@ -43,4 +44,14 @@ void shouldReturnLastItems() {
4344
assertThat(CollectionUtils.getLast(2, List.of(1, 2))).containsExactly(1, 2);
4445
assertThat(CollectionUtils.getLast(2, List.of(1))).containsExactly(1);
4546
}
47+
48+
@Test // GH-????
49+
void getFirstShouldRejectNullList() {
50+
assertThatIllegalArgumentException().isThrownBy(() -> CollectionUtils.getFirst(2, null));
51+
}
52+
53+
@Test // GH-????
54+
void getLastShouldRejectNullList() {
55+
assertThatIllegalArgumentException().isThrownBy(() -> CollectionUtils.getLast(2, null));
56+
}
4657
}

0 commit comments

Comments
 (0)