11package it .aboutbits .springboot .toolbox .util ;
22
33import org .jspecify .annotations .NullMarked ;
4+ import org .jspecify .annotations .Nullable ;
45import org .springframework .data .util .Streamable ;
56
67import java .util .Collection ;
@@ -23,68 +24,77 @@ public final class CollectUtil {
2324 private CollectUtil () {
2425 }
2526
26- public static <T , R > Set <R > collectToSet (Collection <T > items , Function <T , R > mapper ) {
27+ public static <T , R > Set <R > collectToSet (Collection <T > items , Function <T , @ Nullable R > mapper ) {
2728 return items .stream ()
2829 .map (mapper )
2930 .filter (Objects ::nonNull )
3031 .collect (Collectors .toUnmodifiableSet ());
3132 }
3233
33- public static <T , R > Set <R > collectToSet (Streamable <T > items , Function <T , R > mapper ) {
34+ public static <T , R > Set <R > collectToSet (Streamable <T > items , Function <T , @ Nullable R > mapper ) {
3435 return items .stream ()
3536 .map (mapper )
3637 .filter (Objects ::nonNull )
3738 .collect (Collectors .toUnmodifiableSet ());
3839 }
3940
40- public static <T , R > Set <R > collectToSet (Stream <T > items , Function <T , R > mapper ) {
41+ public static <T , R > Set <R > collectToSet (Stream <T > items , Function <T , @ Nullable R > mapper ) {
4142 return items
4243 .map (mapper )
4344 .filter (Objects ::nonNull )
4445 .collect (Collectors .toUnmodifiableSet ());
4546 }
4647
47- public static <T , R > List <R > collectToList (Collection <T > items , Function <T , R > mapper ) {
48+ @ SuppressWarnings ("java:S6204" )
49+ public static <T , R > List <R > collectToList (Collection <T > items , Function <T , @ Nullable R > mapper ) {
50+ //noinspection SimplifyStreamApiCallChains
4851 return items .stream ()
4952 .map (mapper )
5053 .filter (Objects ::nonNull )
51- .toList ( );
54+ .collect ( Collectors . toUnmodifiableList () );
5255 }
5356
54- public static <T , R > List <R > collectToList (Streamable <T > items , Function <T , R > mapper ) {
57+ @ SuppressWarnings ("java:S6204" )
58+ public static <T , R > List <R > collectToList (Streamable <T > items , Function <T , @ Nullable R > mapper ) {
59+ //noinspection SimplifyStreamApiCallChains
5560 return items .stream ()
5661 .map (mapper )
5762 .filter (Objects ::nonNull )
58- .toList ( );
63+ .collect ( Collectors . toUnmodifiableList () );
5964 }
6065
61- public static <T , R > List <R > collectToList (Stream <T > items , Function <T , R > mapper ) {
66+ @ SuppressWarnings ("java:S6204" )
67+ public static <T , R > List <R > collectToList (Stream <T > items , Function <T , @ Nullable R > mapper ) {
68+ //noinspection SimplifyStreamApiCallChains
6269 return items
6370 .map (mapper )
6471 .filter (Objects ::nonNull )
65- .toList ( );
72+ .collect ( Collectors . toUnmodifiableList () );
6673 }
6774
68- public static <T , R > Stream <R > collectToStream (Collection <T > items , Function <T , R > mapper ) {
75+ public static <T , R > Stream <R > collectToStream (Collection <T > items , Function <T , @ Nullable R > mapper ) {
76+ //noinspection NullableProblems
6977 return items .stream ()
7078 .map (mapper )
7179 .filter (Objects ::nonNull );
7280 }
7381
74- public static <T , R > Stream <R > collectToStream (Streamable <T > items , Function <T , R > mapper ) {
82+ public static <T , R > Stream <R > collectToStream (Streamable <T > items , Function <T , @ Nullable R > mapper ) {
83+ //noinspection NullableProblems
7584 return items .stream ()
7685 .map (mapper )
7786 .filter (Objects ::nonNull );
7887 }
7988
80- public static <T , R > Stream <R > collectToStream (Stream <T > items , Function <T , R > mapper ) {
89+ public static <T , R > Stream <R > collectToStream (Stream <T > items , Function <T , @ Nullable R > mapper ) {
90+ //noinspection NullableProblems
8191 return items
8292 .map (mapper )
8393 .filter (Objects ::nonNull );
8494 }
8595
8696 public static <T , K , R > Map <K , R > collectToMap (
87- Collection <T > items ,
97+ Collection <@ Nullable T > items ,
8898 Function <T , K > keyMapper ,
8999 Function <T , R > valueMapper
90100 ) {
@@ -99,12 +109,11 @@ public static <T, K, R> Map<K, R> collectToMap(
99109 Function <T , R > valueMapper
100110 ) {
101111 return items .stream ()
102- .filter (Objects ::nonNull )
103112 .collect (Collectors .toMap (keyMapper , valueMapper ));
104113 }
105114
106115 public static <T , K , R > Map <K , R > collectToMap (
107- Stream <T > items ,
116+ Stream <@ Nullable T > items ,
108117 Function <T , K > keyMapper ,
109118 Function <T , R > valueMapper
110119 ) {
0 commit comments