1
2
3
4
5
6 package gov.nist.secauto.metaschema.core.util;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.Spliterator;
18 import java.util.Spliterators;
19 import java.util.stream.Collectors;
20 import java.util.stream.IntStream;
21 import java.util.stream.Stream;
22 import java.util.stream.StreamSupport;
23
24 import edu.umd.cs.findbugs.annotations.NonNull;
25 import edu.umd.cs.findbugs.annotations.Nullable;
26
27
28
29
30
31
32 @SuppressWarnings("PMD.CouplingBetweenObjects")
33 public final class CollectionUtil {
34
35
36
37
38
39
40
41
42
43 public static <T> Stream<T> toStream(@NonNull Iterator<T> iterator) {
44 Iterable<T> iterable = toIterable(iterator);
45 return StreamSupport.stream(iterable.spliterator(), false);
46 }
47
48
49
50
51
52
53
54
55
56
57 @NonNull
58 public static <T> Iterable<T> toIterable(@NonNull Stream<T> stream) {
59 return toIterable(ObjectUtils.notNull(stream.iterator()));
60 }
61
62
63
64
65
66
67
68
69
70
71 @NonNull
72 public static <T> Iterable<T> toIterable(@NonNull Iterator<T> iterator) {
73 return () -> iterator;
74 }
75
76
77
78
79
80
81
82
83
84
85 @NonNull
86 public static <T> Iterable<T> toDescendingIterable(@NonNull List<T> list) {
87 return toIterable(descendingIterator(list));
88 }
89
90
91
92
93
94
95
96
97
98
99 @NonNull
100 public static <T> List<T> toList(Iterable<T> iterable) {
101 return ObjectUtils.notNull(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()));
102 }
103
104
105
106
107
108
109
110
111
112
113 @NonNull
114 public static <T> List<T> toList(Iterator<T> iterator) {
115 return ObjectUtils.notNull(
116 StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
117 .collect(Collectors.toList()));
118 }
119
120
121
122
123
124
125
126
127
128
129 @NonNull
130 public static <T> Iterator<T> descendingIterator(@NonNull List<T> list) {
131 Iterator<T> retval;
132 if (list instanceof LinkedList) {
133 retval = ((LinkedList<T>) list).descendingIterator();
134 } else if (list instanceof ArrayList) {
135 retval = IntStream.range(0, list.size())
136 .map(i -> list.size() - 1 - i)
137 .mapToObj(list::get).iterator();
138 } else {
139 throw new UnsupportedOperationException();
140 }
141 return ObjectUtils.notNull(retval);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 @NonNull
158 public static <T extends Collection<U>, U> T requireNonEmpty(@NonNull T collection) {
159 if (collection.isEmpty()) {
160 throw new IllegalStateException();
161 }
162 return collection;
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 @NonNull
181 public static <T extends Collection<U>, U> T requireNonEmpty(@NonNull T collection, @NonNull String message) {
182 if (collection.isEmpty()) {
183 throw new IllegalStateException(message);
184 }
185 return collection;
186 }
187
188
189
190
191
192
193
194
195
196
197
198 @SuppressWarnings("null")
199 @NonNull
200 public static <T> Collection<T> unmodifiableCollection(@NonNull Collection<T> collection) {
201 return Collections.unmodifiableCollection(collection);
202 }
203
204
205
206
207
208
209
210
211
212
213
214 @SuppressWarnings("null")
215 @NonNull
216 public static <T> Set<T> singleton(@NonNull T instance) {
217 return Collections.singleton(instance);
218 }
219
220
221
222
223
224
225
226
227
228 @SuppressWarnings("null")
229 @NonNull
230 public static <T> Set<T> emptySet() {
231 return Collections.emptySet();
232 }
233
234
235
236
237
238
239
240
241
242
243
244 @SuppressWarnings("null")
245 @NonNull
246 public static <T> Set<T> unmodifiableSet(@NonNull Set<T> set) {
247 return Collections.unmodifiableSet(set);
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261 @NonNull
262 public static <T> List<T> listOrEmpty(@Nullable List<T> list) {
263 return list == null ? emptyList() : unmodifiableList(list);
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278 @SafeVarargs
279 @SuppressWarnings("null")
280 @NonNull
281 public static <T> List<T> listOrEmpty(@Nullable T... array) {
282 return array == null || array.length == 0 ? emptyList() : unmodifiableList(Arrays.asList(array));
283 }
284
285
286
287
288
289
290
291
292
293 @SuppressWarnings("null")
294 @NonNull
295 public static <T> List<T> emptyList() {
296 return Collections.emptyList();
297 }
298
299
300
301
302
303
304
305
306
307
308
309 @SuppressWarnings("null")
310 @NonNull
311 public static <T> List<T> unmodifiableList(@NonNull List<T> list) {
312 return Collections.unmodifiableList(list);
313 }
314
315
316
317
318
319
320
321
322
323
324
325 @SuppressWarnings("null")
326 @NonNull
327 public static <T> List<T> singletonList(@NonNull T instance) {
328 return Collections.singletonList(instance);
329 }
330
331
332
333
334
335
336
337
338
339
340
341 @SuppressWarnings("null")
342 @NonNull
343 public static <K, V> Map<K, V> emptyMap() {
344 return Collections.emptyMap();
345 }
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 @SuppressWarnings("null")
362 @NonNull
363 public static <K, V> Map<K, V> singletonMap(@NonNull K key, @NonNull V value) {
364 return Collections.singletonMap(key, value);
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378
379 @SuppressWarnings("null")
380 @NonNull
381 public static <K, V> Map<K, V> unmodifiableMap(@NonNull Map<K, V> map) {
382 return Collections.unmodifiableMap(map);
383 }
384
385 private CollectionUtil() {
386
387 }
388 }