1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.metaschema.core.util;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.Spliterator;
39 import java.util.Spliterators;
40 import java.util.stream.Collectors;
41 import java.util.stream.IntStream;
42 import java.util.stream.Stream;
43 import java.util.stream.StreamSupport;
44
45 import edu.umd.cs.findbugs.annotations.NonNull;
46 import edu.umd.cs.findbugs.annotations.Nullable;
47
48 public final class CollectionUtil {
49
50 private CollectionUtil() {
51
52 }
53
54
55
56
57
58
59
60
61
62
63 public static <T> Stream<T> toStream(@NonNull Iterator<T> iterator) {
64 Iterable<T> iterable = toIterable(iterator);
65 return StreamSupport.stream(iterable.spliterator(), false);
66 }
67
68
69
70
71
72
73
74
75
76
77 @NonNull
78 public static <T> Iterable<T> toIterable(@NonNull Stream<T> stream) {
79 return toIterable(ObjectUtils.notNull(stream.iterator()));
80 }
81
82
83
84
85
86
87
88
89
90
91 @NonNull
92 public static <T> Iterable<T> toIterable(@NonNull Iterator<T> iterator) {
93 return () -> iterator;
94 }
95
96
97
98
99
100
101
102
103
104
105 @NonNull
106 public static <T> Iterable<T> toDescendingIterable(@NonNull List<T> list) {
107 return toIterable(descendingIterator(list));
108 }
109
110
111
112
113
114
115
116
117
118
119 @NonNull
120 public static <T> List<T> toList(Iterable<T> iterable) {
121 return ObjectUtils.notNull(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()));
122 }
123
124
125
126
127
128
129
130
131
132
133 @NonNull
134 public static <T> List<T> toList(Iterator<T> iterator) {
135 return ObjectUtils.notNull(
136 StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false)
137 .collect(Collectors.toList()));
138 }
139
140
141
142
143
144
145
146
147
148
149 @NonNull
150 public static <T> Iterator<T> descendingIterator(@NonNull List<T> list) {
151 Iterator<T> retval;
152 if (list instanceof LinkedList) {
153 retval = ((LinkedList<T>) list).descendingIterator();
154 } else if (list instanceof ArrayList) {
155 retval = IntStream.range(0, list.size())
156 .map(i -> list.size() - 1 - i)
157 .mapToObj(list::get).iterator();
158 } else {
159 throw new UnsupportedOperationException();
160 }
161 return ObjectUtils.notNull(retval);
162 }
163
164 @NonNull
165 public static <T extends Collection<A>, A> T requireNonEmpty(@NonNull T collection) {
166 if (collection.isEmpty()) {
167 throw new IllegalStateException();
168 }
169 return collection;
170 }
171
172 @NonNull
173 public static <T extends Collection<A>, A> T requireNonEmpty(@NonNull T collection, @NonNull String message) {
174 if (collection.isEmpty()) {
175 throw new IllegalStateException(message);
176 }
177 return collection;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 @SuppressWarnings("null")
191 @NonNull
192 public static <T> Collection<T> unmodifiableCollection(@NonNull Collection<T> collection) {
193 return Collections.unmodifiableCollection(collection);
194 }
195
196 @SuppressWarnings("null")
197 @NonNull
198 public static <T> Set<T> singleton(@NonNull T value) {
199 return Collections.singleton(value);
200 }
201
202 @SuppressWarnings("null")
203 @NonNull
204 public static <T> Set<T> emptySet() {
205 return Collections.emptySet();
206 }
207
208 @SuppressWarnings("null")
209 @NonNull
210 public static <T> Set<T> unmodifiableSet(@NonNull Set<T> set) {
211 return Collections.unmodifiableSet(set);
212 }
213
214 @NonNull
215 public static <T> List<T> listOrEmpty(@Nullable List<T> list) {
216 return list == null ? emptyList() : list;
217 }
218
219 @SafeVarargs
220 @SuppressWarnings("null")
221 @NonNull
222 public static <T> List<T> listOrEmpty(@Nullable T... array) {
223 return array == null || array.length == 0 ? emptyList() : Arrays.asList(array);
224 }
225
226 @SuppressWarnings("null")
227 @NonNull
228 public static <T> List<T> emptyList() {
229 return Collections.emptyList();
230 }
231
232 @SuppressWarnings("null")
233 @NonNull
234 public static <T> List<T> unmodifiableList(@NonNull List<T> list) {
235 return Collections.unmodifiableList(list);
236 }
237
238 @SuppressWarnings("null")
239 @NonNull
240 public static <T> List<T> singletonList(@NonNull T instance) {
241 return Collections.singletonList(instance);
242 }
243
244 @SuppressWarnings("null")
245 @NonNull
246 public static <K, V> Map<K, V> emptyMap() {
247 return Collections.emptyMap();
248 }
249
250 @SuppressWarnings("null")
251 @NonNull
252 public static <K, V> Map<K, V> singletonMap(@NonNull K key, @NonNull V value) {
253 return Collections.singletonMap(key, value);
254 }
255
256 @SuppressWarnings("null")
257 @NonNull
258 public static <K, V> Map<K, V> unmodifiableMap(@NonNull Map<K, V> map) {
259 return Collections.unmodifiableMap(map);
260 }
261 }