44import com .google .common .collect .ImmutableMap ;
55import com .google .common .collect .ImmutableSet ;
66import graphql .Internal ;
7+ import org .jspecify .annotations .NullMarked ;
8+ import org .jspecify .annotations .Nullable ;
79
810import java .util .Collection ;
911import java .util .List ;
1012import java .util .Map ;
1113import java .util .function .Function ;
14+ import java .util .function .Predicate ;
1215
1316import static graphql .Assert .assertNotNull ;
1417
1518@ Internal
16- @ SuppressWarnings ({ "UnstableApiUsage" })
19+ @ NullMarked
1720public final class ImmutableKit {
1821
1922 public static <T > ImmutableList <T > emptyList () {
2023 return ImmutableList .of ();
2124 }
2225
23- public static <T > ImmutableList <T > nonNullCopyOf (Collection <T > collection ) {
26+ public static <T > ImmutableList <T > nonNullCopyOf (@ Nullable Collection <T > collection ) {
2427 return collection == null ? emptyList () : ImmutableList .copyOf (collection );
2528 }
2629
@@ -41,9 +44,9 @@ public static <T> ImmutableList<T> concatLists(List<T> l1, List<T> l2) {
4144 * for the flexible style. Benchmarking has shown this to outperform `stream()`.
4245 *
4346 * @param collection the collection to map
44- * @param mapper the mapper function
45- * @param <T> for two
46- * @param <R> for result
47+ * @param mapper the mapper function
48+ * @param <T> for two
49+ * @param <R> for result
4750 *
4851 * @return a map immutable list of results
4952 */
@@ -58,15 +61,66 @@ public static <T, R> ImmutableList<R> map(Collection<? extends T> collection, Fu
5861 return builder .build ();
5962 }
6063
64+ /**
65+ * This is more efficient than `c.stream().filter().collect()` because it does not create the intermediate objects needed
66+ * for the flexible style. Benchmarking has shown this to outperform `stream()`.
67+ *
68+ * @param collection the collection to map
69+ * @param filter the filter predicate
70+ * @param <T> for two
71+ *
72+ * @return a map immutable list of results
73+ */
74+ public static <T > ImmutableList <T > filter (Collection <? extends T > collection , Predicate <? super T > filter ) {
75+ assertNotNull (collection );
76+ assertNotNull (filter );
77+ return filterAndMap (collection , filter , Function .identity ());
78+ }
79+
80+ /**
81+ * This is more efficient than `c.stream().filter().map().collect()` because it does not create the intermediate objects needed
82+ * for the flexible style. Benchmarking has shown this to outperform `stream()`.
83+ *
84+ * @param collection the collection to map
85+ * @param filter the filter predicate
86+ * @param mapper the mapper function
87+ * @param <T> for two
88+ * @param <R> for result
89+ *
90+ * @return a map immutable list of results
91+ */
92+ public static <T , R > ImmutableList <R > filterAndMap (Collection <? extends T > collection , Predicate <? super T > filter , Function <? super T , ? extends R > mapper ) {
93+ assertNotNull (collection );
94+ assertNotNull (mapper );
95+ assertNotNull (filter );
96+ ImmutableList .Builder <R > builder = ImmutableList .builderWithExpectedSize (collection .size ());
97+ for (T t : collection ) {
98+ if (filter .test (t )) {
99+ R r = mapper .apply (t );
100+ builder .add (r );
101+ }
102+ }
103+ return builder .build ();
104+ }
105+
106+ public static <T > ImmutableList <T > flatMapList (Collection <List <T >> listLists ) {
107+ ImmutableList .Builder <T > builder = ImmutableList .builder ();
108+ for (List <T > t : listLists ) {
109+ builder .addAll (t );
110+ }
111+ return builder .build ();
112+ }
113+
114+
61115 /**
62116 * This will map a collection of items but drop any that are null from the input.
63117 * This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed
64118 * for the flexible style. Benchmarking has shown this to outperform `stream()`.
65119 *
66120 * @param collection the collection to map
67- * @param mapper the mapper function
68- * @param <T> for two
69- * @param <R> for result
121+ * @param mapper the mapper function
122+ * @param <T> for two
123+ * @param <R> for result
70124 *
71125 * @return a map immutable list of results
72126 */
0 commit comments