Skip to content

Commit 07e55f9

Browse files
committed
add missing null checks
1 parent 57e0de2 commit 07e55f9

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/main/java/io/github/jhspetersson/packrat/Packrat.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ public final class Packrat {
575575
*/
576576
@NonNull
577577
public static <T> Gatherer<T, ?, T> mapWhile(@NonNull Function<? super T, ? extends T> mapper, @NonNull Predicate<? super T> predicate) {
578+
Objects.requireNonNull(predicate, "predicate cannot be null");
578579
return new MapWhileUntilGatherer<>(mapper, predicate);
579580
}
580581

@@ -590,6 +591,7 @@ public final class Packrat {
590591
*/
591592
@NonNull
592593
public static <T> Gatherer<T, ?, T> mapUntil(@NonNull Function<? super T, ? extends T> mapper, @NonNull Predicate<? super T> predicate) {
594+
Objects.requireNonNull(predicate, "predicate cannot be null");
593595
return new MapWhileUntilGatherer<>(mapper, null, predicate);
594596
}
595597

src/test/java/io/github/jhspetersson/packrat/MapWhileUntilTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.stream.IntStream;
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
910

1011
public class MapWhileUntilTest {
1112
@Test
@@ -19,4 +20,24 @@ public void mapUntilTest() {
1920
var mapped = IntStream.rangeClosed(1, 10).boxed().gather(Packrat.mapUntil(n -> n * 10, n -> n == 6)).toList();
2021
assertEquals(List.of(10, 20, 30, 40, 50, 6, 7, 8, 9, 10), mapped);
2122
}
23+
24+
@Test
25+
public void mapWhileNullPredicateThrows() {
26+
assertThrows(NullPointerException.class, () -> Packrat.mapWhile(n -> (Integer) n * 10, null));
27+
}
28+
29+
@Test
30+
public void mapUntilNullPredicateThrows() {
31+
assertThrows(NullPointerException.class, () -> Packrat.mapUntil(n -> (Integer) n * 10, null));
32+
}
33+
34+
@Test
35+
public void mapWhileNullMapperThrows() {
36+
assertThrows(NullPointerException.class, () -> Packrat.<Integer>mapWhile(null, n -> n <= 3));
37+
}
38+
39+
@Test
40+
public void mapUntilNullMapperThrows() {
41+
assertThrows(NullPointerException.class, () -> Packrat.<Integer>mapUntil(null, n -> n == 6));
42+
}
2243
}

0 commit comments

Comments
 (0)