17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
182 |
ilm |
16 |
import org.openconcerto.utils.cc.CustomEquals;
|
19 |
ilm |
17 |
import org.openconcerto.utils.cc.IClosure;
|
|
|
18 |
import org.openconcerto.utils.cc.IPredicate;
|
17 |
ilm |
19 |
import org.openconcerto.utils.cc.ITransformer;
|
180 |
ilm |
20 |
import org.openconcerto.utils.cc.ITransformerExn;
|
65 |
ilm |
21 |
import org.openconcerto.utils.cc.IdentityHashSet;
|
|
|
22 |
import org.openconcerto.utils.cc.IdentitySet;
|
174 |
ilm |
23 |
import org.openconcerto.utils.cc.LinkedIdentitySet;
|
132 |
ilm |
24 |
import org.openconcerto.utils.cc.Transformer;
|
17 |
ilm |
25 |
|
73 |
ilm |
26 |
import java.io.Serializable;
|
|
|
27 |
import java.util.AbstractSet;
|
17 |
ilm |
28 |
import java.util.ArrayList;
|
|
|
29 |
import java.util.Arrays;
|
|
|
30 |
import java.util.Collection;
|
|
|
31 |
import java.util.Collections;
|
|
|
32 |
import java.util.HashMap;
|
|
|
33 |
import java.util.HashSet;
|
180 |
ilm |
34 |
import java.util.IdentityHashMap;
|
17 |
ilm |
35 |
import java.util.Iterator;
|
|
|
36 |
import java.util.LinkedHashMap;
|
180 |
ilm |
37 |
import java.util.LinkedHashSet;
|
73 |
ilm |
38 |
import java.util.LinkedList;
|
17 |
ilm |
39 |
import java.util.List;
|
73 |
ilm |
40 |
import java.util.ListIterator;
|
17 |
ilm |
41 |
import java.util.Map;
|
132 |
ilm |
42 |
import java.util.Map.Entry;
|
73 |
ilm |
43 |
import java.util.NoSuchElementException;
|
17 |
ilm |
44 |
import java.util.RandomAccess;
|
|
|
45 |
import java.util.Set;
|
180 |
ilm |
46 |
import java.util.SortedMap;
|
|
|
47 |
import java.util.SortedSet;
|
|
|
48 |
import java.util.TreeMap;
|
|
|
49 |
import java.util.TreeSet;
|
|
|
50 |
import java.util.function.Function;
|
182 |
ilm |
51 |
import java.util.function.Supplier;
|
17 |
ilm |
52 |
import java.util.regex.Pattern;
|
182 |
ilm |
53 |
import java.util.stream.Collectors;
|
|
|
54 |
import java.util.stream.Stream;
|
17 |
ilm |
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Une classe regroupant des méthodes utilitaires pour les collections.
|
|
|
58 |
*
|
|
|
59 |
* @author ILM Informatique 30 sept. 2004
|
|
|
60 |
*/
|
67 |
ilm |
61 |
public class CollectionUtils {
|
17 |
ilm |
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Concatene une collection. Cette méthode va appliquer un transformation sur chaque élément
|
|
|
65 |
* avant d'appeler toString(). join([-1, 3, 0], " ,", doubleTransformer) == "-2, 6, 0"
|
|
|
66 |
*
|
|
|
67 |
* @param <E> type of items
|
|
|
68 |
* @param c la collection a concaténer.
|
|
|
69 |
* @param sep le séparateur entre chaque élément.
|
|
|
70 |
* @param tf la transformation à appliquer à chaque élément.
|
|
|
71 |
* @return la chaine composée de chacun des éléments séparés par <code>sep</code>.
|
|
|
72 |
*/
|
|
|
73 |
static public final <E> String join(final Collection<E> c, final String sep, final ITransformer<? super E, ?> tf) {
|
19 |
ilm |
74 |
final int size = c.size();
|
|
|
75 |
if (size == 0)
|
17 |
ilm |
76 |
return "";
|
|
|
77 |
|
19 |
ilm |
78 |
final StringBuffer res = new StringBuffer(size * 4);
|
17 |
ilm |
79 |
if (c instanceof RandomAccess && c instanceof List) {
|
|
|
80 |
final List<E> list = (List<E>) c;
|
19 |
ilm |
81 |
for (int i = 0; i < size; i++) {
|
17 |
ilm |
82 |
res.append(tf.transformChecked(list.get(i)));
|
19 |
ilm |
83 |
if (i < size - 1)
|
|
|
84 |
res.append(sep);
|
17 |
ilm |
85 |
}
|
|
|
86 |
} else {
|
|
|
87 |
final Iterator<E> iter = c.iterator();
|
|
|
88 |
while (iter.hasNext()) {
|
|
|
89 |
final E elem = iter.next();
|
|
|
90 |
res.append(tf.transformChecked(elem));
|
|
|
91 |
if (iter.hasNext())
|
|
|
92 |
res.append(sep);
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
return res.toString();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Concatene une collection en appelant simplement toString() sur chaque élément.
|
|
|
100 |
*
|
|
|
101 |
* @param <T> type of collection
|
|
|
102 |
* @param c la collection a concaténer.
|
|
|
103 |
* @param sep le séparateur entre chaque élément.
|
|
|
104 |
* @return la chaine composée de chacun des éléments séparés par <code>sep</code>.
|
|
|
105 |
* @see #join(Collection, String, ITransformer)
|
|
|
106 |
*/
|
|
|
107 |
static public <T> String join(Collection<T> c, String sep) {
|
149 |
ilm |
108 |
return join(c, sep, org.openconcerto.utils.cc.Transformer.<T> nopTransformer());
|
17 |
ilm |
109 |
}
|
|
|
110 |
|
67 |
ilm |
111 |
static public <T, U, C extends Collection<? super U>> C transform(final Collection<T> c, final ITransformer<? super T, U> transf, final C res) {
|
|
|
112 |
return transformAndFilter(c, transf, IPredicate.truePredicate(), res);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
static public <T, U, C extends Collection<? super U>> C transformAndFilter(final Collection<T> c, final ITransformer<? super T, U> transf, final IPredicate<? super U> filterAfter, final C res) {
|
|
|
116 |
return filterTransformAndFilter(c, IPredicate.truePredicate(), transf, filterAfter, res);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
static public <T, U, C extends Collection<? super U>> C filterAndTransform(final Collection<T> c, final IPredicate<? super T> filterBefore, final ITransformer<? super T, U> transf, final C res) {
|
|
|
120 |
return filterTransformAndFilter(c, filterBefore, transf, IPredicate.truePredicate(), res);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
static public <T, U, C extends Collection<? super U>> C filterTransformAndFilter(final Collection<T> c, final IPredicate<? super T> filterBefore, final ITransformer<? super T, U> transf,
|
|
|
124 |
final IPredicate<? super U> filterAfter, final C res) {
|
|
|
125 |
iterate(c, filterBefore, new IClosure<T>() {
|
19 |
ilm |
126 |
@Override
|
|
|
127 |
public void executeChecked(T input) {
|
|
|
128 |
final U item = transf.transformChecked(input);
|
67 |
ilm |
129 |
if (filterAfter.evaluateChecked(item))
|
19 |
ilm |
130 |
res.add(item);
|
|
|
131 |
}
|
|
|
132 |
});
|
|
|
133 |
return res;
|
|
|
134 |
}
|
17 |
ilm |
135 |
|
19 |
ilm |
136 |
static public <T> void iterate(final Collection<T> c, final IClosure<T> cl) {
|
67 |
ilm |
137 |
iterate(c, IPredicate.truePredicate(), cl);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
static public <T> void iterate(final Collection<T> c, final IPredicate<? super T> filterBefore, final IClosure<T> cl) {
|
19 |
ilm |
141 |
if (c instanceof RandomAccess && c instanceof List) {
|
|
|
142 |
final List<T> list = (List<T>) c;
|
|
|
143 |
final int size = c.size();
|
|
|
144 |
for (int i = 0; i < size; i++) {
|
67 |
ilm |
145 |
final T item = list.get(i);
|
|
|
146 |
if (filterBefore.evaluateChecked(item))
|
|
|
147 |
cl.executeChecked(item);
|
19 |
ilm |
148 |
}
|
|
|
149 |
} else {
|
|
|
150 |
final Iterator<T> iter = c.iterator();
|
|
|
151 |
while (iter.hasNext()) {
|
67 |
ilm |
152 |
final T item = iter.next();
|
|
|
153 |
if (filterBefore.evaluateChecked(item))
|
|
|
154 |
cl.executeChecked(item);
|
19 |
ilm |
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
17 |
ilm |
159 |
private static final Pattern COMMA = Pattern.compile("\\p{Space}*,\\p{Space}*");
|
|
|
160 |
|
|
|
161 |
static public List<String> split(String s) {
|
|
|
162 |
return split(s, COMMA);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
static public List<String> split(String s, String sep) {
|
|
|
166 |
return split(s, Pattern.compile(sep));
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Split a string into a list based on a pattern.
|
|
|
171 |
*
|
|
|
172 |
* @param s the string to split.
|
|
|
173 |
* @param pattern the pattern where to cut the string.
|
|
|
174 |
* @return the splitted string, empty list if <code>s</code> is "".
|
|
|
175 |
*/
|
|
|
176 |
static public List<String> split(String s, Pattern pattern) {
|
149 |
ilm |
177 |
return s.length() == 0 ? Collections.<String> emptyList() : Arrays.asList(pattern.split(s));
|
17 |
ilm |
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Return an index between <code>0</code> and <code>l.size()</code> inclusive. If <code>i</code>
|
93 |
ilm |
182 |
* is negative, it is added to <code>l.size()</code> (i.e. -1 is the index of the last item and
|
|
|
183 |
* -size is the first) :
|
17 |
ilm |
184 |
*
|
|
|
185 |
* <pre>
|
|
|
186 |
* a b c a b c
|
|
|
187 |
* -3 -2 -1 0 1 2 3
|
|
|
188 |
* </pre>
|
|
|
189 |
*
|
93 |
ilm |
190 |
* @param l the list, e.g. a list of 3 items.
|
|
|
191 |
* @param i the virtual index, e.g. -1.
|
|
|
192 |
* @return the real index, e.g. 2.
|
|
|
193 |
* @throws IndexOutOfBoundsException if not <code>0 ≤ abs(i) ≤ l.size()</code>
|
17 |
ilm |
194 |
*/
|
93 |
ilm |
195 |
static public int getValidIndex(final List<?> l, final int i) throws IndexOutOfBoundsException {
|
|
|
196 |
return getValidIndex(l, i, true);
|
61 |
ilm |
197 |
}
|
|
|
198 |
|
93 |
ilm |
199 |
/**
|
|
|
200 |
* Return an index between <code>0</code> and <code>l.size()</code> inclusive. If <code>i</code>
|
|
|
201 |
* is negative, it is added to <code>l.size()</code> (bounded to 0 if <code>strict</code> is
|
|
|
202 |
* <code>false</code>), i.e. for a list of 3 items, -1 is the index of the last item ; -3 and -4
|
|
|
203 |
* are both the first. If <code>i</code> is greater than <code>l.size()</code> then
|
|
|
204 |
* <code>l.size()</code> is returned if not <code>strict</code>.
|
|
|
205 |
*
|
|
|
206 |
* @param l the list, e.g. a list of 3 items.
|
|
|
207 |
* @param i the virtual index, e.g. -1.
|
|
|
208 |
* @param strict if <code>true</code> <code>abs(i)</code> must be between <code>0</code> and
|
|
|
209 |
* <code>l.size()</code>, else values are bounded between <code>0</code> and
|
|
|
210 |
* <code>l.size()</code>.
|
|
|
211 |
* @return the real index between <code>0</code> and <code>l.size()</code>, e.g. 2.
|
|
|
212 |
* @throws IndexOutOfBoundsException if <code>strict</code> is <code>true</code> and not
|
|
|
213 |
* <code>0 ≤ abs(i) ≤ l.size()</code>
|
|
|
214 |
*/
|
|
|
215 |
static public int getValidIndex(final List<?> l, final int i, final boolean strict) throws IndexOutOfBoundsException {
|
61 |
ilm |
216 |
final int size = l.size();
|
|
|
217 |
if (i > size) {
|
|
|
218 |
if (strict)
|
|
|
219 |
throw new IndexOutOfBoundsException("Too high : " + i + " > " + size);
|
|
|
220 |
return size;
|
|
|
221 |
} else if (i < -size) {
|
|
|
222 |
if (strict)
|
|
|
223 |
throw new IndexOutOfBoundsException("Too low : " + i + " < " + -size);
|
|
|
224 |
return 0;
|
17 |
ilm |
225 |
} else if (i >= 0) {
|
|
|
226 |
return i;
|
61 |
ilm |
227 |
} else {
|
|
|
228 |
return size + i;
|
|
|
229 |
}
|
17 |
ilm |
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
93 |
ilm |
233 |
* Deletes a slice of a list. Pass indexes to {@link #getValidIndex(List, int, boolean)} to
|
|
|
234 |
* allow delete(l, 0, -1) to clear l or delete(l, -2, -2) to remove the penultimate item.
|
17 |
ilm |
235 |
*
|
|
|
236 |
* @param l the list to delete from.
|
|
|
237 |
* @param from the first index to be removed (inclusive).
|
|
|
238 |
* @param to the last index to be removed (inclusive).
|
|
|
239 |
*/
|
|
|
240 |
static public void delete(List<?> l, int from, int to) {
|
|
|
241 |
if (!l.isEmpty())
|
93 |
ilm |
242 |
l.subList(getValidIndex(l, from, false), getValidIndex(l, to, false) + 1).clear();
|
17 |
ilm |
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Deletes the tail of a list. The resulting list will have a size of <code>from</code>.
|
|
|
247 |
*
|
|
|
248 |
* @param l the list to delete from.
|
|
|
249 |
* @param from the first index to be removed (inclusive).
|
|
|
250 |
*/
|
|
|
251 |
static public void delete(List<?> l, int from) {
|
|
|
252 |
delete(l, from, -1);
|
|
|
253 |
}
|
|
|
254 |
|
67 |
ilm |
255 |
public static <T> boolean exists(Collection<T> collection, IPredicate<? super T> predicate) {
|
180 |
ilm |
256 |
return collection.stream().anyMatch(predicate::evaluateChecked);
|
67 |
ilm |
257 |
}
|
|
|
258 |
|
17 |
ilm |
259 |
/**
|
|
|
260 |
* Convertit une map en 2 listes, une pour les clefs, une pour les valeurs.
|
|
|
261 |
*
|
|
|
262 |
* @param map la Map à convertir.
|
|
|
263 |
* @return un tuple de 2 List, en 0 les clefs, en 1 les valeurs.
|
|
|
264 |
* @param <K> type of key
|
|
|
265 |
* @param <V> type of value
|
|
|
266 |
*/
|
|
|
267 |
static public <K, V> Tuple2<List<K>, List<V>> mapToLists(Map<K, V> map) {
|
|
|
268 |
final List<K> keys = new ArrayList<K>(map.size());
|
|
|
269 |
final List<V> vals = new ArrayList<V>(map.size());
|
|
|
270 |
for (final Map.Entry<K, V> e : map.entrySet()) {
|
|
|
271 |
keys.add(e.getKey());
|
|
|
272 |
vals.add(e.getValue());
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return Tuple2.create(keys, vals);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Add entries from <code>toAdd</code> into <code>map</code> only if the key is not already
|
|
|
280 |
* present.
|
|
|
281 |
*
|
|
|
282 |
* @param <K> type of keys.
|
|
|
283 |
* @param <V> type of values.
|
|
|
284 |
* @param map the map to fill.
|
|
|
285 |
* @param toAdd the entries to add.
|
|
|
286 |
* @return <code>map</code>.
|
|
|
287 |
*/
|
|
|
288 |
static public <K, V> Map<K, V> addIfNotPresent(Map<K, V> map, Map<? extends K, ? extends V> toAdd) {
|
|
|
289 |
for (final Map.Entry<? extends K, ? extends V> e : toAdd.entrySet()) {
|
|
|
290 |
if (!map.containsKey(e.getKey()))
|
|
|
291 |
map.put(e.getKey(), e.getValue());
|
|
|
292 |
}
|
|
|
293 |
return map;
|
|
|
294 |
}
|
|
|
295 |
|
180 |
ilm |
296 |
// compared to computeIfAbsent() :
|
|
|
297 |
// 1. allow exceptions
|
|
|
298 |
// 2. allow nulls
|
|
|
299 |
static public <K, V, X extends Exception> V computeIfAbsent(final Map<K, V> map, final K key, final ITransformerExn<K, V, X> function, final boolean allowNullValue) throws X {
|
|
|
300 |
V res = map.get(key);
|
|
|
301 |
final boolean contains;
|
|
|
302 |
if (allowNullValue) {
|
|
|
303 |
contains = res != null || map.containsKey(key);
|
|
|
304 |
} else {
|
|
|
305 |
contains = res != null;
|
|
|
306 |
}
|
|
|
307 |
if (!contains) {
|
|
|
308 |
res = function.transformChecked(key);
|
|
|
309 |
if (res == null && !allowNullValue)
|
|
|
310 |
throw new IllegalStateException("Null value computed for key '" + key + "'");
|
|
|
311 |
map.put(key, res);
|
|
|
312 |
}
|
|
|
313 |
assert allowNullValue || res != null;
|
|
|
314 |
return res;
|
|
|
315 |
}
|
|
|
316 |
|
17 |
ilm |
317 |
/**
|
|
|
318 |
* Compute the index that have changed (added or removed) between 2 lists. One of the lists MUST
|
|
|
319 |
* be a sublist of the other, ie the to go from one to the other we just add or remove items but
|
|
|
320 |
* we don't do both.
|
|
|
321 |
*
|
|
|
322 |
* @param oldList the first list.
|
|
|
323 |
* @param newList the second list.
|
|
|
324 |
* @return a list of Integer.
|
|
|
325 |
* @param <E> type of item
|
|
|
326 |
* @throws IllegalStateException if one list is not a sublist of the other.
|
|
|
327 |
*/
|
|
|
328 |
static public <E> List<Integer> getIndexesChanged(List<E> oldList, List<E> newList) {
|
|
|
329 |
final List<E> longer;
|
|
|
330 |
final List<E> shorter;
|
|
|
331 |
if (newList.size() > oldList.size()) {
|
|
|
332 |
longer = new ArrayList<E>(newList);
|
|
|
333 |
shorter = new ArrayList<E>(oldList);
|
|
|
334 |
} else {
|
|
|
335 |
longer = new ArrayList<E>(oldList);
|
|
|
336 |
shorter = new ArrayList<E>(newList);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
final List<Integer> res = new ArrayList<Integer>();
|
|
|
340 |
int offset = 0;
|
|
|
341 |
while (shorter.size() > 0) {
|
|
|
342 |
if (longer.size() < shorter.size())
|
|
|
343 |
throw new IllegalStateException(shorter + " is not a sublist of " + longer);
|
|
|
344 |
// compare nulls
|
|
|
345 |
if (CompareUtils.equals(shorter.get(0), longer.get(0))) {
|
|
|
346 |
shorter.remove(0);
|
|
|
347 |
longer.remove(0);
|
|
|
348 |
} else {
|
|
|
349 |
longer.remove(0);
|
|
|
350 |
res.add(offset);
|
|
|
351 |
}
|
|
|
352 |
offset++;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
for (int i = 0; i < longer.size(); i++) {
|
|
|
356 |
res.add(i + offset);
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
return res;
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
/**
|
|
|
363 |
* Aggregate a list of ints into a list of intervals. Eg aggregate([-1,0,1,2,5]) returns
|
|
|
364 |
* [[-1,2], [5,5]].
|
|
|
365 |
*
|
|
|
366 |
* @param ints a list of Integer strictly increasing.
|
|
|
367 |
* @return a list of int[2].
|
|
|
368 |
*/
|
|
|
369 |
static public List<int[]> aggregate(Collection<? extends Number> ints) {
|
|
|
370 |
final List<int[]> res = new ArrayList<int[]>();
|
|
|
371 |
int[] currentInterval = null;
|
|
|
372 |
for (final Number n : ints) {
|
|
|
373 |
final int index = n.intValue();
|
|
|
374 |
if (currentInterval == null || index != currentInterval[1] + 1) {
|
|
|
375 |
currentInterval = new int[2];
|
|
|
376 |
currentInterval[0] = index;
|
|
|
377 |
currentInterval[1] = currentInterval[0];
|
|
|
378 |
res.add(currentInterval);
|
|
|
379 |
} else {
|
|
|
380 |
currentInterval[1] = index;
|
|
|
381 |
}
|
|
|
382 |
}
|
|
|
383 |
return res;
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
/**
|
|
|
387 |
* Test whether col2 is contained in col1.
|
|
|
388 |
*
|
|
|
389 |
* @param <T> type of collection
|
|
|
390 |
* @param col1 the first collection
|
|
|
391 |
* @param col2 the second collection
|
|
|
392 |
* @return <code>null</code> if col1 contains all of col2, else return the extra items that col2
|
|
|
393 |
* have.
|
|
|
394 |
*/
|
|
|
395 |
static public <T> Set<T> contains(final Set<T> col1, final Set<T> col2) {
|
|
|
396 |
if (col1.containsAll(col2))
|
|
|
397 |
return null;
|
|
|
398 |
else {
|
|
|
399 |
final Set<T> names = new HashSet<T>(col2);
|
|
|
400 |
names.removeAll(col1);
|
|
|
401 |
return names;
|
|
|
402 |
}
|
|
|
403 |
}
|
|
|
404 |
|
67 |
ilm |
405 |
static public <C extends Collection<?>> boolean containsAny(final C coll1, final C coll2) {
|
142 |
ilm |
406 |
return !Collections.disjoint(coll1, coll2);
|
67 |
ilm |
407 |
}
|
|
|
408 |
|
180 |
ilm |
409 |
static public final boolean identityContains(final Collection<?> coll, final Object item) {
|
182 |
ilm |
410 |
final int size = coll.size();
|
|
|
411 |
if (size > 0) {
|
|
|
412 |
if (coll instanceof RandomAccess && coll instanceof List) {
|
|
|
413 |
final List<?> list = (List<?>) coll;
|
|
|
414 |
for (int i = 0; i < size; i++) {
|
|
|
415 |
final Object v = list.get(i);
|
|
|
416 |
if (item == v)
|
|
|
417 |
return true;
|
|
|
418 |
}
|
|
|
419 |
} else {
|
|
|
420 |
for (final Object v : coll) {
|
|
|
421 |
if (item == v)
|
|
|
422 |
return true;
|
|
|
423 |
}
|
|
|
424 |
}
|
93 |
ilm |
425 |
}
|
|
|
426 |
return false;
|
|
|
427 |
}
|
|
|
428 |
|
182 |
ilm |
429 |
static public final boolean identityRemove(final Collection<?> coll, final Object item) {
|
|
|
430 |
final int size = coll.size();
|
|
|
431 |
if (size > 0) {
|
|
|
432 |
if (coll instanceof RandomAccess && coll instanceof List) {
|
|
|
433 |
final List<?> list = (List<?>) coll;
|
|
|
434 |
for (int i = 0; i < size; i++) {
|
|
|
435 |
final Object v = list.get(i);
|
|
|
436 |
if (item == v) {
|
|
|
437 |
list.remove(i);
|
|
|
438 |
return true;
|
|
|
439 |
}
|
|
|
440 |
}
|
|
|
441 |
} else {
|
|
|
442 |
for (Iterator<?> i = coll.iterator(); i.hasNext();) {
|
|
|
443 |
if (item == i.next()) {
|
|
|
444 |
i.remove();
|
|
|
445 |
return true;
|
|
|
446 |
}
|
|
|
447 |
}
|
|
|
448 |
}
|
|
|
449 |
}
|
|
|
450 |
return false;
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
static public final <T> int identityIndexOf(final List<T> coll, final T item) {
|
|
|
454 |
return CustomEquals.indexOf(coll, item, CustomEquals.getIdentity());
|
|
|
455 |
}
|
|
|
456 |
|
180 |
ilm |
457 |
static public final boolean identityEquals(final List<?> coll1, final List<?> coll2) {
|
|
|
458 |
if (coll1 == coll2)
|
|
|
459 |
return true;
|
|
|
460 |
final int size = coll1.size();
|
|
|
461 |
if (size != coll2.size())
|
|
|
462 |
return false;
|
|
|
463 |
if (size == 0)
|
|
|
464 |
return true;
|
|
|
465 |
|
|
|
466 |
final Iterator<?> iter1 = coll1.iterator();
|
|
|
467 |
final Iterator<?> iter2 = coll2.iterator();
|
|
|
468 |
while (iter1.hasNext()) {
|
|
|
469 |
final Object elem1 = iter1.next();
|
|
|
470 |
final Object elem2 = iter2.next();
|
|
|
471 |
if (elem1 != elem2)
|
|
|
472 |
return false;
|
|
|
473 |
}
|
|
|
474 |
assert !iter2.hasNext();
|
|
|
475 |
return true;
|
|
|
476 |
}
|
|
|
477 |
|
17 |
ilm |
478 |
/**
|
|
|
479 |
* Convert an array to a list of a different type.
|
|
|
480 |
*
|
|
|
481 |
* @param <U> type of array
|
|
|
482 |
* @param <T> type of list
|
|
|
483 |
* @param array the array to convert, eg new Object[]{"a", "b"}.
|
|
|
484 |
* @param clazz the class of the list items, eg String.class.
|
|
|
485 |
* @return all items of <code>array</code> into a list, eg ["a", "b"].
|
|
|
486 |
* @throws ClassCastException if some item of <code>array</code> is not a <code>T</code>.
|
|
|
487 |
*/
|
|
|
488 |
static public <U, T extends U> List<T> castToList(U[] array, Class<T> clazz) throws ClassCastException {
|
|
|
489 |
final List<T> res = new ArrayList<T>(array.length);
|
|
|
490 |
for (final U item : array) {
|
|
|
491 |
res.add(clazz.cast(item));
|
|
|
492 |
}
|
|
|
493 |
return res;
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
/**
|
142 |
ilm |
497 |
* Cast list
|
|
|
498 |
*
|
|
|
499 |
* @param <E> type of result list
|
|
|
500 |
* @param list the list to convert
|
|
|
501 |
* @param c the class of the result list
|
|
|
502 |
* @return a new ArrayList create from <code>list</code> or null if <code>list</code> is null
|
|
|
503 |
* @throws ClassCastException if some item of <code>list</code> is not a <code>E</code>.
|
|
|
504 |
*/
|
|
|
505 |
public static <E> List<E> castList(final List<?> list, Class<E> c) throws ClassCastException {
|
|
|
506 |
if (list == null) {
|
|
|
507 |
return null;
|
|
|
508 |
}
|
|
|
509 |
|
182 |
ilm |
510 |
final List<E> result;
|
|
|
511 |
if (list instanceof RandomAccess) {
|
|
|
512 |
final int size = list.size();
|
|
|
513 |
result = new ArrayList<>(size);
|
|
|
514 |
for (int i = 0; i < size; i++) {
|
|
|
515 |
result.add(c.cast(list.get(i)));
|
|
|
516 |
}
|
|
|
517 |
} else {
|
|
|
518 |
result = new LinkedList<>();
|
|
|
519 |
for (final Object o : list) {
|
|
|
520 |
result.add(c.cast(o));
|
|
|
521 |
}
|
142 |
ilm |
522 |
}
|
|
|
523 |
return result;
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
/**
|
|
|
527 |
* Cast Map
|
|
|
528 |
*
|
|
|
529 |
* @param <E> type of key
|
|
|
530 |
* @param <F> type of value
|
|
|
531 |
* @param map the map to convert
|
|
|
532 |
* @param cKey the class of key
|
|
|
533 |
* @param cValue the class of value
|
|
|
534 |
* @return a new HashMap create from the <code>map</code> or null if <code>map</code> is null
|
|
|
535 |
* @throws ClassCastException if some item of <code>map</code> have a key which the type is not
|
|
|
536 |
* a <code>E</code> or a value which the type is not a <code>F</code>.
|
|
|
537 |
*/
|
|
|
538 |
public static <E, F> Map<E, F> castMap(final Map<?, ?> map, Class<E> cKey, Class<F> cValue) throws ClassCastException {
|
182 |
ilm |
539 |
return castMap(map, cKey, cValue, HashMap::new);
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
public static <E, F> Map<E, F> castMap(final Map<?, ?> map, Class<E> cKey, Class<F> cValue, final Supplier<Map<E, F>> ctor) throws ClassCastException {
|
142 |
ilm |
543 |
if (map == null) {
|
|
|
544 |
return null;
|
|
|
545 |
}
|
|
|
546 |
|
182 |
ilm |
547 |
final Map<E, F> result = ctor.get();
|
142 |
ilm |
548 |
for (final Entry<?, ?> mapEntry : map.entrySet()) {
|
|
|
549 |
final E key;
|
|
|
550 |
try {
|
|
|
551 |
key = cKey.cast(mapEntry.getKey());
|
|
|
552 |
} catch (final ClassCastException ex) {
|
|
|
553 |
throw new ClassCastException("Key " + mapEntry.getKey().toString() + " is not valid: " + ex.getMessage());
|
|
|
554 |
}
|
|
|
555 |
final F value;
|
|
|
556 |
try {
|
|
|
557 |
value = cValue.cast(mapEntry.getValue());
|
|
|
558 |
} catch (final ClassCastException ex) {
|
|
|
559 |
throw new ClassCastException("Value " + mapEntry.getKey().toString() + " is not valid: " + ex.getMessage());
|
|
|
560 |
}
|
|
|
561 |
result.put(key, value);
|
|
|
562 |
}
|
|
|
563 |
return result;
|
|
|
564 |
}
|
|
|
565 |
|
182 |
ilm |
566 |
public static <T> Stream<T> subclassFilter(final Collection<? super T> coll, final Class<T> subclass) {
|
|
|
567 |
return coll.stream().filter(subclass::isInstance).map(subclass::cast);
|
|
|
568 |
}
|
|
|
569 |
|
|
|
570 |
public static <T> List<T> subclassFilterToList(final Collection<? super T> coll, final Class<T> subclass) {
|
|
|
571 |
return subclassFilter(coll, subclass).collect(Collectors.toList());
|
|
|
572 |
}
|
|
|
573 |
|
142 |
ilm |
574 |
/**
|
17 |
ilm |
575 |
* The number of equals item between a and b, starting from the end.
|
|
|
576 |
*
|
|
|
577 |
* @param <T> type of items.
|
|
|
578 |
* @param a the first list, eg [a, b, c].
|
|
|
579 |
* @param b the second list, eg [a, null, z, c].
|
|
|
580 |
* @return the number of common items, eg 1.
|
|
|
581 |
*/
|
|
|
582 |
public static <T> int equalsFromEnd(final List<T> a, final List<T> b) {
|
|
|
583 |
return equals(a, b, true, null);
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
public static <T> int equalsFromStart(final List<T> a, final List<T> b) {
|
|
|
587 |
return equals(a, b, false, null);
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
/**
|
|
|
591 |
* The number of equals item between a and b, starting from the choosen end.
|
|
|
592 |
*
|
|
|
593 |
* @param <A> type of the first list.
|
|
|
594 |
* @param <B> type of the second list.
|
|
|
595 |
* @param a the first list, eg [a, b, c].
|
|
|
596 |
* @param b the second list, eg [a, null, z, c].
|
|
|
597 |
* @param fromEnd whether search from the start or the end, <code>true</code>.
|
|
|
598 |
* @param transf how items of <code>a</code> should be transformed before being compared, can be
|
|
|
599 |
* <code>null</code>.
|
|
|
600 |
* @return the number of common items, eg 1.
|
|
|
601 |
*/
|
|
|
602 |
public final static <A, B> int equals(final List<A> a, final List<B> b, boolean fromEnd, ITransformer<A, B> transf) {
|
|
|
603 |
final int sizeA = a.size();
|
|
|
604 |
final int sizeB = b.size();
|
|
|
605 |
final int lastI = Math.min(sizeA, sizeB);
|
|
|
606 |
for (int i = 0; i < lastI; i++) {
|
|
|
607 |
final A itemA = a.get(fromEnd ? sizeA - 1 - i : i);
|
|
|
608 |
final B itemB = b.get(fromEnd ? sizeB - 1 - i : i);
|
|
|
609 |
if (!CompareUtils.equals(transf == null ? itemA : transf.transformChecked(itemA), itemB))
|
|
|
610 |
return i;
|
|
|
611 |
}
|
|
|
612 |
return lastI;
|
|
|
613 |
}
|
|
|
614 |
|
83 |
ilm |
615 |
public final static <T> int getEqualsCount(final Iterator<? extends T> a, final Iterator<? extends T> b) {
|
|
|
616 |
return getEqualsCount(a, b, null);
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
public final static <A, B> int getEqualsCount(final Iterator<A> a, final Iterator<B> b, final ITransformer<A, B> transf) {
|
|
|
620 |
int res = 0;
|
|
|
621 |
while (a.hasNext() && b.hasNext()) {
|
|
|
622 |
final A itemA = a.next();
|
|
|
623 |
final B itemB = b.next();
|
|
|
624 |
if (!CompareUtils.equals(transf == null ? itemA : transf.transformChecked(itemA), itemB))
|
|
|
625 |
break;
|
|
|
626 |
res++;
|
|
|
627 |
}
|
|
|
628 |
return res;
|
|
|
629 |
}
|
|
|
630 |
|
67 |
ilm |
631 |
public static <T> Collection<T> select(final Collection<T> a, final IPredicate<? super T> pred) {
|
|
|
632 |
return select(a, pred, new ArrayList<T>());
|
65 |
ilm |
633 |
}
|
|
|
634 |
|
67 |
ilm |
635 |
public static <T, C extends Collection<? super T>> C select(final Collection<T> a, final IPredicate<? super T> pred, final C b) {
|
65 |
ilm |
636 |
for (final T item : a)
|
|
|
637 |
if (pred.evaluateChecked(item))
|
|
|
638 |
b.add(item);
|
|
|
639 |
return b;
|
|
|
640 |
}
|
|
|
641 |
|
17 |
ilm |
642 |
// avoid name collision causing eclipse bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=319603
|
|
|
643 |
@SuppressWarnings("unchecked")
|
|
|
644 |
public static <T> Collection<T> intersection(final Collection<T> a, final Collection<T> b) {
|
|
|
645 |
return org.apache.commons.collections.CollectionUtils.intersection(a, b);
|
|
|
646 |
}
|
|
|
647 |
|
|
|
648 |
/**
|
|
|
649 |
* Compute the intersection of a and b. <code>null</code>s are ignored : x ∩ null = x.
|
|
|
650 |
*
|
|
|
651 |
* @param <T> type of collection.
|
|
|
652 |
* @param a the first set, can be <code>null</code>.
|
|
|
653 |
* @param b the second set, can be <code>null</code>.
|
|
|
654 |
* @return the intersection.
|
|
|
655 |
*/
|
|
|
656 |
@SuppressWarnings("unchecked")
|
|
|
657 |
public static <T> Set<T> inter(final Set<T> a, final Set<T> b) {
|
|
|
658 |
return (Set<T>) interSubtype(a, b);
|
|
|
659 |
}
|
|
|
660 |
|
|
|
661 |
public static <T> Set<? extends T> interSubtype(final Set<? extends T> a, final Set<? extends T> b) {
|
|
|
662 |
if (a == b)
|
|
|
663 |
return a;
|
|
|
664 |
else if (a == null)
|
|
|
665 |
return b;
|
|
|
666 |
else if (b == null)
|
|
|
667 |
return a;
|
|
|
668 |
else if (a.size() > b.size()) {
|
|
|
669 |
return interSubtype(b, a);
|
|
|
670 |
}
|
|
|
671 |
|
|
|
672 |
final Set<T> res = new HashSet<T>();
|
|
|
673 |
for (final T item : a) {
|
|
|
674 |
if (b.contains(item))
|
|
|
675 |
res.add(item);
|
|
|
676 |
}
|
|
|
677 |
return res;
|
|
|
678 |
}
|
|
|
679 |
|
|
|
680 |
public static <T> Set<T> inter(final Set<T>... sets) {
|
|
|
681 |
return inter(Arrays.asList(sets));
|
|
|
682 |
}
|
|
|
683 |
|
|
|
684 |
public static <T> Set<T> inter(final List<Set<T>> sets) {
|
|
|
685 |
final List<Set<T>> mutable = new ArrayList<Set<T>>(sets.size());
|
|
|
686 |
for (final Set<T> s : sets) {
|
|
|
687 |
// ignore nulls
|
|
|
688 |
if (s != null)
|
|
|
689 |
mutable.add(s);
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
if (mutable.isEmpty())
|
|
|
693 |
return null;
|
|
|
694 |
else if (mutable.size() == 1)
|
|
|
695 |
return mutable.get(0);
|
|
|
696 |
|
|
|
697 |
final int indexMin = indexOfMinSize(mutable);
|
|
|
698 |
if (indexMin != 0) {
|
|
|
699 |
mutable.add(0, mutable.remove(indexMin));
|
|
|
700 |
return inter(mutable);
|
|
|
701 |
}
|
|
|
702 |
|
|
|
703 |
if (mutable.get(0).isEmpty())
|
|
|
704 |
return Collections.emptySet();
|
|
|
705 |
|
|
|
706 |
// replace the first 2 by their intersection
|
|
|
707 |
// (inter will swap as appropriate if java doesn't evalute args in source order)
|
|
|
708 |
mutable.add(0, inter(mutable.remove(0), mutable.remove(0)));
|
|
|
709 |
return inter(mutable);
|
|
|
710 |
}
|
|
|
711 |
|
|
|
712 |
private static final <T> int indexOfMinSize(final List<Set<T>> sets) {
|
|
|
713 |
if (sets.isEmpty())
|
|
|
714 |
throw new IllegalArgumentException("empty sets");
|
|
|
715 |
|
|
|
716 |
int res = 0;
|
|
|
717 |
for (int i = 1; i < sets.size(); i++) {
|
|
|
718 |
if (sets.get(i).size() < sets.get(res).size())
|
|
|
719 |
res = i;
|
|
|
720 |
}
|
|
|
721 |
return res;
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
/**
|
|
|
725 |
* Returns a {@link Set} containing the union of the given {@link Set}s.
|
|
|
726 |
*
|
|
|
727 |
* @param <T> type of items.
|
|
|
728 |
* @param a the first set, must not be <code>null</code>
|
|
|
729 |
* @param b the second set, must not be <code>null</code>
|
|
|
730 |
* @return the union of the two.
|
|
|
731 |
*/
|
|
|
732 |
public static <T> Set<T> union(final Set<? extends T> a, final Set<? extends T> b) {
|
|
|
733 |
final Set<T> res = new HashSet<T>(a);
|
|
|
734 |
if (a != b)
|
|
|
735 |
res.addAll(b);
|
|
|
736 |
return res;
|
|
|
737 |
}
|
|
|
738 |
|
132 |
ilm |
739 |
public static <T> Set<T> union(final Collection<? extends Collection<? extends T>> colls) {
|
|
|
740 |
return union(new HashSet<T>(), colls);
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
public static <T> List<T> cat(final Collection<? extends Collection<? extends T>> colls) {
|
|
|
744 |
return union(new ArrayList<T>(), colls);
|
|
|
745 |
}
|
|
|
746 |
|
|
|
747 |
public static <T, C extends Collection<? super T>> C union(final C collector, final Collection<? extends Collection<? extends T>> colls) {
|
149 |
ilm |
748 |
return union(collector, colls, Transformer.<Collection<? extends T>> nopTransformer());
|
132 |
ilm |
749 |
}
|
|
|
750 |
|
|
|
751 |
public static <T, C extends Collection<? super T>, A> C union(final C collector, final Collection<? extends A> colls, final ITransformer<? super A, ? extends Collection<? extends T>> transf) {
|
|
|
752 |
for (final A coll : colls) {
|
|
|
753 |
collector.addAll(transf.transformChecked(coll));
|
|
|
754 |
}
|
|
|
755 |
return collector;
|
|
|
756 |
}
|
|
|
757 |
|
17 |
ilm |
758 |
public static <T> Collection<T> subtract(final Collection<T> a, final Collection<? extends T> b) {
|
174 |
ilm |
759 |
final Collection<T> res = a instanceof IdentitySet ? new LinkedIdentitySet<>(a) : new ArrayList<>(a);
|
|
|
760 |
for (Iterator<? extends T> it = b.iterator(); it.hasNext();) {
|
|
|
761 |
res.remove(it.next());
|
|
|
762 |
}
|
|
|
763 |
return res;
|
17 |
ilm |
764 |
}
|
|
|
765 |
|
|
|
766 |
public static <T> Collection<T> substract(final Collection<T> a, final Collection<? extends T> b) {
|
174 |
ilm |
767 |
return subtract(a, b);
|
17 |
ilm |
768 |
}
|
|
|
769 |
|
83 |
ilm |
770 |
public static final <T> T coalesce(T o1, T o2) {
|
|
|
771 |
return o1 != null ? o1 : o2;
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
public static final <T> T coalesce(T... objects) {
|
|
|
775 |
for (T o : objects) {
|
|
|
776 |
if (o != null)
|
|
|
777 |
return o;
|
|
|
778 |
}
|
|
|
779 |
return null;
|
|
|
780 |
}
|
|
|
781 |
|
17 |
ilm |
782 |
/**
|
132 |
ilm |
783 |
* Return the one and only item of <code>l</code>, otherwise <code>null</code>.
|
17 |
ilm |
784 |
*
|
|
|
785 |
* @param <T> type of list.
|
|
|
786 |
* @param l the list.
|
132 |
ilm |
787 |
* @param atMostOne <code>true</code> if the passed collection must not have more than one item.
|
|
|
788 |
* @return the only item of <code>l</code>, <code>null</code> if there's not exactly one.
|
|
|
789 |
* @throws IllegalArgumentException if <code>atMostOne</code> and <code>l.size() > 1</code>.
|
17 |
ilm |
790 |
*/
|
132 |
ilm |
791 |
public static <T> T getSole(Collection<T> l, final boolean atMostOne) throws IllegalArgumentException {
|
|
|
792 |
final int size = l.size();
|
|
|
793 |
if (atMostOne && size > 1)
|
|
|
794 |
throw new IllegalArgumentException("More than one");
|
|
|
795 |
if (size != 1)
|
|
|
796 |
return null;
|
|
|
797 |
else if (l instanceof List)
|
|
|
798 |
return ((List<T>) l).get(0);
|
|
|
799 |
else
|
|
|
800 |
return l.iterator().next();
|
17 |
ilm |
801 |
}
|
|
|
802 |
|
|
|
803 |
public static <T> T getSole(Collection<T> l) {
|
132 |
ilm |
804 |
return getSole(l, false);
|
17 |
ilm |
805 |
}
|
|
|
806 |
|
|
|
807 |
public static <T> T getFirst(Collection<T> l) {
|
|
|
808 |
return l.size() > 0 ? l.iterator().next() : null;
|
|
|
809 |
}
|
|
|
810 |
|
|
|
811 |
/**
|
|
|
812 |
* Return the first item of <code>l</code> if it isn't empty, otherwise <code>null</code>.
|
|
|
813 |
*
|
|
|
814 |
* @param <T> type of list.
|
|
|
815 |
* @param l the list.
|
|
|
816 |
* @return the first item of <code>l</code> or <code>null</code>.
|
|
|
817 |
*/
|
|
|
818 |
public static <T> T getFirst(List<T> l) {
|
|
|
819 |
return getNoExn(l, 0);
|
|
|
820 |
}
|
|
|
821 |
|
|
|
822 |
/**
|
|
|
823 |
* Return the last item of <code>l</code> if it isn't empty, otherwise <code>null</code>.
|
|
|
824 |
*
|
|
|
825 |
* @param <T> type of list.
|
|
|
826 |
* @param l the list.
|
|
|
827 |
* @return the last item of <code>l</code> or <code>null</code>.
|
|
|
828 |
*/
|
|
|
829 |
public static <T> T getLast(List<T> l) {
|
|
|
830 |
return getNoExn(l, l.size() - 1);
|
|
|
831 |
}
|
|
|
832 |
|
|
|
833 |
/**
|
|
|
834 |
* Return the item no <code>index</code> of <code>l</code> if it exists, otherwise
|
|
|
835 |
* <code>null</code>.
|
|
|
836 |
*
|
|
|
837 |
* @param <T> type of list.
|
|
|
838 |
* @param l the list.
|
|
|
839 |
* @param index the wanted index.
|
|
|
840 |
* @return the corresponding item of <code>l</code> or <code>null</code>.
|
|
|
841 |
*/
|
|
|
842 |
public static <T> T getNoExn(List<T> l, int index) {
|
|
|
843 |
return index >= 0 && index < l.size() ? l.get(index) : null;
|
|
|
844 |
}
|
|
|
845 |
|
73 |
ilm |
846 |
@SuppressWarnings("rawtypes")
|
|
|
847 |
private static final Iterator EMPTY_ITERATOR = new Iterator() {
|
|
|
848 |
@Override
|
|
|
849 |
public boolean hasNext() {
|
|
|
850 |
return false;
|
|
|
851 |
}
|
|
|
852 |
|
|
|
853 |
@Override
|
|
|
854 |
public Object next() {
|
|
|
855 |
throw new NoSuchElementException();
|
|
|
856 |
}
|
|
|
857 |
|
|
|
858 |
@Override
|
|
|
859 |
public void remove() {
|
|
|
860 |
throw new UnsupportedOperationException();
|
|
|
861 |
}
|
|
|
862 |
};
|
|
|
863 |
|
|
|
864 |
@SuppressWarnings("unchecked")
|
|
|
865 |
public static <T> Iterator<T> emptyIterator() {
|
83 |
ilm |
866 |
return EMPTY_ITERATOR;
|
73 |
ilm |
867 |
}
|
|
|
868 |
|
|
|
869 |
public static <T> LinkedList<T> toLinkedList(final Iterator<T> iter) {
|
|
|
870 |
return addTo(iter, new LinkedList<T>());
|
|
|
871 |
}
|
|
|
872 |
|
|
|
873 |
public static <T> ArrayList<T> toArrayList(final Iterator<T> iter, final int estimatedSize) {
|
|
|
874 |
return addTo(iter, new ArrayList<T>(estimatedSize));
|
|
|
875 |
}
|
|
|
876 |
|
|
|
877 |
public static <T, C extends Collection<? super T>> C addTo(final Iterator<T> iter, final C c) {
|
|
|
878 |
while (iter.hasNext())
|
|
|
879 |
c.add(iter.next());
|
|
|
880 |
return c;
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
public static <T> ListIterator<T> getListIterator(final List<T> l, final boolean reversed) {
|
|
|
884 |
if (!reversed)
|
|
|
885 |
return l.listIterator();
|
|
|
886 |
|
|
|
887 |
return reverseListIterator(l.listIterator(l.size()));
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
public static <T> ListIterator<T> reverseListIterator(final ListIterator<T> listIter) {
|
|
|
891 |
if (listIter instanceof ReverseListIter)
|
|
|
892 |
return ((ReverseListIter<T>) listIter).listIter;
|
|
|
893 |
else
|
|
|
894 |
return new ReverseListIter<T>(listIter);
|
|
|
895 |
}
|
|
|
896 |
|
|
|
897 |
private static final class ReverseListIter<T> implements ListIterator<T> {
|
|
|
898 |
private final ListIterator<T> listIter;
|
|
|
899 |
|
|
|
900 |
private ReverseListIter(ListIterator<T> listIter) {
|
|
|
901 |
this.listIter = listIter;
|
|
|
902 |
}
|
|
|
903 |
|
|
|
904 |
@Override
|
|
|
905 |
public boolean hasNext() {
|
|
|
906 |
return this.listIter.hasPrevious();
|
|
|
907 |
}
|
|
|
908 |
|
|
|
909 |
@Override
|
|
|
910 |
public T next() {
|
|
|
911 |
return this.listIter.previous();
|
|
|
912 |
}
|
|
|
913 |
|
|
|
914 |
@Override
|
|
|
915 |
public boolean hasPrevious() {
|
|
|
916 |
return this.listIter.hasNext();
|
|
|
917 |
}
|
|
|
918 |
|
|
|
919 |
@Override
|
|
|
920 |
public T previous() {
|
|
|
921 |
return this.listIter.next();
|
|
|
922 |
}
|
|
|
923 |
|
|
|
924 |
@Override
|
|
|
925 |
public int nextIndex() {
|
|
|
926 |
return this.listIter.previousIndex();
|
|
|
927 |
}
|
|
|
928 |
|
|
|
929 |
@Override
|
|
|
930 |
public int previousIndex() {
|
|
|
931 |
return this.listIter.nextIndex();
|
|
|
932 |
}
|
|
|
933 |
|
|
|
934 |
@Override
|
|
|
935 |
public void remove() {
|
|
|
936 |
this.listIter.remove();
|
|
|
937 |
}
|
|
|
938 |
|
|
|
939 |
@Override
|
|
|
940 |
public void set(T e) {
|
|
|
941 |
this.listIter.set(e);
|
|
|
942 |
}
|
|
|
943 |
|
|
|
944 |
@Override
|
|
|
945 |
public void add(T e) {
|
|
|
946 |
throw new UnsupportedOperationException();
|
|
|
947 |
}
|
|
|
948 |
}
|
|
|
949 |
|
142 |
ilm |
950 |
public static <T> List<T> createList(T item1, T item2) {
|
|
|
951 |
final List<T> res = new ArrayList<T>();
|
|
|
952 |
res.add(item1);
|
|
|
953 |
res.add(item2);
|
|
|
954 |
return res;
|
|
|
955 |
}
|
|
|
956 |
|
|
|
957 |
// workaround for lack of @SafeVarargs in Java 6, TODO use Arrays.asList() in Java 7
|
|
|
958 |
public static <T> List<T> createList(T item1, T item2, T item3) {
|
|
|
959 |
final List<T> res = createList(item1, item2);
|
|
|
960 |
res.add(item3);
|
|
|
961 |
return res;
|
|
|
962 |
}
|
|
|
963 |
|
17 |
ilm |
964 |
public static <T> Set<T> createSet(T... items) {
|
|
|
965 |
return new HashSet<T>(Arrays.asList(items));
|
|
|
966 |
}
|
|
|
967 |
|
73 |
ilm |
968 |
public static <T> IdentitySet<T> createIdentitySet(T... items) {
|
67 |
ilm |
969 |
return new IdentityHashSet<T>(Arrays.asList(items));
|
|
|
970 |
}
|
|
|
971 |
|
|
|
972 |
/**
|
|
|
973 |
* Return an {@link IdentitySet} consisting of <code>items</code>.
|
|
|
974 |
*
|
|
|
975 |
* @param items the collection whose elements are to be in the result.
|
|
|
976 |
* @return a set, possibly <code>items</code> if it's already an identity set.
|
|
|
977 |
*/
|
|
|
978 |
public static <T> Set<T> toIdentitySet(Collection<T> items) {
|
65 |
ilm |
979 |
if (items instanceof IdentitySet)
|
|
|
980 |
return (Set<T>) items;
|
|
|
981 |
else
|
|
|
982 |
return new IdentityHashSet<T>(items);
|
|
|
983 |
}
|
|
|
984 |
|
73 |
ilm |
985 |
@SuppressWarnings("rawtypes")
|
|
|
986 |
private static final IdentitySet EMPTY_SET = new EmptyIdentitySet();
|
|
|
987 |
|
|
|
988 |
@SuppressWarnings("unchecked")
|
|
|
989 |
public static <T> IdentitySet<T> emptyIdentitySet() {
|
83 |
ilm |
990 |
return EMPTY_SET;
|
73 |
ilm |
991 |
}
|
|
|
992 |
|
|
|
993 |
private static final class EmptyIdentitySet extends AbstractSet<Object> implements IdentitySet<Object>, Serializable {
|
|
|
994 |
@Override
|
|
|
995 |
public Iterator<Object> iterator() {
|
|
|
996 |
return emptyIterator();
|
|
|
997 |
}
|
|
|
998 |
|
|
|
999 |
@Override
|
|
|
1000 |
public int size() {
|
|
|
1001 |
return 0;
|
|
|
1002 |
}
|
|
|
1003 |
|
|
|
1004 |
@Override
|
|
|
1005 |
public boolean contains(Object obj) {
|
|
|
1006 |
return false;
|
|
|
1007 |
}
|
|
|
1008 |
|
|
|
1009 |
// Preserves singleton property
|
|
|
1010 |
private Object readResolve() {
|
|
|
1011 |
return EMPTY_SET;
|
|
|
1012 |
}
|
|
|
1013 |
}
|
|
|
1014 |
|
182 |
ilm |
1015 |
@SafeVarargs
|
|
|
1016 |
public static final <T> List<T> toImmutableList(final T... items) {
|
|
|
1017 |
final int length = items.length;
|
|
|
1018 |
if (length == 0)
|
|
|
1019 |
return Collections.emptyList();
|
|
|
1020 |
else if (length == 1)
|
|
|
1021 |
return Collections.singletonList(items[0]);
|
|
|
1022 |
return Collections.unmodifiableList(Arrays.asList(Arrays.copyOf(items, length)));
|
|
|
1023 |
}
|
|
|
1024 |
|
180 |
ilm |
1025 |
public static final <T> List<T> toImmutableList(final Collection<? extends T> coll) {
|
|
|
1026 |
return toImmutableList(coll, ArrayList::new);
|
|
|
1027 |
}
|
|
|
1028 |
|
|
|
1029 |
public static final <T, C extends Collection<? extends T>> List<T> toImmutableList(final C coll, final Function<? super C, ? extends List<T>> createColl) {
|
182 |
ilm |
1030 |
return toImmutableList(coll, createColl, true);
|
|
|
1031 |
}
|
|
|
1032 |
|
|
|
1033 |
/**
|
|
|
1034 |
* Create a new immutable list.
|
|
|
1035 |
*
|
|
|
1036 |
* @param <T> type of items
|
|
|
1037 |
* @param <C> type of collection
|
|
|
1038 |
* @param coll the collection holding the items.
|
|
|
1039 |
* @param createColl how to copy the collection into a new list.
|
|
|
1040 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
|
|
1041 |
* {@link Object#equals(Object)}, in that case {@link Collections#singletonList(Object)}
|
|
|
1042 |
* can be returned.
|
|
|
1043 |
* @return an immutable list with the same items as <code>coll</code>.
|
|
|
1044 |
*/
|
|
|
1045 |
public static final <T, C extends Collection<? extends T>> List<T> toImmutableList(final C coll, final Function<? super C, ? extends List<T>> createColl, final boolean std) {
|
|
|
1046 |
final int size = coll.size();
|
|
|
1047 |
if (size == 0)
|
180 |
ilm |
1048 |
return Collections.emptyList();
|
182 |
ilm |
1049 |
else if (std && size == 1)
|
|
|
1050 |
return Collections.singletonList(coll.iterator().next());
|
180 |
ilm |
1051 |
return Collections.unmodifiableList(createColl.apply(coll));
|
|
|
1052 |
}
|
|
|
1053 |
|
|
|
1054 |
public static final <T> Set<T> toImmutableSet(final Collection<T> coll) {
|
|
|
1055 |
if (coll instanceof SortedSet) {
|
|
|
1056 |
// force TreeSet(SortedSet) to keep Comparator
|
|
|
1057 |
// ATTN see eclipse bug below about wrong constructor, we need SortedSet<T>, not
|
|
|
1058 |
// SortedSet<? extends T>, otherwise "Open Declaration" will match to TreeSet(SortedSet)
|
|
|
1059 |
// but not at runtime.
|
|
|
1060 |
return toImmutableSet((SortedSet<T>) coll, TreeSet::new);
|
|
|
1061 |
} else if (coll instanceof IdentitySet) {
|
182 |
ilm |
1062 |
return toImmutableSet((IdentitySet<? extends T>) coll, LinkedIdentitySet::new, false);
|
180 |
ilm |
1063 |
} else {
|
|
|
1064 |
// In doubt, keep order
|
|
|
1065 |
// ATTN LinkedHashSet extends HashSet
|
|
|
1066 |
return toImmutableSet(coll, coll.getClass() == HashSet.class ? HashSet::new : LinkedHashSet::new);
|
|
|
1067 |
}
|
|
|
1068 |
}
|
|
|
1069 |
|
|
|
1070 |
public static final <T, C extends Collection<? extends T>> Set<T> toImmutableSet(final C coll, final Function<? super C, ? extends Set<T>> createColl) {
|
182 |
ilm |
1071 |
return toImmutableSet(coll, createColl, true);
|
|
|
1072 |
|
|
|
1073 |
}
|
|
|
1074 |
|
|
|
1075 |
/**
|
|
|
1076 |
* Create a new immutable set.
|
|
|
1077 |
*
|
|
|
1078 |
* @param <T> type of items
|
|
|
1079 |
* @param <C> type of collection
|
|
|
1080 |
* @param coll the collection holding the items.
|
|
|
1081 |
* @param createColl how to copy the collection into a new list.
|
|
|
1082 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
|
|
1083 |
* {@link Object#equals(Object)}, in that case {@link Collections#singleton(Object)} can
|
|
|
1084 |
* be returned.
|
|
|
1085 |
* @return an immutable set with the same items as <code>coll</code>.
|
|
|
1086 |
*/
|
|
|
1087 |
public static final <T, C extends Collection<? extends T>> Set<T> toImmutableSet(final C coll, final Function<? super C, ? extends Set<T>> createColl, final boolean std) {
|
|
|
1088 |
final int size = coll.size();
|
|
|
1089 |
if (size == 0)
|
180 |
ilm |
1090 |
return Collections.emptySet();
|
182 |
ilm |
1091 |
else if (std && size == 1)
|
|
|
1092 |
return Collections.singleton(coll.iterator().next());
|
180 |
ilm |
1093 |
final Set<T> res = createColl.apply(coll);
|
|
|
1094 |
return Collections.unmodifiableSet(res);
|
|
|
1095 |
}
|
|
|
1096 |
|
|
|
1097 |
/**
|
|
|
1098 |
* Return an immutable map equal to the passed one.
|
|
|
1099 |
*
|
|
|
1100 |
* @param <K> type of keys.
|
|
|
1101 |
* @param <V> type of values.
|
|
|
1102 |
* @param map the map, not "? extends K" to be able to copy {@link SortedMap}.
|
|
|
1103 |
* @return an immutable map.
|
|
|
1104 |
*/
|
|
|
1105 |
public static final <K, V> Map<K, V> toImmutableMap(final Map<K, ? extends V> map) {
|
|
|
1106 |
if (map instanceof SortedMap) {
|
|
|
1107 |
// force TreeMap(SortedMap) to keep Comparator
|
|
|
1108 |
// ATTN see eclipse bug below about wrong constructor
|
|
|
1109 |
return toImmutableMap((SortedMap<K, ? extends V>) map, TreeMap::new);
|
|
|
1110 |
} else if (map instanceof IdentityHashMap) {
|
182 |
ilm |
1111 |
return toImmutableMap((IdentityHashMap<? extends K, ? extends V>) map, IdentityHashMap::new, false);
|
180 |
ilm |
1112 |
} else {
|
|
|
1113 |
// In doubt, keep order
|
|
|
1114 |
// ATTN LinkedHashMap extends HashMap
|
|
|
1115 |
return toImmutableMap(map, map.getClass() == HashMap.class ? HashMap::new : LinkedHashMap::new);
|
|
|
1116 |
}
|
|
|
1117 |
}
|
|
|
1118 |
|
182 |
ilm |
1119 |
public static final <K, V, InMap extends Map<? extends K, ? extends V>> Map<K, V> toImmutableMap(final InMap map, final Function<? super InMap, ? extends Map<K, V>> copyFunction) {
|
|
|
1120 |
return toImmutableMap(map, copyFunction, true);
|
|
|
1121 |
}
|
|
|
1122 |
|
180 |
ilm |
1123 |
/**
|
|
|
1124 |
* Return an immutable map with the same entries as the passed one. NOTE: <code>copyMap</code>
|
|
|
1125 |
* <strong>must</strong> copy the entries so that a modification of <code>map</code> doesn't
|
|
|
1126 |
* affect the copy.
|
|
|
1127 |
*
|
|
|
1128 |
* @param <K> type of keys.
|
|
|
1129 |
* @param <V> type of values.
|
|
|
1130 |
* @param <InMap> type of passed map, ATTN if {@link SortedMap} eclipse "Open Declaration"
|
|
|
1131 |
* matches <code>TreeMap::new</code> to {@link TreeMap#TreeMap(SortedMap)} ignoring
|
|
|
1132 |
* generic type (i.e. it is declared with "K" but we pass "? extends K") but
|
|
|
1133 |
* {@link TreeMap#TreeMap(Map)} is (correctly) executed at runtime.
|
|
|
1134 |
* @param map the map.
|
|
|
1135 |
* @param copyFunction how to copy the passed map.
|
182 |
ilm |
1136 |
* @param std <code>true</code> if the created <code>createColl</code> use
|
|
|
1137 |
* {@link Object#equals(Object)}, in that case
|
|
|
1138 |
* {@link Collections#singletonMap(Object, Object)} can be returned.
|
180 |
ilm |
1139 |
* @return an immutable map.
|
|
|
1140 |
*/
|
182 |
ilm |
1141 |
public static final <K, V, InMap extends Map<? extends K, ? extends V>> Map<K, V> toImmutableMap(final InMap map, final Function<? super InMap, ? extends Map<K, V>> copyFunction,
|
|
|
1142 |
final boolean std) {
|
|
|
1143 |
final int size = map.size();
|
|
|
1144 |
if (size == 0) {
|
180 |
ilm |
1145 |
return Collections.emptyMap();
|
182 |
ilm |
1146 |
} else if (std && size == 1) {
|
|
|
1147 |
final Entry<? extends K, ? extends V> e = map.entrySet().iterator().next();
|
|
|
1148 |
return Collections.singletonMap(e.getKey(), e.getValue());
|
|
|
1149 |
}
|
180 |
ilm |
1150 |
return Collections.unmodifiableMap(copyFunction.apply(map));
|
|
|
1151 |
}
|
|
|
1152 |
|
17 |
ilm |
1153 |
public static <K, V> Map<K, V> createMap(K key, V val, K key2, V val2) {
|
80 |
ilm |
1154 |
// arguments are ordered, so should the result
|
|
|
1155 |
final Map<K, V> res = new LinkedHashMap<K, V>();
|
17 |
ilm |
1156 |
res.put(key, val);
|
|
|
1157 |
res.put(key2, val2);
|
|
|
1158 |
return res;
|
|
|
1159 |
}
|
|
|
1160 |
|
|
|
1161 |
public static <K, V> Map<K, V> createMap(K key, V val, K key2, V val2, K key3, V val3) {
|
|
|
1162 |
final Map<K, V> res = createMap(key, val, key2, val2);
|
|
|
1163 |
res.put(key3, val3);
|
|
|
1164 |
return res;
|
|
|
1165 |
}
|
|
|
1166 |
|
149 |
ilm |
1167 |
@SafeVarargs
|
|
|
1168 |
public static <T> Map<T, T> createMapFromList(final T... keyAndVal) {
|
|
|
1169 |
return createMapFromList(Arrays.asList(keyAndVal));
|
|
|
1170 |
}
|
|
|
1171 |
|
|
|
1172 |
public static <T> Map<T, T> createMapFromList(final List<T> keyAndVal) {
|
|
|
1173 |
final int size = keyAndVal.size();
|
|
|
1174 |
if (size % 2 != 0)
|
|
|
1175 |
throw new IllegalArgumentException(size + " is not an even number of values : " + keyAndVal);
|
|
|
1176 |
// arguments are ordered, so should the result
|
|
|
1177 |
final Map<T, T> res = new LinkedHashMap<>(size);
|
|
|
1178 |
for (int i = 0; i < size; i += 2) {
|
|
|
1179 |
res.put(keyAndVal.get(i), keyAndVal.get(i + 1));
|
|
|
1180 |
}
|
|
|
1181 |
return res;
|
|
|
1182 |
}
|
|
|
1183 |
|
182 |
ilm |
1184 |
// same as HashMap and HashSet
|
|
|
1185 |
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
|
|
1186 |
|
|
|
1187 |
public static interface MapConstructor<K, V, M extends Map<K, V>> {
|
|
|
1188 |
M create(int initialCapacity, float loadFactor);
|
|
|
1189 |
}
|
|
|
1190 |
|
|
|
1191 |
public static final int getCapacity(final int plannedSize, final float loadFactor) {
|
|
|
1192 |
return Math.max((int) Math.ceil(plannedSize / loadFactor), 4);
|
|
|
1193 |
}
|
|
|
1194 |
|
|
|
1195 |
public static final <K, V> LinkedHashMap<K, V> newLinkedHashMap(final int plannedSize) {
|
|
|
1196 |
return newMap(plannedSize, DEFAULT_LOAD_FACTOR, (MapConstructor<K, V, LinkedHashMap<K, V>>) LinkedHashMap::new);
|
|
|
1197 |
}
|
|
|
1198 |
|
|
|
1199 |
public static final <K, V> HashMap<K, V> newHashMap(final int plannedSize) {
|
|
|
1200 |
return newHashMap(plannedSize, DEFAULT_LOAD_FACTOR);
|
|
|
1201 |
}
|
|
|
1202 |
|
17 |
ilm |
1203 |
/**
|
182 |
ilm |
1204 |
* Create a new map with no rehash operations until at least a certain size.
|
|
|
1205 |
*
|
|
|
1206 |
* @param <K> the type of keys maintained by the new map
|
|
|
1207 |
* @param <V> the type of mapped values
|
|
|
1208 |
* @param plannedSize the number of entries expected to be added to the returned map.
|
|
|
1209 |
* @param loadFactor the load factor of the returned map, not advised to be <code>1.0f</code>
|
|
|
1210 |
* because it increases the chances of hash collisions.
|
|
|
1211 |
* @return a new map that won't rehash until at least <code>plannedSize</code> items.
|
|
|
1212 |
*/
|
|
|
1213 |
public static final <K, V> HashMap<K, V> newHashMap(final int plannedSize, final float loadFactor) {
|
|
|
1214 |
return newMap(plannedSize, loadFactor, (MapConstructor<K, V, HashMap<K, V>>) HashMap::new);
|
|
|
1215 |
}
|
|
|
1216 |
|
|
|
1217 |
/**
|
|
|
1218 |
* Create a new map with no rehash operations until at least a certain size.
|
|
|
1219 |
*
|
|
|
1220 |
* @param <K> the type of keys maintained by the new map
|
|
|
1221 |
* @param <V> the type of mapped values
|
|
|
1222 |
* @param <M> the type of the new map
|
|
|
1223 |
* @param plannedSize the number of entries expected to be added to the returned map
|
|
|
1224 |
* @param loadFactor the load factor, passed to <code>ctor</code>.
|
|
|
1225 |
* @param ctor how to create the new map, will be passed the computed initial capacity and
|
|
|
1226 |
* <code>loadFactor</code>, e.g. {@link LinkedHashMap#LinkedHashMap(int, float)
|
|
|
1227 |
* LinkedHashMap::new}.
|
|
|
1228 |
* @return a new map that won't rehash until at least <code>plannedSize</code> items.
|
|
|
1229 |
*/
|
|
|
1230 |
public static final <K, V, M extends Map<K, V>> M newMap(final int plannedSize, final float loadFactor, final MapConstructor<K, V, M> ctor) {
|
|
|
1231 |
return ctor.create(getCapacity(plannedSize, loadFactor), loadFactor);
|
|
|
1232 |
}
|
|
|
1233 |
|
|
|
1234 |
public static interface SetConstructor<V, S extends Set<V>> {
|
|
|
1235 |
S create(int initialCapacity, float loadFactor);
|
|
|
1236 |
}
|
|
|
1237 |
|
|
|
1238 |
public static final <V> LinkedHashSet<V> newLinkedHashSet(final int plannedSize) {
|
|
|
1239 |
return newSet(plannedSize, DEFAULT_LOAD_FACTOR, (SetConstructor<V, LinkedHashSet<V>>) LinkedHashSet::new);
|
|
|
1240 |
}
|
|
|
1241 |
|
|
|
1242 |
public static final <V> HashSet<V> newHashSet(final int plannedSize) {
|
|
|
1243 |
return newHashSet(plannedSize, DEFAULT_LOAD_FACTOR);
|
|
|
1244 |
}
|
|
|
1245 |
|
|
|
1246 |
public static final <V> HashSet<V> newHashSet(final int plannedSize, final float loadFactor) {
|
|
|
1247 |
return newSet(plannedSize, loadFactor, (SetConstructor<V, HashSet<V>>) HashSet::new);
|
|
|
1248 |
}
|
|
|
1249 |
|
|
|
1250 |
public static final <V, S extends Set<V>> S newSet(final int plannedSize, final float loadFactor, final SetConstructor<V, S> ctor) {
|
|
|
1251 |
return ctor.create(getCapacity(plannedSize, loadFactor), loadFactor);
|
|
|
1252 |
}
|
|
|
1253 |
|
|
|
1254 |
/**
|
17 |
ilm |
1255 |
* Creates a map with null values.
|
|
|
1256 |
*
|
|
|
1257 |
* @param <K> type of key.
|
|
|
1258 |
* @param <V> type of value.
|
|
|
1259 |
* @param keys the keys of the map.
|
174 |
ilm |
1260 |
* @return a new ordered map.
|
17 |
ilm |
1261 |
*/
|
|
|
1262 |
public static <K, V> Map<K, V> createMap(Collection<? extends K> keys) {
|
174 |
ilm |
1263 |
// there's no way to tell if a Collection is ordered, so always use ordered map.
|
|
|
1264 |
return fillMap(new LinkedHashMap<K, V>(keys.size()), keys);
|
17 |
ilm |
1265 |
}
|
|
|
1266 |
|
|
|
1267 |
/**
|
|
|
1268 |
* Fills a map with null values.
|
|
|
1269 |
*
|
|
|
1270 |
* @param <K> type of key.
|
|
|
1271 |
* @param <V> type of value.
|
|
|
1272 |
* @param <M> type of map.
|
|
|
1273 |
* @param m the map to fill.
|
|
|
1274 |
* @param keys the keys to add.
|
|
|
1275 |
* @return the passed map.
|
|
|
1276 |
*/
|
|
|
1277 |
public static <K, V, M extends Map<K, V>> M fillMap(final M m, Collection<? extends K> keys) {
|
80 |
ilm |
1278 |
return fillMap(m, keys, null);
|
|
|
1279 |
}
|
|
|
1280 |
|
|
|
1281 |
/**
|
|
|
1282 |
* Fills a map with the same value.
|
|
|
1283 |
*
|
|
|
1284 |
* @param <K> type of key.
|
|
|
1285 |
* @param <V> type of value.
|
|
|
1286 |
* @param <M> type of map.
|
|
|
1287 |
* @param m the map to fill.
|
|
|
1288 |
* @param keys the keys to add.
|
83 |
ilm |
1289 |
* @param val the value to put.
|
80 |
ilm |
1290 |
* @return the passed map.
|
|
|
1291 |
*/
|
|
|
1292 |
public static <K, V, M extends Map<K, V>> M fillMap(final M m, final Collection<? extends K> keys, final V val) {
|
17 |
ilm |
1293 |
for (final K key : keys)
|
80 |
ilm |
1294 |
m.put(key, val);
|
17 |
ilm |
1295 |
return m;
|
|
|
1296 |
}
|
132 |
ilm |
1297 |
|
|
|
1298 |
public static <K, V, M extends Map<K, V>> M invertMap(final M m, final Map<? extends V, ? extends K> source) {
|
|
|
1299 |
for (final Entry<? extends V, ? extends K> e : source.entrySet()) {
|
|
|
1300 |
m.put(e.getValue(), e.getKey());
|
|
|
1301 |
}
|
|
|
1302 |
return m;
|
|
|
1303 |
}
|
|
|
1304 |
|
|
|
1305 |
public static <K, V, M extends CollectionMap2Itf<K, ?, V>> M invertMap(final M m, final Map<? extends V, ? extends K> source) {
|
|
|
1306 |
for (final Entry<? extends V, ? extends K> e : source.entrySet()) {
|
|
|
1307 |
m.add(e.getValue(), e.getKey());
|
|
|
1308 |
}
|
|
|
1309 |
return m;
|
|
|
1310 |
}
|
17 |
ilm |
1311 |
}
|