46344
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.util;
|
|
24 |
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.Arrays;
|
|
27 |
import java.util.Collection;
|
|
28 |
import java.util.Collections;
|
|
29 |
import java.util.Iterator;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.Objects;
|
|
32 |
import java.util.function.Function;
|
|
33 |
import java.util.function.IntFunction;
|
|
34 |
import java.util.function.Predicate;
|
|
35 |
import java.util.function.Supplier;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This class contains utility methods for commonly used functional patterns for collections.
|
|
39 |
*/
|
48861
|
40 |
public final class CollectionsUtil {
|
|
41 |
|
|
42 |
private CollectionsUtil() {
|
|
43 |
}
|
46344
|
44 |
|
|
45 |
/**
|
|
46 |
* Concatenates two iterables into a single iterable. The iterator exposed by the returned
|
|
47 |
* iterable does not support {@link Iterator#remove()} even if the input iterables do.
|
|
48 |
*
|
|
49 |
* @throws NullPointerException if {@code a} or {@code b} is {@code null}
|
|
50 |
*/
|
|
51 |
public static <T> Iterable<T> concat(Iterable<T> a, Iterable<T> b) {
|
|
52 |
List<Iterable<T>> l = Arrays.asList(a, b);
|
|
53 |
return concat(l);
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Concatenates multiple iterables into a single iterable. The iterator exposed by the returned
|
|
58 |
* iterable does not support {@link Iterator#remove()} even if the input iterables do.
|
|
59 |
*
|
|
60 |
* @throws NullPointerException if {@code iterables} or any of its elements are {@code null}
|
|
61 |
*/
|
|
62 |
public static <T> Iterable<T> concat(List<Iterable<T>> iterables) {
|
|
63 |
for (Iterable<T> iterable : iterables) {
|
|
64 |
Objects.requireNonNull(iterable);
|
|
65 |
}
|
|
66 |
return new Iterable<T>() {
|
|
67 |
@Override
|
|
68 |
public Iterator<T> iterator() {
|
|
69 |
if (iterables.size() == 0) {
|
|
70 |
return Collections.emptyIterator();
|
|
71 |
}
|
|
72 |
return new Iterator<T>() {
|
|
73 |
Iterator<Iterable<T>> cursor = iterables.iterator();
|
|
74 |
Iterator<T> currentIterator = cursor.next().iterator();
|
|
75 |
|
|
76 |
private void advance() {
|
|
77 |
while (!currentIterator.hasNext() && cursor.hasNext()) {
|
|
78 |
currentIterator = cursor.next().iterator();
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
@Override
|
|
83 |
public boolean hasNext() {
|
|
84 |
advance();
|
|
85 |
return currentIterator.hasNext();
|
|
86 |
}
|
|
87 |
|
|
88 |
@Override
|
|
89 |
public T next() {
|
|
90 |
advance();
|
|
91 |
return currentIterator.next();
|
|
92 |
}
|
|
93 |
};
|
|
94 |
}
|
|
95 |
|
|
96 |
};
|
|
97 |
}
|
|
98 |
|
48861
|
99 |
/**
|
|
100 |
* Returns whether all elements in {@code inputs} match {@code predicate}. May not evaluate
|
|
101 |
* {@code predicate} on all elements if not necessary for determining the result. If
|
|
102 |
* {@code inputs} is empty then {@code true} is returned and {@code predicate} is not evaluated.
|
|
103 |
*
|
|
104 |
* @return {@code true} if either all elements in {@code inputs} match {@code predicate} or
|
|
105 |
* {@code inputs} is empty, otherwise {@code false}.
|
|
106 |
*/
|
46344
|
107 |
public static <T> boolean allMatch(T[] inputs, Predicate<T> predicate) {
|
|
108 |
return allMatch(Arrays.asList(inputs), predicate);
|
|
109 |
}
|
|
110 |
|
48861
|
111 |
/**
|
|
112 |
* Returns whether all elements in {@code inputs} match {@code predicate}. May not evaluate
|
|
113 |
* {@code predicate} on all elements if not necessary for determining the result. If
|
|
114 |
* {@code inputs} is empty then {@code true} is returned and {@code predicate} is not evaluated.
|
|
115 |
*
|
|
116 |
* @return {@code true} if either all elements in {@code inputs} match {@code predicate} or
|
|
117 |
* {@code inputs} is empty, otherwise {@code false}.
|
|
118 |
*/
|
46344
|
119 |
public static <T> boolean allMatch(Iterable<T> inputs, Predicate<T> predicate) {
|
|
120 |
for (T t : inputs) {
|
|
121 |
if (!predicate.test(t)) {
|
|
122 |
return false;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
return true;
|
|
126 |
}
|
|
127 |
|
48861
|
128 |
/**
|
|
129 |
* Returns whether any elements in {@code inputs} match {@code predicate}. May not evaluate
|
|
130 |
* {@code predicate} on all elements if not necessary for determining the result. If
|
|
131 |
* {@code inputs} is empty then {@code false} is returned and {@code predicate} is not
|
|
132 |
* evaluated.
|
|
133 |
*
|
|
134 |
* @return {@code true} if any elements in {@code inputs} match {@code predicate}, otherwise
|
|
135 |
* {@code false}.
|
|
136 |
*/
|
46344
|
137 |
public static <T> boolean anyMatch(T[] inputs, Predicate<T> predicate) {
|
|
138 |
return anyMatch(Arrays.asList(inputs), predicate);
|
|
139 |
}
|
|
140 |
|
48861
|
141 |
/**
|
|
142 |
* Returns whether any elements in {@code inputs} match {@code predicate}. May not evaluate
|
|
143 |
* {@code predicate} on all elements if not necessary for determining the result. If
|
|
144 |
* {@code inputs} is empty then {@code false} is returned and {@code predicate} is not
|
|
145 |
* evaluated.
|
|
146 |
*
|
|
147 |
* @return {@code true} if any elements in {@code inputs} match {@code predicate}, otherwise
|
|
148 |
* {@code false}.
|
|
149 |
*/
|
46344
|
150 |
public static <T> boolean anyMatch(Iterable<T> inputs, Predicate<T> predicate) {
|
|
151 |
for (T t : inputs) {
|
|
152 |
if (predicate.test(t)) {
|
|
153 |
return true;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
return false;
|
|
157 |
}
|
|
158 |
|
48861
|
159 |
/**
|
|
160 |
* Returns a new list consisting of the elements in {@code inputs} that match {@code predicate}.
|
|
161 |
*
|
|
162 |
* @return the new list.
|
|
163 |
*/
|
46344
|
164 |
public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate) {
|
|
165 |
return filterToList(inputs, predicate, ArrayList::new);
|
|
166 |
}
|
|
167 |
|
48861
|
168 |
/**
|
|
169 |
* Appends elements of {@code inputs} that match {@code predicate} to the list generated by
|
|
170 |
* {@code listGenerator}.
|
|
171 |
*
|
|
172 |
* @return the list generated by {@code listGenerator}.
|
|
173 |
*/
|
46344
|
174 |
public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate, Supplier<List<T>> listGenerator) {
|
|
175 |
List<T> resultList = listGenerator.get();
|
|
176 |
for (T t : inputs) {
|
|
177 |
if (predicate.test(t)) {
|
|
178 |
resultList.add(t);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
return resultList;
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
48861
|
185 |
* Filters {@code inputs} with {@code predicate}, applies {@code mapper} and adds them in the
|
|
186 |
* array provided by {@code arrayGenerator}.
|
|
187 |
*
|
|
188 |
* @return the array provided by {@code arrayGenerator}.
|
46344
|
189 |
*/
|
|
190 |
public static <T, R> R[] filterAndMapToArray(T[] inputs, Predicate<? super T> predicate, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
|
|
191 |
List<R> resultList = new ArrayList<>();
|
|
192 |
for (T t : inputs) {
|
|
193 |
if (predicate.test(t)) {
|
|
194 |
resultList.add(mapper.apply(t));
|
|
195 |
}
|
|
196 |
}
|
|
197 |
return resultList.toArray(arrayGenerator.apply(resultList.size()));
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
48861
|
201 |
* Applies {@code mapper} on the elements in {@code inputs} and adds them in the array provided
|
|
202 |
* by {@code arrayGenerator}.
|
|
203 |
*
|
|
204 |
* @return the array provided by {@code arrayGenerator}.
|
46344
|
205 |
*/
|
|
206 |
public static <T, R> R[] mapToArray(T[] inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
|
|
207 |
return mapToArray(Arrays.asList(inputs), mapper, arrayGenerator);
|
|
208 |
}
|
|
209 |
|
48861
|
210 |
/**
|
|
211 |
* Applies {@code mapper} on the elements in {@code inputs} and adds them in the array provided
|
|
212 |
* by {@code arrayGenerator}.
|
|
213 |
*
|
|
214 |
* @return the array provided by {@code arrayGenerator}.
|
|
215 |
*/
|
46344
|
216 |
public static <T, R> R[] mapToArray(Collection<T> inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
|
|
217 |
R[] result = arrayGenerator.apply(inputs.size());
|
|
218 |
int idx = 0;
|
|
219 |
for (T t : inputs) {
|
|
220 |
result[idx++] = mapper.apply(t);
|
|
221 |
}
|
|
222 |
return result;
|
|
223 |
}
|
|
224 |
|
48861
|
225 |
/**
|
|
226 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
227 |
* by {@code delimiter}.
|
|
228 |
*
|
|
229 |
* @return a new String that is composed from {@code inputs}.
|
|
230 |
*/
|
46344
|
231 |
public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter) {
|
|
232 |
return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, "", "");
|
|
233 |
}
|
|
234 |
|
48861
|
235 |
/**
|
|
236 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
237 |
* by {@code delimiter} and starting with {@code prefix}.
|
|
238 |
*
|
|
239 |
* @return a new String that is composed from {@code inputs}.
|
|
240 |
*/
|
46344
|
241 |
public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
|
|
242 |
return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, "");
|
|
243 |
}
|
|
244 |
|
48861
|
245 |
/**
|
|
246 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
247 |
* by {@code delimiter} and starting with {@code prefix} and ending with {@code suffix}.
|
|
248 |
*
|
|
249 |
* @return a new String that is composed from {@code inputs}.
|
|
250 |
*/
|
46344
|
251 |
public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
|
|
252 |
return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, suffix);
|
|
253 |
}
|
|
254 |
|
48861
|
255 |
/**
|
|
256 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
257 |
* by {@code delimiter}.
|
|
258 |
*
|
|
259 |
* @return a new String that is composed from {@code inputs}.
|
|
260 |
*/
|
46344
|
261 |
public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter) {
|
|
262 |
return mapAndJoin(inputs, mapper, delimiter, "", "");
|
|
263 |
}
|
|
264 |
|
48861
|
265 |
/**
|
|
266 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
267 |
* by {@code delimiter} and starting with {@code prefix}.
|
|
268 |
*
|
|
269 |
* @return a new String that is composed from {@code inputs}.
|
|
270 |
*/
|
46344
|
271 |
public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
|
|
272 |
return mapAndJoin(inputs, mapper, delimiter, prefix, "");
|
|
273 |
}
|
|
274 |
|
48861
|
275 |
/**
|
|
276 |
* Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
|
|
277 |
* by {@code delimiter} and starting with {@code prefix} and ending with {@code suffix}.
|
|
278 |
*
|
|
279 |
* @return a new String that is composed from {@code inputs}.
|
|
280 |
*/
|
46344
|
281 |
public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
|
|
282 |
StringBuilder strb = new StringBuilder();
|
|
283 |
String sep = "";
|
|
284 |
for (T t : inputs) {
|
|
285 |
strb.append(sep).append(prefix).append(mapper.apply(t)).append(suffix);
|
|
286 |
sep = delimiter;
|
|
287 |
}
|
|
288 |
return strb.toString();
|
|
289 |
}
|
48861
|
290 |
|
46344
|
291 |
}
|