|
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 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.collect; |
|
36 |
| |
|
37 |
| import java.util.*; |
|
38 |
| import java.util.concurrent.CopyOnWriteArrayList; |
|
39 |
| import java.util.concurrent.CopyOnWriteArraySet; |
|
40 |
| import java.io.Serializable; |
|
41 |
| import edu.rice.cs.plt.lambda.*; |
|
42 |
| import edu.rice.cs.plt.tuple.Option; |
|
43 |
| import edu.rice.cs.plt.tuple.Pair; |
|
44 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
45 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
46 |
| import edu.rice.cs.plt.object.ObjectUtil; |
|
47 |
| |
|
48 |
| public final class CollectUtil { |
|
49 |
| |
|
50 |
| |
|
51 |
0
| private CollectUtil() {}
|
|
52 |
| |
|
53 |
| |
|
54 |
| public static final Predicate2<Set<?>, Object> SET_CONTENTS_PREDICATE = new SetContentsPredicate(); |
|
55 |
| |
|
56 |
| private static final class SetContentsPredicate implements Predicate2<Set<?>, Object>, Serializable { |
|
57 |
16
| private SetContentsPredicate() {}
|
|
58 |
0
| public boolean contains(Set<?> set, Object val) { return set.contains(val); }
|
|
59 |
| } |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
1
| @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> hashSetFactory() {
|
|
66 |
1
| return (Thunk<Set<T>>) (Thunk<?>) DefaultHashSetFactory.INSTANCE;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| private static final class DefaultHashSetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
70 |
| public static final DefaultHashSetFactory<Object> INSTANCE = new DefaultHashSetFactory<Object>(); |
|
71 |
1
| private DefaultHashSetFactory() {}
|
|
72 |
1
| public Set<T> value() { return new HashSet<T>(); }
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
3
| public static <T> Thunk<Set<T>> hashSetFactory(int initialCapacity) {
|
|
80 |
3
| return new CustomHashSetFactory<T>(initialCapacity);
|
|
81 |
| } |
|
82 |
| |
|
83 |
| private static final class CustomHashSetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
84 |
| private final int _initialCapacity; |
|
85 |
3
| public CustomHashSetFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
|
|
86 |
7
| public Set<T> value() { return new HashSet<T>(_initialCapacity); }
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
0
| @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> linkedHashSetFactory() {
|
|
94 |
0
| return (Thunk<Set<T>>) (Thunk<?>) DefaultLinkedHashSetFactory.INSTANCE;
|
|
95 |
| } |
|
96 |
| |
|
97 |
| private static final class DefaultLinkedHashSetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
98 |
| public static final DefaultLinkedHashSetFactory<Object> INSTANCE = new DefaultLinkedHashSetFactory<Object>(); |
|
99 |
0
| private DefaultLinkedHashSetFactory() {}
|
|
100 |
0
| public Set<T> value() { return new LinkedHashSet<T>(); }
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
0
| public static <T> Thunk<Set<T>> linkedHashSetFactory(int initialCapacity) {
|
|
108 |
0
| return new CustomLinkedHashSetFactory<T>(initialCapacity);
|
|
109 |
| } |
|
110 |
| |
|
111 |
| private static final class CustomLinkedHashSetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
112 |
| private final int _initialCapacity; |
|
113 |
0
| public CustomLinkedHashSetFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
|
|
114 |
0
| public Set<T> value() { return new LinkedHashSet<T>(_initialCapacity); }
|
|
115 |
| } |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
3
| @SuppressWarnings("unchecked")
|
|
122 |
| public static <T extends Comparable<? super T>> Thunk<Set<T>> treeSetFactory() { |
|
123 |
| |
|
124 |
3
| return (Thunk<Set<T>>) (Thunk<? extends Set<?>>) DefaultTreeSetFactory.INSTANCE;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| private static final class DefaultTreeSetFactory<T extends Comparable<? super T>> |
|
128 |
| implements Thunk<Set<T>>, Serializable { |
|
129 |
| public static final DefaultTreeSetFactory<String> INSTANCE = new DefaultTreeSetFactory<String>(); |
|
130 |
1
| private DefaultTreeSetFactory() {}
|
|
131 |
9
| public Set<T> value() { return new TreeSet<T>(); }
|
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
0
| public static <T> Thunk<Set<T>> treeSetFactory(Comparator<? super T> comparator) {
|
|
139 |
0
| return new CustomTreeSetFactory<T>(comparator);
|
|
140 |
| } |
|
141 |
| |
|
142 |
| private static final class CustomTreeSetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
143 |
| private final Comparator<? super T> _comp; |
|
144 |
0
| public CustomTreeSetFactory(Comparator<? super T> comp) { _comp = comp; }
|
|
145 |
0
| public Set<T> value() { return new TreeSet<T>(_comp); }
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
0
| @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> copyOnWriteArraySetFactory() {
|
|
153 |
0
| return (Thunk<Set<T>>) (Thunk<?>) CopyOnWriteArraySetFactory.INSTANCE;
|
|
154 |
| } |
|
155 |
| |
|
156 |
| private static final class CopyOnWriteArraySetFactory<T> implements Thunk<Set<T>>, Serializable { |
|
157 |
| public static final CopyOnWriteArraySetFactory<Object> INSTANCE = new CopyOnWriteArraySetFactory<Object>(); |
|
158 |
0
| private CopyOnWriteArraySetFactory() {}
|
|
159 |
0
| public Set<T> value() { return new CopyOnWriteArraySet<T>(); }
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
3
| @SuppressWarnings("unchecked") public static <K, V> Thunk<Map<K, V>> hashMapFactory() {
|
|
167 |
3
| return (Thunk<Map<K, V>>) (Thunk<?>) DefaultHashMapFactory.INSTANCE;
|
|
168 |
| } |
|
169 |
| |
|
170 |
| private static final class DefaultHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable { |
|
171 |
| public static final DefaultHashMapFactory<Object, Object> INSTANCE = |
|
172 |
| new DefaultHashMapFactory<Object, Object>(); |
|
173 |
1
| private DefaultHashMapFactory() {}
|
|
174 |
3
| public Map<K, V> value() { return new HashMap<K, V>(); }
|
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
0
| public static <K, V> Thunk<Map<K, V>> hashMapFactory(int initialCapacity) {
|
|
182 |
0
| return new CustomHashMapFactory<K, V>(initialCapacity);
|
|
183 |
| } |
|
184 |
| |
|
185 |
| private static final class CustomHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable { |
|
186 |
| private final int _initialCapacity; |
|
187 |
0
| public CustomHashMapFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
|
|
188 |
0
| public Map<K, V> value() { return new HashMap<K, V>(_initialCapacity); }
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
0
| @SuppressWarnings("unchecked") public static <K, V> Thunk<Map<K, V>> linkedHashMapFactory() {
|
|
196 |
0
| return (Thunk<Map<K, V>>) (Thunk<?>) DefaultLinkedHashMapFactory.INSTANCE;
|
|
197 |
| } |
|
198 |
| |
|
199 |
| private static final class DefaultLinkedHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable { |
|
200 |
| public static final DefaultLinkedHashMapFactory<Object, Object> INSTANCE = |
|
201 |
| new DefaultLinkedHashMapFactory<Object, Object>(); |
|
202 |
0
| private DefaultLinkedHashMapFactory() {}
|
|
203 |
0
| public Map<K, V> value() { return new LinkedHashMap<K, V>(); }
|
|
204 |
| } |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
0
| public static <K, V> Thunk<Map<K, V>> linkedHashMapFactory(int initialCapacity) {
|
|
211 |
0
| return new CustomLinkedHashMapFactory<K, V>(initialCapacity);
|
|
212 |
| } |
|
213 |
| |
|
214 |
| private static final class CustomLinkedHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable { |
|
215 |
| private final int _initialCapacity; |
|
216 |
0
| public CustomLinkedHashMapFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
|
|
217 |
0
| public Map<K, V> value() { return new LinkedHashMap<K, V>(_initialCapacity); }
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
3
| @SuppressWarnings("unchecked")
|
|
225 |
| public static <K extends Comparable<? super K>, V> Thunk<Map<K, V>> treeMapFactory() { |
|
226 |
| |
|
227 |
3
| return (Thunk<Map<K, V>>) (Thunk<? extends Map<?, ?>>) DefaultTreeMapFactory.INSTANCE;
|
|
228 |
| } |
|
229 |
| |
|
230 |
| private static final class DefaultTreeMapFactory<K extends Comparable<? super K>, V> |
|
231 |
| implements Thunk<Map<K, V>>, Serializable { |
|
232 |
| public static final DefaultTreeMapFactory<String, Object> INSTANCE = |
|
233 |
| new DefaultTreeMapFactory<String, Object>(); |
|
234 |
1
| private DefaultTreeMapFactory() {}
|
|
235 |
3
| public Map<K, V> value() { return new TreeMap<K, V>(); }
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
0
| public static <K, V> Thunk<Map<K, V>> treeMapFactory(Comparator<? super K> comparator) {
|
|
243 |
0
| return new CustomTreeMapFactory<K, V>(comparator);
|
|
244 |
| } |
|
245 |
| |
|
246 |
| private static final class CustomTreeMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable { |
|
247 |
| private final Comparator<? super K> _comp; |
|
248 |
0
| public CustomTreeMapFactory(Comparator<? super K> comp) { _comp = comp; }
|
|
249 |
0
| public Map<K, V> value() { return new TreeMap<K, V>(_comp); }
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
0
| @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> arrayListFactory() {
|
|
257 |
0
| return (Thunk<List<T>>) (Thunk<?>) DefaultArrayListFactory.INSTANCE;
|
|
258 |
| } |
|
259 |
| |
|
260 |
| private static final class DefaultArrayListFactory<T> implements Thunk<List<T>>, Serializable { |
|
261 |
| public static final DefaultArrayListFactory<Object> INSTANCE = new DefaultArrayListFactory<Object>(); |
|
262 |
0
| private DefaultArrayListFactory() {}
|
|
263 |
0
| public List<T> value() { return new ArrayList<T>(); }
|
|
264 |
| } |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
0
| public static <T> Thunk<List<T>> arrayListFactory(int initialCapacity) {
|
|
271 |
0
| return new CustomArrayListFactory<T>(initialCapacity);
|
|
272 |
| } |
|
273 |
| |
|
274 |
| private static final class CustomArrayListFactory<T> implements Thunk<List<T>>, Serializable { |
|
275 |
| private final int _initialCapacity; |
|
276 |
0
| public CustomArrayListFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
|
|
277 |
0
| public List<T> value() { return new ArrayList<T>(_initialCapacity); }
|
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
0
| @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> linkedListFactory() {
|
|
285 |
0
| return (Thunk<List<T>>) (Thunk<?>) DefaultLinkedListFactory.INSTANCE;
|
|
286 |
| } |
|
287 |
| |
|
288 |
| private static final class DefaultLinkedListFactory<T> implements Thunk<List<T>>, Serializable { |
|
289 |
| public static final DefaultLinkedListFactory<Object> INSTANCE = new DefaultLinkedListFactory<Object>(); |
|
290 |
0
| private DefaultLinkedListFactory() {}
|
|
291 |
0
| public List<T> value() { return new LinkedList<T>(); }
|
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
0
| @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> copyOnWriteArrayListFactory() {
|
|
299 |
0
| return (Thunk<List<T>>) (Thunk<?>) CopyOnWriteArrayListFactory.INSTANCE;
|
|
300 |
| } |
|
301 |
| |
|
302 |
| private static final class CopyOnWriteArrayListFactory<T> implements Thunk<List<T>>, Serializable { |
|
303 |
| public static final CopyOnWriteArrayListFactory<Object> INSTANCE = new CopyOnWriteArrayListFactory<Object>(); |
|
304 |
0
| private CopyOnWriteArrayListFactory() {}
|
|
305 |
0
| public List<T> value() { return new CopyOnWriteArrayList<T>(); }
|
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
158
| public static <T> PredicateSet<T> makeSet(T... elements) {
|
|
313 |
158
| return makeSet(IterUtil.asIterable(elements));
|
|
314 |
| } |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
158
| public static <T> PredicateSet<T> makeSet(Iterable<? extends T> elements) {
|
|
321 |
158
| if (IterUtil.isEmpty(elements)) {
|
|
322 |
0
| return EmptySet.make();
|
|
323 |
| } |
|
324 |
158
| else if (IterUtil.sizeOf(elements, 2) == 1) {
|
|
325 |
84
| return new SingletonSet<T>(IterUtil.first(elements));
|
|
326 |
| } |
|
327 |
| else { |
|
328 |
74
| Set<T> result = new LinkedHashSet<T>(asCollection(elements));
|
|
329 |
74
| return new ImmutableSet<T>(result) {
|
|
330 |
0
| @Override public boolean hasFixedSize() { return true; }
|
|
331 |
0
| @Override public boolean isStatic() { return true; }
|
|
332 |
| }; |
|
333 |
| } |
|
334 |
| } |
|
335 |
| |
|
336 |
| |
|
337 |
0
| public static <T> PredicateSet<T> makeSet(Option<? extends T> opt) {
|
|
338 |
0
| if (opt.isSome()) { return new SingletonSet<T>(opt.unwrap()); }
|
|
339 |
0
| else { return EmptySet.make(); }
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
0
| public static <T1, T2> Relation<T1, T2> makeRelation(Iterable<? extends Pair<? extends T1, ? extends T2>> pairs) {
|
|
347 |
0
| if (IterUtil.isEmpty(pairs)) {
|
|
348 |
0
| return EmptyRelation.make();
|
|
349 |
| } |
|
350 |
0
| else if (IterUtil.sizeOf(pairs, 2) == 1) {
|
|
351 |
0
| Pair<? extends T1, ? extends T2> elt = IterUtil.first(pairs);
|
|
352 |
0
| return new SingletonRelation<T1, T2>(elt.first(), elt.second());
|
|
353 |
| } |
|
354 |
| else { |
|
355 |
0
| Relation<T1, T2> result = IndexedRelation.makeLinkedHashBased();
|
|
356 |
0
| for (Pair<? extends T1, ? extends T2> elt : pairs) {
|
|
357 |
0
| result.add(elt.first(), elt.second());
|
|
358 |
| } |
|
359 |
0
| return new ImmutableRelation<T1, T2>(result) {
|
|
360 |
0
| @Override public boolean hasFixedSize() { return true; }
|
|
361 |
0
| @Override public boolean isStatic() { return true; }
|
|
362 |
| }; |
|
363 |
| } |
|
364 |
| } |
|
365 |
| |
|
366 |
| |
|
367 |
0
| public static <T> List<T> makeList(Iterable<? extends T> iter) {
|
|
368 |
0
| return makeArrayList(iter);
|
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
0
| public static <T> ArrayList<T> makeArrayList(Iterable<? extends T> iter) {
|
|
373 |
0
| if (iter instanceof Collection<?>) {
|
|
374 |
0
| @SuppressWarnings("unchecked")
|
|
375 |
| Collection<? extends T> cast = (Collection<? extends T>) iter; |
|
376 |
0
| return new ArrayList<T>(cast);
|
|
377 |
| } |
|
378 |
0
| else if (iter instanceof SizedIterable<?>) {
|
|
379 |
0
| ArrayList<T> result = new ArrayList<T>(((SizedIterable<?>) iter).size());
|
|
380 |
0
| for (T e : iter) { result.add(e); }
|
|
381 |
0
| return result;
|
|
382 |
| } |
|
383 |
| else { |
|
384 |
0
| ArrayList<T> result = new ArrayList<T>();
|
|
385 |
0
| for (T e : iter) { result.add(e); }
|
|
386 |
0
| return result;
|
|
387 |
| } |
|
388 |
| } |
|
389 |
| |
|
390 |
| |
|
391 |
10
| public static <T> LinkedList<T> makeLinkedList(Iterable<? extends T> iter) {
|
|
392 |
10
| if (iter instanceof Collection<?>) {
|
|
393 |
10
| @SuppressWarnings("unchecked")
|
|
394 |
| Collection<? extends T> cast = (Collection<? extends T>) iter; |
|
395 |
10
| return new LinkedList<T>(cast);
|
|
396 |
| } |
|
397 |
| else { |
|
398 |
0
| LinkedList<T> result = new LinkedList<T>();
|
|
399 |
0
| for (T e : iter) { result.add(e); }
|
|
400 |
0
| return result;
|
|
401 |
| } |
|
402 |
| } |
|
403 |
| |
|
404 |
| |
|
405 |
0
| public static <T> ConsList<T> makeConsList(Iterable<? extends T> iter) {
|
|
406 |
0
| ConsList<T> result = ConsList.empty();
|
|
407 |
0
| for (T elt : IterUtil.reverse(iter)) { result = ConsList.cons(elt, result); }
|
|
408 |
0
| return result;
|
|
409 |
| } |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
0
| @SuppressWarnings("unchecked") public static <T> List<T> emptyList() {
|
|
416 |
0
| return (List<T>) Collections.EMPTY_LIST;
|
|
417 |
| } |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
2
| @SuppressWarnings("unchecked") public static <T> EmptySet<T> emptySet() {
|
|
424 |
2
| return (EmptySet<T>) EmptySet.INSTANCE;
|
|
425 |
| } |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
0
| @SuppressWarnings("unchecked") public static <K, V> EmptyMap<K, V> emptyMap() {
|
|
432 |
0
| return (EmptyMap<K, V>) EmptyMap.INSTANCE;
|
|
433 |
| } |
|
434 |
| |
|
435 |
| |
|
436 |
0
| @SuppressWarnings("unchecked") public static <T1, T2> EmptyRelation<T1, T2> emptyRelation() {
|
|
437 |
0
| return (EmptyRelation<T1, T2>) EmptyRelation.INSTANCE;
|
|
438 |
| } |
|
439 |
| |
|
440 |
| |
|
441 |
0
| public static <T> SingletonSet<T> singleton(T elt) {
|
|
442 |
0
| return new SingletonSet<T>(elt);
|
|
443 |
| } |
|
444 |
| |
|
445 |
| |
|
446 |
0
| public static <T1, T2> SingletonRelation<T1, T2> singleton(T1 first, T2 second) {
|
|
447 |
0
| return new SingletonRelation<T1, T2>(first, second);
|
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
0
| public static <K, V> SingletonMap<K, V> singletonMap(K key, V value) {
|
|
452 |
0
| return new SingletonMap<K, V>(key, value);
|
|
453 |
| } |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
0
| public static <T> Set<T> asSet(Iterable<T> iter) {
|
|
460 |
0
| if (iter instanceof Set<?>) { return (Set<T>) iter; }
|
|
461 |
0
| else { return new IterableSet<T>(iter); }
|
|
462 |
| } |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
48
| public static <T> PredicateSet<T> asPredicateSet(Iterable<T> iter) {
|
|
470 |
0
| if (iter instanceof PredicateSet<?>) { return (PredicateSet<T>) iter; }
|
|
471 |
48
| else if (iter instanceof Set<?>) { return new DelegatingSet<T>((Set<T>) iter); }
|
|
472 |
0
| else { return new IterableSet<T>(iter); }
|
|
473 |
| } |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
103
| public static <T> Collection<T> asCollection(Iterable<T> iter) {
|
|
480 |
29
| if (iter instanceof Collection<?>) { return (Collection<T>) iter; }
|
|
481 |
74
| else { return new IterableCollection<T>(iter); }
|
|
482 |
| } |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
0
| public static <K, V> LambdaMap<K, V> asLambdaMap(Map<K, V> m) {
|
|
489 |
0
| if (m instanceof LambdaMap<?, ?>) { return (LambdaMap<K, V>) m; }
|
|
490 |
0
| else { return new DelegatingMap<K, V>(m); }
|
|
491 |
| } |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
0
| public static <K, V> Map<K, V> asMap(Dictionary<K, V> d) {
|
|
498 |
| |
|
499 |
| |
|
500 |
0
| if (d instanceof Hashtable<?, ?>) { return (Hashtable<K, V>) d; }
|
|
501 |
0
| return new DictionaryMap<K, V>(d);
|
|
502 |
| } |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
0
| public static <T> PredicateSet<T> immutable(Set<? extends T> set) {
|
|
509 |
0
| return new ImmutableSet<T>(set);
|
|
510 |
| } |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
| |
|
516 |
0
| public static <K, V> Map<K, V> immutable(Map<? extends K, ? extends V> map) {
|
|
517 |
0
| return new ImmutableMap<K, V>(map);
|
|
518 |
| } |
|
519 |
| |
|
520 |
| |
|
521 |
0
| public static <T1, T2> ImmutableRelation<T1, T2> immutable(Relation<T1, T2> r) {
|
|
522 |
0
| return new ImmutableRelation<T1, T2>(r);
|
|
523 |
| } |
|
524 |
| |
|
525 |
| |
|
526 |
0
| public static <T> PredicateSet<T> snapshot(Set<? extends T> set) {
|
|
527 |
0
| return makeSet(set);
|
|
528 |
| } |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
0
| public static <T> Iterable<T> conditionalSnapshot(Set<T> set, int threshold) {
|
|
535 |
0
| if (ObjectUtil.compositeSize(set) > threshold) { return makeSet(set); }
|
|
536 |
0
| else { return set; }
|
|
537 |
| } |
|
538 |
| |
|
539 |
| |
|
540 |
0
| public static <T1, T2> Relation<T1, T2> snapshot(Relation<? extends T1, ? extends T2> relation) {
|
|
541 |
0
| return makeRelation(relation);
|
|
542 |
| } |
|
543 |
| |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
0
| public static <T1, T2> Relation<T1, T2> conditionalSnapshot(Relation<T1, T2> rel, int threshold) {
|
|
549 |
0
| if (ObjectUtil.compositeSize(rel) > threshold) { return makeRelation(rel); }
|
|
550 |
0
| else { return rel; }
|
|
551 |
| } |
|
552 |
| |
|
553 |
| |
|
554 |
24
| public static <K, V> LambdaMap<K, V> snapshot(Map<? extends K, ? extends V> map) {
|
|
555 |
24
| return new DelegatingMap<K, V>(new HashMap<K, V>(map));
|
|
556 |
| } |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
0
| public static <K, V> Map<K, V> conditionalSnapshot(Map<K, V> map, int threshold) {
|
|
563 |
0
| if (ObjectUtil.compositeSize(map) > threshold) { return snapshot(map); }
|
|
564 |
0
| else { return map; }
|
|
565 |
| } |
|
566 |
| |
|
567 |
| |
|
568 |
0
| public static <T> List<T> snapshot(List<? extends T> list) {
|
|
569 |
0
| return makeArrayList(list);
|
|
570 |
| } |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
0
| public static <T> SnapshotSynchronizedSet<T> snapshotSynchronized(Set<T> s) {
|
|
577 |
0
| return new SnapshotSynchronizedSet<T>(s);
|
|
578 |
| } |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
| |
|
583 |
| |
|
584 |
0
| public static <T> SnapshotSynchronizedList<T> snapshotSynchronized(List<T> l) {
|
|
585 |
0
| return new SnapshotSynchronizedList<T>(l);
|
|
586 |
| } |
|
587 |
| |
|
588 |
| |
|
589 |
0
| public static <T> PredicateSet<T> union(Set<? extends T> s1, Set<? extends T> s2) {
|
|
590 |
0
| return new UnionSet<T>(s1, s2);
|
|
591 |
| } |
|
592 |
| |
|
593 |
| |
|
594 |
0
| public static <T> PredicateSet<T> union(Set<? extends T> set, T elt) {
|
|
595 |
0
| return new UnionSet<T>(set, new SingletonSet<T>(elt));
|
|
596 |
| } |
|
597 |
| |
|
598 |
| |
|
599 |
0
| public static <T> PredicateSet<T> intersection(Set<?> s1, Set<? extends T> s2) {
|
|
600 |
0
| return new IntersectionSet<T>(s1, s2);
|
|
601 |
| } |
|
602 |
| |
|
603 |
| |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
0
| public static <T> PredicateSet<T> complement(Set<? extends T> domain, Set<?> excluded) {
|
|
608 |
0
| return new ComplementSet<T>(domain, excluded);
|
|
609 |
| } |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
| |
|
614 |
| |
|
615 |
0
| public static <T> PredicateSet<T> complement(Set<? extends T> domain, T excluded) {
|
|
616 |
0
| return new ComplementSet<T>(domain, new SingletonSet<T>(excluded));
|
|
617 |
| } |
|
618 |
| |
|
619 |
| |
|
620 |
0
| public static <T> PredicateSet<T> filter(Set<? extends T> set, Predicate<? super T> predicate) {
|
|
621 |
0
| return new FilteredSet<T>(set, predicate);
|
|
622 |
| } |
|
623 |
| |
|
624 |
| |
|
625 |
0
| public static <T1, T2> Relation<T1, T2> cross(Set<? extends T1> left, Set<? extends T2> right) {
|
|
626 |
0
| return new CartesianRelation<T1, T2>(left, right);
|
|
627 |
| } |
|
628 |
| |
|
629 |
| |
|
630 |
0
| public static <T1, T2> Relation<T1, T2> union(Relation<T1, T2> r1, Relation<T1, T2> r2) {
|
|
631 |
0
| return new UnionRelation<T1, T2>(r1, r2);
|
|
632 |
| } |
|
633 |
| |
|
634 |
| |
|
635 |
0
| public static <T1, T2> Relation<T1, T2> union(Relation<T1, T2> rel, T1 first, T2 second) {
|
|
636 |
0
| return new UnionRelation<T1, T2>(rel, new SingletonRelation<T1, T2>(first, second));
|
|
637 |
| } |
|
638 |
| |
|
639 |
| |
|
640 |
0
| public static <T1, T2> Relation<T1, T2> intersection(Relation<T1, T2> r1, Relation<T1, T2> r2) {
|
|
641 |
0
| return new IntersectionRelation<T1, T2>(r1, r2);
|
|
642 |
| } |
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
| |
|
647 |
| |
|
648 |
0
| public static <T1, T2> Relation<T1, T2> complement(Relation<T1, T2> domain,
|
|
649 |
| Relation<? super T1, ? super T2> excluded) { |
|
650 |
0
| return new ComplementRelation<T1, T2>(domain, excluded);
|
|
651 |
| } |
|
652 |
| |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
0
| public static <T1, T2> Relation<T1, T2> complement(Relation<T1, T2> domain, T1 first, T2 second) {
|
|
658 |
0
| return new ComplementRelation<T1, T2>(domain, new SingletonRelation<T1, T2>(first, second));
|
|
659 |
| } |
|
660 |
| |
|
661 |
| |
|
662 |
0
| public static <T1, T2, T3> Relation<T1, T3> compose(Relation<T1, T2> left, Relation<T2, T3> right) {
|
|
663 |
0
| return new ComposedRelation<T1, T2, T3>(left, right);
|
|
664 |
| } |
|
665 |
| |
|
666 |
| |
|
667 |
0
| public static <T1, T2> Relation<T1, T2> filter(Relation<T1, T2> relation,
|
|
668 |
| Predicate2<? super T1, ? super T2> pred) { |
|
669 |
0
| return new FilteredRelation<T1, T2>(relation, pred);
|
|
670 |
| } |
|
671 |
| |
|
672 |
| |
|
673 |
| |
|
674 |
| |
|
675 |
| |
|
676 |
0
| public static <K, V> LambdaMap<K, V> union(Map<? extends K, ? extends V> parent,
|
|
677 |
| Map<? extends K, ? extends V> child) { |
|
678 |
0
| return new UnionMap<K, V>(parent, child);
|
|
679 |
| } |
|
680 |
| |
|
681 |
| |
|
682 |
0
| public static <K, X, V> LambdaMap<K, V> compose(Map<? extends K, ? extends X> left,
|
|
683 |
| Map<? super X, ? extends V> right) { |
|
684 |
0
| return new ComposedMap<K, X, V>(left, right);
|
|
685 |
| } |
|
686 |
| |
|
687 |
| |
|
688 |
| |
|
689 |
| |
|
690 |
| |
|
691 |
| |
|
692 |
| |
|
693 |
578
| @SuppressWarnings("unchecked")
|
|
694 |
| public static <T> Option<T> castIfContains(Collection<? extends T> c, Object obj) { |
|
695 |
578
| if (c.contains(obj)) { return Option.some((T) obj); }
|
|
696 |
0
| else { return Option.none(); }
|
|
697 |
| } |
|
698 |
| |
|
699 |
| |
|
700 |
| |
|
701 |
| |
|
702 |
| |
|
703 |
| |
|
704 |
0
| public static boolean containsAny(Collection<?> c, Iterable<?> candidates) {
|
|
705 |
0
| for (Object o : candidates) {
|
|
706 |
0
| if (c.contains(o)) { return true; }
|
|
707 |
| } |
|
708 |
0
| return false;
|
|
709 |
| } |
|
710 |
| |
|
711 |
| |
|
712 |
| |
|
713 |
| |
|
714 |
| |
|
715 |
| |
|
716 |
| |
|
717 |
5
| public static boolean containsAll(Collection<?> c, Iterable<?> subset) {
|
|
718 |
5
| if (subset instanceof Collection<?>) {
|
|
719 |
5
| return c.containsAll((Collection<?>) subset);
|
|
720 |
| } |
|
721 |
| else { |
|
722 |
0
| for (Object o : subset) {
|
|
723 |
0
| if (!c.contains(o)) { return false; }
|
|
724 |
| } |
|
725 |
0
| return true;
|
|
726 |
| } |
|
727 |
| } |
|
728 |
| |
|
729 |
| |
|
730 |
| |
|
731 |
| |
|
732 |
| |
|
733 |
| |
|
734 |
22
| public static <E> boolean addAll(Collection<E> c, Iterable<? extends E> elts) {
|
|
735 |
22
| if (elts instanceof Collection<?>) {
|
|
736 |
0
| @SuppressWarnings("unchecked")
|
|
737 |
| Collection<? extends E> eltsColl = (Collection<? extends E>) elts; |
|
738 |
0
| return c.addAll(eltsColl);
|
|
739 |
| } |
|
740 |
| else { |
|
741 |
22
| boolean result = false;
|
|
742 |
5
| for (E elt : elts) { result |= c.add(elt); }
|
|
743 |
22
| return result;
|
|
744 |
| } |
|
745 |
| } |
|
746 |
| |
|
747 |
| |
|
748 |
| |
|
749 |
| |
|
750 |
| |
|
751 |
| |
|
752 |
0
| public static boolean removeAll(Collection<?> c, Iterable<?> elts) {
|
|
753 |
0
| if (elts instanceof Collection<?>) {
|
|
754 |
0
| return c.removeAll((Collection<?>) elts);
|
|
755 |
| } |
|
756 |
| else { |
|
757 |
0
| boolean result = false;
|
|
758 |
0
| for (Object elt : elts) { result |= c.remove(elt); }
|
|
759 |
0
| return result;
|
|
760 |
| } |
|
761 |
| } |
|
762 |
| |
|
763 |
| |
|
764 |
| |
|
765 |
| |
|
766 |
| |
|
767 |
| |
|
768 |
0
| public static boolean retainAll(Collection<?> c, Iterable<?> elts) {
|
|
769 |
0
| if (elts instanceof Collection<?>) {
|
|
770 |
0
| return c.retainAll((Collection<?>) elts);
|
|
771 |
| } |
|
772 |
0
| else { return c.retainAll(makeSet(elts)); }
|
|
773 |
| } |
|
774 |
| |
|
775 |
| |
|
776 |
| |
|
777 |
| |
|
778 |
| |
|
779 |
0
| public static <T> Set<T> functionClosure(T base, Lambda<? super T, ? extends T> function) {
|
|
780 |
0
| return functionClosure(Collections.singleton(base), function);
|
|
781 |
| } |
|
782 |
| |
|
783 |
| |
|
784 |
| |
|
785 |
| |
|
786 |
| |
|
787 |
0
| public static <T> Set<T> functionClosure(Set<? extends T> base, final Lambda<? super T, ? extends T> function) {
|
|
788 |
0
| Lambda<T, Set<T>> neighbors = new Lambda<T, Set<T>>() {
|
|
789 |
0
| public Set<T> value(T node) { return Collections.<T>singleton(function.value(node)); }
|
|
790 |
| }; |
|
791 |
0
| return graphClosure(base, neighbors);
|
|
792 |
| } |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
| |
|
797 |
| |
|
798 |
0
| public static <T> Set<T> partialFunctionClosure(T base, Lambda<? super T, ? extends Option<? extends T>> function) {
|
|
799 |
0
| return partialFunctionClosure(Collections.singleton(base), function);
|
|
800 |
| } |
|
801 |
| |
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
| |
|
806 |
0
| public static <T> Set<T> partialFunctionClosure(Set<? extends T> base,
|
|
807 |
| final Lambda<? super T, ? extends Option<? extends T>> function) { |
|
808 |
0
| Lambda<T, Set<T>> neighbors = new Lambda<T, Set<T>>() {
|
|
809 |
0
| public Set<T> value(T node) { return makeSet(function.value(node)); }
|
|
810 |
| }; |
|
811 |
0
| return graphClosure(base, neighbors);
|
|
812 |
| } |
|
813 |
| |
|
814 |
| |
|
815 |
0
| public static <T> Set<T> graphClosure(T base, Lambda<? super T, ? extends Iterable<? extends T>> neighbors) {
|
|
816 |
0
| return graphClosure(Collections.singleton(base), neighbors);
|
|
817 |
| } |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
0
| public static <T> Set<T> graphClosure(Set<? extends T> base,
|
|
824 |
| Lambda<? super T, ? extends Iterable<? extends T>> neighbors) { |
|
825 |
0
| Set<T> result = new LinkedHashSet<T>(base);
|
|
826 |
0
| LinkedList<T> workList = new LinkedList<T>(base);
|
|
827 |
0
| while (!workList.isEmpty()) {
|
|
828 |
0
| for (T newElt : neighbors.value(workList.removeFirst())) {
|
|
829 |
0
| if (!result.contains(newElt)) {
|
|
830 |
0
| result.add(newElt);
|
|
831 |
0
| workList.addLast(newElt);
|
|
832 |
| } |
|
833 |
| } |
|
834 |
| } |
|
835 |
0
| return result;
|
|
836 |
| } |
|
837 |
| |
|
838 |
| |
|
839 |
| |
|
840 |
| |
|
841 |
| |
|
842 |
| |
|
843 |
| |
|
844 |
14
| public static <T> List<T> maxList(Iterable<? extends T> vals, Order<? super T> order) {
|
|
845 |
14
| switch (IterUtil.sizeOf(vals, 2)) {
|
|
846 |
2
| case 0: return Collections.emptyList();
|
|
847 |
2
| case 1: return Collections.singletonList(IterUtil.first(vals));
|
|
848 |
10
| default:
|
|
849 |
10
| LinkedList<? extends T> workList = makeLinkedList(vals);
|
|
850 |
10
| LinkedList<T> result = new LinkedList<T>();
|
|
851 |
10
| Iterable<T> remainingTs = IterUtil.compose(workList, result);
|
|
852 |
10
| while (!workList.isEmpty()) {
|
|
853 |
| |
|
854 |
34
| T t = workList.removeLast();
|
|
855 |
34
| boolean discard = IterUtil.or(remainingTs, LambdaUtil.bindFirst(order, t));
|
|
856 |
16
| if (!discard) { result.addFirst(t); }
|
|
857 |
| } |
|
858 |
10
| return result;
|
|
859 |
| } |
|
860 |
| } |
|
861 |
| |
|
862 |
| |
|
863 |
| |
|
864 |
| |
|
865 |
| |
|
866 |
| |
|
867 |
18
| public static <T> List<T> composeMaxLists(Iterable<? extends T> vals1, Iterable<? extends T> vals2,
|
|
868 |
| Order<? super T> order) { |
|
869 |
18
| List<T> results2 = new LinkedList<T>();
|
|
870 |
18
| for (T t : vals2) {
|
|
871 |
| |
|
872 |
26
| boolean discard = IterUtil.or(vals1, LambdaUtil.bindFirst(order, t));
|
|
873 |
16
| if (!discard) { results2.add(t); }
|
|
874 |
| } |
|
875 |
18
| List<T> results1 = new LinkedList<T>();
|
|
876 |
18
| for (T t : vals1) {
|
|
877 |
| |
|
878 |
26
| boolean discard = IterUtil.or(results2, LambdaUtil.bindFirst(order, t));
|
|
879 |
24
| if (!discard) { results1.add(t); }
|
|
880 |
| } |
|
881 |
18
| results1.addAll(results2);
|
|
882 |
18
| return results1;
|
|
883 |
| } |
|
884 |
| |
|
885 |
| |
|
886 |
| |
|
887 |
| |
|
888 |
| |
|
889 |
| |
|
890 |
| |
|
891 |
7
| public static <T> List<T> minList(Iterable<? extends T> vals, Order<? super T> order) {
|
|
892 |
7
| return maxList(vals, inverse(order));
|
|
893 |
| } |
|
894 |
| |
|
895 |
| |
|
896 |
| |
|
897 |
| |
|
898 |
| |
|
899 |
| |
|
900 |
9
| public static <T> List<T> composeMinLists(Iterable<? extends T> vals1, Iterable<? extends T> vals2,
|
|
901 |
| Order<? super T> order) { |
|
902 |
9
| return composeMaxLists(vals1, vals2, inverse(order));
|
|
903 |
| } |
|
904 |
| |
|
905 |
| |
|
906 |
2
| @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> TotalOrder<T> naturalOrder() {
|
|
907 |
2
| return (TotalOrder<T>) NaturalOrder.INSTANCE;
|
|
908 |
| } |
|
909 |
| |
|
910 |
| private static final class NaturalOrder<T extends Comparable<? super T>> |
|
911 |
| extends TotalOrder<T> implements Serializable { |
|
912 |
| private static final NaturalOrder<Comparable<Object>> INSTANCE = new NaturalOrder<Comparable<Object>>(); |
|
913 |
1
| private NaturalOrder() {}
|
|
914 |
56
| public int compare(T arg1, T arg2) { return arg1.compareTo(arg2); }
|
|
915 |
| } |
|
916 |
| |
|
917 |
| |
|
918 |
0
| public static <T> TotalOrder<T> asTotalOrder(Comparator<? super T> comp) {
|
|
919 |
0
| return new ComparatorTotalOrder<T>(comp);
|
|
920 |
| } |
|
921 |
| |
|
922 |
| private static final class ComparatorTotalOrder<T> extends TotalOrder<T> { |
|
923 |
| private final Comparator<? super T> _comp; |
|
924 |
0
| public ComparatorTotalOrder(Comparator<? super T> comp) { _comp = comp; }
|
|
925 |
0
| public int compare(T arg1, T arg2) { return _comp.compare(arg1, arg2); }
|
|
926 |
0
| public boolean equals(Object o) {
|
|
927 |
0
| if (this == o) { return true; }
|
|
928 |
0
| else if (!(o instanceof ComparatorTotalOrder<?>)) { return false; }
|
|
929 |
0
| else { return _comp.equals(((ComparatorTotalOrder<?>) o)._comp); }
|
|
930 |
| } |
|
931 |
0
| public int hashCode() { return ObjectUtil.hash(ComparatorTotalOrder.class, _comp); }
|
|
932 |
| } |
|
933 |
| |
|
934 |
| |
|
935 |
16
| public static <T> Order<T> inverse(Order<? super T> ord) {
|
|
936 |
16
| return new InverseOrder<T>(ord);
|
|
937 |
| } |
|
938 |
| |
|
939 |
| private static final class InverseOrder<T> implements Order<T> { |
|
940 |
| private final Order<? super T> _ord; |
|
941 |
16
| public InverseOrder(Order<? super T> ord) { _ord = ord; }
|
|
942 |
46
| public boolean contains(T arg1, T arg2) { return _ord.contains(arg2, arg1); }
|
|
943 |
0
| public boolean equals(Object o) {
|
|
944 |
0
| if (this == o) { return true; }
|
|
945 |
0
| else if (!(o instanceof InverseOrder<?>)) { return false; }
|
|
946 |
0
| else { return _ord.equals(((InverseOrder<?>) o)._ord); }
|
|
947 |
| } |
|
948 |
0
| public int hashCode() { return ObjectUtil.hash(InverseOrder.class, _ord); }
|
|
949 |
| } |
|
950 |
| |
|
951 |
| |
|
952 |
0
| public static <T> TotalOrder<T> inverse(Comparator<? super T> ord) {
|
|
953 |
0
| return new InverseTotalOrder<T>(ord);
|
|
954 |
| } |
|
955 |
| |
|
956 |
| private static final class InverseTotalOrder<T> extends TotalOrder<T> { |
|
957 |
| private final Comparator<? super T> _ord; |
|
958 |
0
| public InverseTotalOrder(Comparator<? super T> ord) { _ord = ord; }
|
|
959 |
0
| public int compare(T arg1, T arg2) { return _ord.compare(arg2, arg1); }
|
|
960 |
0
| public boolean equals(Object o) {
|
|
961 |
0
| if (this == o) { return true; }
|
|
962 |
0
| else if (!(o instanceof InverseTotalOrder<?>)) { return false; }
|
|
963 |
0
| else { return _ord.equals(((InverseTotalOrder<?>) o)._ord); }
|
|
964 |
| } |
|
965 |
0
| public int hashCode() { return ObjectUtil.hash(InverseTotalOrder.class, _ord); }
|
|
966 |
| } |
|
967 |
| |
|
968 |
| |
|
969 |
| |
|
970 |
| |
|
971 |
| |
|
972 |
| public static final Order<Iterable<?>> SUBSET_ORDER = new SubsetOrder(); |
|
973 |
| |
|
974 |
| private static final class SubsetOrder implements Order<Iterable<?>>, Serializable { |
|
975 |
16
| private SubsetOrder() {}
|
|
976 |
5
| public boolean contains(Iterable<?> sub, Iterable<?> sup) { return IterUtil.containsAll(sup, sub); }
|
|
977 |
| } |
|
978 |
| |
|
979 |
| |
|
980 |
| |
|
981 |
| |
|
982 |
| |
|
983 |
| public static final Order<String> SUBSTRING_ORDER = new SubstringOrder(); |
|
984 |
| |
|
985 |
| private static final class SubstringOrder implements Order<String>, Serializable { |
|
986 |
16
| private SubstringOrder() {}
|
|
987 |
9
| public boolean contains(String sub, String sup) { return sup.contains(sub); }
|
|
988 |
| } |
|
989 |
| |
|
990 |
| |
|
991 |
| |
|
992 |
| |
|
993 |
| |
|
994 |
| public static final Order<String> STRING_PREFIX_ORDER = new StringPrefixOrder(); |
|
995 |
| |
|
996 |
| private static final class StringPrefixOrder implements Order<String>, Serializable { |
|
997 |
16
| private StringPrefixOrder() {}
|
|
998 |
101
| public boolean contains(String pre, String s) { return s.startsWith(pre); }
|
|
999 |
| } |
|
1000 |
| |
|
1001 |
| } |