forked from apache/commons-collections
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IterableUtils.java
1114 lines (1037 loc) · 45.8 KB
/
IterableUtils.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.iterators.LazyIteratorChain;
import org.apache.commons.collections4.iterators.PairedIterator.PairedItem;
import org.apache.commons.collections4.iterators.ReverseListIterator;
import org.apache.commons.collections4.iterators.UniqueFilterIterator;
/**
* Provides utility methods and decorators for {@link Iterable} instances.
* <p>
* <b>Note</b>: this util class has been designed for fail-fast argument checking.
* </p>
* <ul>
* <li>
* all decorator methods are <b>NOT</b> null-safe wrt the provided Iterable argument, i.e.
* they will throw a {@link NullPointerException} if a null Iterable is passed as argument.
* <li>
* all other utility methods are null-safe wrt the provided Iterable argument, i.e. they will
* treat a null Iterable the same way as an empty one. Other arguments which are null,
* e.g. a {@link Predicate}, will result in a {@link NullPointerException}. Exception: passing
* a null {@link Comparator} is equivalent to a Comparator with natural ordering.
* </ul>
*
* @since 4.1
*/
public class IterableUtils {
/**
* An empty iterable.
*/
@SuppressWarnings("rawtypes")
static final FluentIterable EMPTY_ITERABLE = new FluentIterable<Object>() {
@Override
public Iterator<Object> iterator() {
return IteratorUtils.emptyIterator();
}
};
// Empty
// ----------------------------------------------------------------------
/**
* Gets an empty iterable.
* <p>
* This iterable does not contain any elements.
*
* @param <E> the element type
* @return an empty iterable
*/
@SuppressWarnings("unchecked") // OK, empty collection is compatible with any type
public static <E> Iterable<E> emptyIterable() {
return EMPTY_ITERABLE;
}
// Chained
// ----------------------------------------------------------------------
/**
* Combines two iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a},
* followed by the elements in {@code b}. The source iterators are not polled until
* necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param a the first iterable, may not be null
* @param b the second iterable, may not be null
* @return a new iterable, combining the provided iterables
* @throws NullPointerException if either a or b is null
*/
@SuppressWarnings("unchecked")
public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b) {
return chainedIterable(new Iterable[] {a, b});
}
/**
* Combines three iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a},
* followed by the elements in {@code b} and {@code c}. The source iterators are
* not polled until necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param a the first iterable, may not be null
* @param b the second iterable, may not be null
* @param c the third iterable, may not be null
* @return a new iterable, combining the provided iterables
* @throws NullPointerException if either of the provided iterables is null
*/
@SuppressWarnings("unchecked")
public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b,
final Iterable<? extends E> c) {
return chainedIterable(new Iterable[] {a, b, c});
}
/**
* Combines four iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a},
* followed by the elements in {@code b}, {@code c} and {@code d}. The source
* iterators are not polled until necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param a the first iterable, may not be null
* @param b the second iterable, may not be null
* @param c the third iterable, may not be null
* @param d the fourth iterable, may not be null
* @return a new iterable, combining the provided iterables
* @throws NullPointerException if either of the provided iterables is null
*/
@SuppressWarnings("unchecked")
public static <E> Iterable<E> chainedIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b,
final Iterable<? extends E> c,
final Iterable<? extends E> d) {
return chainedIterable(new Iterable[] {a, b, c, d});
}
/**
* Combines the provided iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in the order
* of the arguments, i.e. iterables[0], iterables[1], .... The source iterators
* are not polled until necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param iterables the iterables to combine, may not be null
* @return a new iterable, combining the provided iterables
* @throws NullPointerException if either of the provided iterables is null
*/
public static <E> Iterable<E> chainedIterable(final Iterable<? extends E>... iterables) {
checkNotNull(iterables);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return new LazyIteratorChain<E>() {
@Override
protected Iterator<? extends E> nextIterator(final int count) {
if (count > iterables.length) {
return null;
}
return iterables[count - 1].iterator();
}
};
}
};
}
// Collated
// ----------------------------------------------------------------------
/**
* Combines the two provided iterables into an ordered iterable using
* natural ordering.
* <p>
* The returned iterable's iterator supports {@code remove()} when the
* corresponding input iterator supports it.
*
* @param <E> the element type
* @param a the first iterable, must not be null
* @param b the second iterable, must not be null
* @return a filtered view on the specified iterable
* @throws NullPointerException if either of the provided iterables is null
*/
public static <E> Iterable<E> collatedIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b) {
checkNotNull(a, b);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.collatedIterator(null, a.iterator(), b.iterator());
}
};
}
/**
* Combines the two provided iterables into an ordered iterable using the
* provided comparator. If the comparator is null, natural ordering will be
* used.
* <p>
* The returned iterable's iterator supports {@code remove()} when the
* corresponding input iterator supports it.
*
* @param <E> the element type
* @param comparator the comparator defining an ordering over the elements,
* may be null, in which case natural ordering will be used
* @param a the first iterable, may not be null
* @param b the second iterable, may not be null
* @return a filtered view on the specified iterable
* @throws NullPointerException if either of the provided iterables is null
*/
public static <E> Iterable<E> collatedIterable(final Comparator<? super E> comparator,
final Iterable<? extends E> a,
final Iterable<? extends E> b) {
checkNotNull(a, b);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.collatedIterator(comparator, a.iterator(), b.iterator());
}
};
}
// Filtered
// ----------------------------------------------------------------------
/**
* Returns a view of the given iterable that only contains elements matching
* the provided predicate.
* <p>
* The returned iterable's iterator supports {@code remove()} when the
* corresponding input iterator supports it.
*
* @param <E> the element type
* @param iterable the iterable to filter, may not be null
* @param predicate the predicate used to filter elements, may not be null
* @return a filtered view on the specified iterable
* @throws NullPointerException if either iterable or predicate is null
*/
public static <E> Iterable<E> filteredIterable(final Iterable<E> iterable,
final Predicate<? super E> predicate) {
checkNotNull(iterable);
Objects.requireNonNull(predicate, "predicate");
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.filteredIterator(emptyIteratorIfNull(iterable), predicate);
}
};
}
// Bounded
// ----------------------------------------------------------------------
/**
* Returns a view of the given iterable that contains at most the given number
* of elements.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param iterable the iterable to limit, may not be null
* @param maxSize the maximum number of elements, must not be negative
* @return a bounded view on the specified iterable
* @throws IllegalArgumentException if maxSize is negative
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> boundedIterable(final Iterable<E> iterable, final long maxSize) {
checkNotNull(iterable);
if (maxSize < 0) {
throw new IllegalArgumentException("MaxSize parameter must not be negative.");
}
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.boundedIterator(iterable.iterator(), maxSize);
}
};
}
// Looping
// ----------------------------------------------------------------------
/**
* Returns a view of the given iterable which will cycle infinitely over
* its elements.
* <p>
* The returned iterable's iterator supports {@code remove()} if
* {@code iterable.iterator()} does. After {@code remove()} is called, subsequent
* cycles omit the removed element, which is no longer in {@code iterable}. The
* iterator's {@code hasNext()} method returns {@code true} until {@code iterable}
* is empty.
*
* @param <E> the element type
* @param iterable the iterable to loop, may not be null
* @return a view of the iterable, providing an infinite loop over its elements
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> loopingIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return new LazyIteratorChain<E>() {
@Override
protected Iterator<? extends E> nextIterator(final int count) {
if (IterableUtils.isEmpty(iterable)) {
return null;
}
return iterable.iterator();
}
};
}
};
}
// Reversed
// ----------------------------------------------------------------------
/**
* Returns a reversed view of the given iterable.
* <p>
* In case the provided iterable is a {@link List} instance, a
* {@link ReverseListIterator} will be used to reverse the traversal
* order, otherwise an intermediate {@link List} needs to be created.
* <p>
* The returned iterable's iterator supports {@code remove()} if the
* provided iterable is a {@link List} instance.
*
* @param <E> the element type
* @param iterable the iterable to use, may not be null
* @return a reversed view of the specified iterable
* @throws NullPointerException if iterable is null
* @see ReverseListIterator
*/
public static <E> Iterable<E> reversedIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
final List<E> list = (iterable instanceof List<?>) ?
(List<E>) iterable :
IteratorUtils.toList(iterable.iterator());
return new ReverseListIterator<>(list);
}
};
}
// Skipping
// ----------------------------------------------------------------------
/**
* Returns a view of the given iterable that skips the first N elements.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param iterable the iterable to use, may not be null
* @param elementsToSkip the number of elements to skip from the start, must not be negative
* @return a view of the specified iterable, skipping the first N elements
* @throws IllegalArgumentException if elementsToSkip is negative
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> skippingIterable(final Iterable<E> iterable, final long elementsToSkip) {
checkNotNull(iterable);
if (elementsToSkip < 0) {
throw new IllegalArgumentException("ElementsToSkip parameter must not be negative.");
}
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.skippingIterator(iterable.iterator(), elementsToSkip);
}
};
}
// Transformed
// ----------------------------------------------------------------------
/**
* Returns a transformed view of the given iterable where all of its elements
* have been transformed by the provided transformer.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <I> the input element type
* @param <O> the output element type
* @param iterable the iterable to transform, may not be null
* @param transformer the transformer, must not be null
* @return a transformed view of the specified iterable
* @throws NullPointerException if either iterable or transformer is null
*/
public static <I, O> Iterable<O> transformedIterable(final Iterable<I> iterable,
final Transformer<? super I, ? extends O> transformer) {
checkNotNull(iterable);
Objects.requireNonNull(transformer, "transformer");
return new FluentIterable<O>() {
@Override
public Iterator<O> iterator() {
return IteratorUtils.transformedIterator(iterable.iterator(), transformer);
}
};
}
// Unique
// ----------------------------------------------------------------------
/**
* Returns a unique view of the given iterable.
* <p>
* The returned iterable's iterator supports {@code remove()} when the
* corresponding input iterator supports it. Calling {@code remove()}
* will only remove a single element from the underlying iterator.
*
* @param <E> the element type
* @param iterable the iterable to use, may not be null
* @return a unique view of the specified iterable
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> uniqueIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return new UniqueFilterIterator<>(iterable.iterator());
}
};
}
// Unmodifiable
// ----------------------------------------------------------------------
/**
* Returns an unmodifiable view of the given iterable.
* <p>
* The returned iterable's iterator does not support {@code remove()}.
*
* @param <E> the element type
* @param iterable the iterable to use, may not be null
* @return an unmodifiable view of the specified iterable
* @throws NullPointerException if iterable is null
*/
public static <E> Iterable<E> unmodifiableIterable(final Iterable<E> iterable) {
checkNotNull(iterable);
if (iterable instanceof UnmodifiableIterable<?>) {
return iterable;
}
return new UnmodifiableIterable<>(iterable);
}
/**
* Inner class to distinguish unmodifiable instances.
*/
private static final class UnmodifiableIterable<E> extends FluentIterable<E> {
private final Iterable<E> unmodifiable;
UnmodifiableIterable(final Iterable<E> iterable) {
this.unmodifiable = iterable;
}
@Override
public Iterator<E> iterator() {
return IteratorUtils.unmodifiableIterator(unmodifiable.iterator());
}
}
// Zipping
// ----------------------------------------------------------------------
/**
* Interleaves two iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a}
* and {@code b} in alternating order. The source iterators are not polled until
* necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param a the first iterable, may not be null
* @param b the second iterable, may not be null
* @return a new iterable, interleaving the provided iterables
* @throws NullPointerException if either a or b is null
*/
public static <E> Iterable<E> zippingIterable(final Iterable<? extends E> a,
final Iterable<? extends E> b) {
checkNotNull(a);
checkNotNull(b);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
return IteratorUtils.zippingIterator(a.iterator(), b.iterator());
}
};
}
/**
* Interleaves two iterables into a single iterable.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a}
* and {@code b} in alternating order. The source iterators are not polled until
* necessary.
* <p>
* The returned iterable's iterator supports {@code remove()} when the corresponding
* input iterator supports it.
*
* @param <E> the element type
* @param first the first iterable, may not be null
* @param others the array of iterables to interleave, may not be null
* @return a new iterable, interleaving the provided iterables
* @throws NullPointerException if either of the provided iterables is null
*/
public static <E> Iterable<E> zippingIterable(final Iterable<? extends E> first,
final Iterable<? extends E>... others) {
checkNotNull(first);
checkNotNull(others);
return new FluentIterable<E>() {
@Override
public Iterator<E> iterator() {
@SuppressWarnings("unchecked") // safe
final
Iterator<? extends E>[] iterators = new Iterator[others.length + 1];
iterators[0] = first.iterator();
for (int i = 0; i < others.length; i++) {
iterators[i + 1] = others[i].iterator();
}
return IteratorUtils.zippingIterator(iterators);
}
};
}
/**
* Provides iteration over the elements contained in a pair of Iterables in-tandem.
* <p>
* The returned iterable has an iterator that traverses the elements in {@code a}
* and {@code b} together until one of the iterables is traversed completely.
* <p>
* The returned iterable's iterator does NOT support {@code remove()}.
*
* @param <L> the left elements' type
* @param <R> the right elements' type
* @param left the iterable for the left side elements
* @param right the iterable for the right side elements
* @return an iterable, over the decorated iterables to traverse them together until one is
* exhausted
* @throws NullPointerException if any iterator is null
*/
public static <L, R> Iterable<PairedItem<L, R>> pairedIterable(final Iterable<L> left, final Iterable<R> right) {
checkNotNull(left);
checkNotNull(right);
return new FluentIterable<PairedItem<L, R>>(){
@Override
public Iterator<PairedItem<L, R>> iterator() {
return IteratorUtils.pairedIterator(left.iterator(), right.iterator());
}
};
}
// Utility methods
// ----------------------------------------------------------------------
/**
* Returns an immutable empty iterable if the argument is null,
* or the argument itself otherwise.
*
* @param <E> the element type
* @param iterable the iterable, may be null
* @return an empty iterable if the argument is null
*/
public static <E> Iterable<E> emptyIfNull(final Iterable<E> iterable) {
return iterable == null ? IterableUtils.<E>emptyIterable() : iterable;
}
/**
* Applies the closure to each element of the provided iterable.
*
* @param <E> the element type
* @param iterable the iterator to use, may be null
* @param closure the closure to apply to each element, may not be null
* @throws NullPointerException if closure is null
*/
public static <E> void forEach(final Iterable<E> iterable, final Closure<? super E> closure) {
IteratorUtils.forEach(emptyIteratorIfNull(iterable), closure);
}
/**
* Executes the given closure on each but the last element in the iterable.
* <p>
* If the input iterable is null no change is made.
*
* @param <E> the type of object the {@link Iterable} contains
* @param iterable the iterable to get the input from, may be null
* @param closure the closure to perform, may not be null
* @return the last element in the iterable, or null if iterable is null or empty
*/
public static <E> E forEachButLast(final Iterable<E> iterable, final Closure<? super E> closure) {
return IteratorUtils.forEachButLast(emptyIteratorIfNull(iterable), closure);
}
/**
* Finds the first element in the given iterable which matches the given predicate.
* <p>
* A {@code null} or empty iterator returns null.
*
* @param <E> the element type
* @param iterable the iterable to search, may be null
* @param predicate the predicate to use, must not be null
* @return the first element of the iterable which matches the predicate or null if none could be found
* @throws NullPointerException if predicate is null
*/
public static <E> E find(final Iterable<E> iterable, final Predicate<? super E> predicate) {
return IteratorUtils.find(emptyIteratorIfNull(iterable), predicate);
}
/**
* Returns the index of the first element in the specified iterable that
* matches the given predicate.
* <p>
* A {@code null} or empty iterable returns -1.
*
* @param <E> the element type
* @param iterable the iterable to search, may be null
* @param predicate the predicate to use, must not be null
* @return the index of the first element which matches the predicate or -1 if none matches
* @throws NullPointerException if predicate is null
*/
public static <E> int indexOf(final Iterable<E> iterable, final Predicate<? super E> predicate) {
return IteratorUtils.indexOf(emptyIteratorIfNull(iterable), predicate);
}
/**
* Answers true if a predicate is true for every element of an iterable.
* <p>
* A {@code null} or empty iterable returns true.
*
* @param <E> the type of object the {@link Iterable} contains
* @param iterable the {@link Iterable} to use, may be null
* @param predicate the predicate to use, may not be null
* @return true if every element of the collection matches the predicate or if the
* collection is empty, false otherwise
* @throws NullPointerException if predicate is null
*/
public static <E> boolean matchesAll(final Iterable<E> iterable, final Predicate<? super E> predicate) {
return IteratorUtils.matchesAll(emptyIteratorIfNull(iterable), predicate);
}
/**
* Answers true if a predicate is true for any element of the iterable.
* <p>
* A {@code null} or empty iterable returns false.
*
* @param <E> the type of object the {@link Iterable} contains
* @param iterable the {@link Iterable} to use, may be null
* @param predicate the predicate to use, may not be null
* @return true if any element of the collection matches the predicate, false otherwise
* @throws NullPointerException if predicate is null
*/
public static <E> boolean matchesAny(final Iterable<E> iterable, final Predicate<? super E> predicate) {
return IteratorUtils.matchesAny(emptyIteratorIfNull(iterable), predicate);
}
/**
* Counts the number of elements in the input iterable that match the predicate.
* <p>
* A {@code null} iterable matches no elements.
*
* @param <E> the type of object the {@link Iterable} contains
* @param input the {@link Iterable} to get the input from, may be null
* @param predicate the predicate to use, may not be null
* @return the number of matches for the predicate in the collection
* @throws NullPointerException if predicate is null
*/
public static <E> long countMatches(final Iterable<E> input, final Predicate<? super E> predicate) {
Objects.requireNonNull(predicate, "predicate");
return size(filteredIterable(emptyIfNull(input), predicate));
}
/**
* Answers true if the provided iterable is empty.
* <p>
* A {@code null} iterable returns true.
*
* @param iterable the {@link Iterable to use}, may be null
* @return true if the iterable is null or empty, false otherwise
*/
public static boolean isEmpty(final Iterable<?> iterable) {
if (iterable instanceof Collection<?>) {
return ((Collection<?>) iterable).isEmpty();
}
return IteratorUtils.isEmpty(emptyIteratorIfNull(iterable));
}
/**
* Checks if the object is contained in the given iterable.
* <p>
* A {@code null} or empty iterable returns false.
*
* @param <E> the type of object the {@link Iterable} contains
* @param iterable the iterable to check, may be null
* @param object the object to check
* @return true if the object is contained in the iterable, false otherwise
*/
public static <E> boolean contains(final Iterable<E> iterable, final Object object) {
if (iterable instanceof Collection<?>) {
return ((Collection<E>) iterable).contains(object);
}
return IteratorUtils.contains(emptyIteratorIfNull(iterable), object);
}
/**
* Checks if the object is contained in the given iterable. Object equality
* is tested with an {@code equator} unlike {@link #contains(Iterable, Object)}
* which uses {@link Object#equals(Object)}.
* <p>
* A {@code null} or empty iterable returns false.
* A {@code null} object will not be passed to the equator, instead a
* {@link org.apache.commons.collections4.functors.NullPredicate NullPredicate}
* will be used.
*
* @param <E> the type of object the {@link Iterable} contains
* @param iterable the iterable to check, may be null
* @param object the object to check
* @param equator the equator to use to check, may not be null
* @return true if the object is contained in the iterable, false otherwise
* @throws NullPointerException if equator is null
*/
public static <E> boolean contains(final Iterable<? extends E> iterable, final E object,
final Equator<? super E> equator) {
Objects.requireNonNull(equator, "equator");
return matchesAny(iterable, EqualPredicate.equalPredicate(object, equator));
}
/**
* Returns the number of occurrences of the provided object in the iterable.
*
* @param <E> the element type that the {@link Iterable} may contain
* @param <T> the element type of the object to find
* @param iterable the {@link Iterable} to search
* @param obj the object to find the cardinality of
* @return the number of occurrences of obj in iterable
*/
public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) {
if (iterable instanceof Set<?>) {
return ((Set<E>) iterable).contains(obj) ? 1 : 0;
}
if (iterable instanceof Bag<?>) {
return ((Bag<E>) iterable).getCount(obj);
}
return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj)));
}
/**
* Returns the {@code index}-th value in the {@code iterable}'s {@link Iterator}, throwing
* {@code IndexOutOfBoundsException} if there is no such element.
* <p>
* If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}.
*
* @param <T> the type of object in the {@link Iterable}.
* @param iterable the {@link Iterable} to get a value from, may be null
* @param index the index to get
* @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
*/
public static <T> T get(final Iterable<T> iterable, final int index) {
CollectionUtils.checkIndexBounds(index);
if (iterable instanceof List<?>) {
return ((List<T>) iterable).get(index);
}
return IteratorUtils.get(emptyIteratorIfNull(iterable), index);
}
/**
* Shortcut for {@code get(iterator, 0)}.
* <p>
* Returns the {@code first} value in the {@code iterable}'s {@link Iterator}, throwing
* {@code IndexOutOfBoundsException} if there is no such element.
* </p>
* <p>
* If the {@link Iterable} is a {@link List}, then it will use {@link List#get(int)}.
* </p>
*
* @param <T> the type of object in the {@link Iterable}.
* @param iterable the {@link Iterable} to get a value from, may be null
* @return the first object
* @throws IndexOutOfBoundsException if the request is invalid
* @since 4.2
*/
public static <T> T first(final Iterable<T> iterable) {
return get(iterable, 0);
}
/**
* Returns the number of elements contained in the given iterator.
* <p>
* A {@code null} or empty iterator returns {@code 0}.
*
* @param iterable the iterable to check, may be null
* @return the number of elements contained in the iterable
*/
public static int size(final Iterable<?> iterable) {
if (iterable == null) {
return 0;
}
if (iterable instanceof Collection<?>) {
return ((Collection<?>) iterable).size();
}
return IteratorUtils.size(emptyIteratorIfNull(iterable));
}
/**
* Partitions all elements from iterable into separate output collections,
* based on the evaluation of the given predicate.
* <p>
* For each predicate, the result will contain a list holding all elements of the
* input iterable matching the predicate. The last list will hold all elements
* which didn't match any predicate:
* <pre>
* [C1, R] = partition(I, P1) with
* I = input
* P1 = first predicate
* C1 = collection of elements matching P1
* R = collection of elements rejected by all predicates
* </pre>
* <p>
* If the input iterable is {@code null}, the same is returned as for an
* empty iterable.
* <p>
* Example: for an input list [1, 2, 3, 4, 5] calling partition with a predicate [x < 3]
* will result in the following output: [[1, 2], [3, 4, 5]].
*
* @param <O> the type of object the {@link Iterable} contains
* @param iterable the iterable to partition, may be null
* @param predicate the predicate to use, may not be null
* @return a list containing the output collections
* @throws NullPointerException if predicate is null
*/
public static <O> List<List<O>> partition(final Iterable<? extends O> iterable,
final Predicate<? super O> predicate) {
Objects.requireNonNull(predicate, "predicate");
@SuppressWarnings({ "unchecked", "rawtypes" }) // safe
final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class) ArrayList.class);
@SuppressWarnings("unchecked") // safe
final Predicate<? super O>[] predicates = new Predicate[] { predicate };
return partition(iterable, factory, predicates);
}
/**
* Partitions all elements from iterable into separate output collections,
* based on the evaluation of the given predicates.
* <p>
* For each predicate, the result will contain a list holding all elements of the
* input iterable matching the predicate. The last list will hold all elements
* which didn't match any predicate:
* <pre>
* [C1, C2, R] = partition(I, P1, P2) with
* I = input
* P1 = first predicate
* P2 = second predicate
* C1 = collection of elements matching P1
* C2 = collection of elements matching P2
* R = collection of elements rejected by all predicates
* </pre>
* <p>
* <b>Note</b>: elements are only added to the output collection of the first matching
* predicate, determined by the order of arguments.
* <p>
* If the input iterable is {@code null}, the same is returned as for an
* empty iterable.
* <p>
* Example: for an input list [1, 2, 3, 4, 5] calling partition with predicates [x < 3]
* and [x < 5] will result in the following output: [[1, 2], [3, 4], [5]].
*
* @param <O> the type of object the {@link Iterable} contains
* @param iterable the collection to get the input from, may be null
* @param predicates the predicates to use, may not be null
* @return a list containing the output collections
* @throws NullPointerException if any predicate is null
*/
public static <O> List<List<O>> partition(final Iterable<? extends O> iterable,
final Predicate<? super O>... predicates) {
@SuppressWarnings({ "unchecked", "rawtypes" }) // safe
final Factory<List<O>> factory = FactoryUtils.instantiateFactory((Class) ArrayList.class);
return partition(iterable, factory, predicates);
}
/**
* Partitions all elements from iterable into separate output collections,
* based on the evaluation of the given predicates.
* <p>
* For each predicate, the returned list will contain a collection holding
* all elements of the input iterable matching the predicate. The last collection
* contained in the list will hold all elements which didn't match any predicate:
* <pre>
* [C1, C2, R] = partition(I, P1, P2) with
* I = input
* P1 = first predicate
* P2 = second predicate
* C1 = collection of elements matching P1
* C2 = collection of elements matching P2
* R = collection of elements rejected by all predicates
* </pre>
* <p>
* <b>Note</b>: elements are only added to the output collection of the first matching
* predicate, determined by the order of arguments.
* <p>
* If the input iterable is {@code null}, the same is returned as for an
* empty iterable.
* If no predicates have been provided, all elements of the input collection
* will be added to the rejected collection.
* <p>
* Example: for an input list [1, 2, 3, 4, 5] calling partition with predicates [x < 3]
* and [x < 5] will result in the following output: [[1, 2], [3, 4], [5]].
*
* @param <O> the type of object the {@link Iterable} contains
* @param <R> the type of the output {@link Collection}
* @param iterable the collection to get the input from, may be null
* @param partitionFactory the factory used to create the output collections
* @param predicates the predicates to use, may not be null
* @return a list containing the output collections
* @throws NullPointerException if any predicate is null
*/
public static <O, R extends Collection<O>> List<R> partition(final Iterable<? extends O> iterable,
final Factory<R> partitionFactory, final Predicate<? super O>... predicates) {
if (iterable == null) {
final Iterable<O> empty = emptyIterable();
return partition(empty, partitionFactory, predicates);
}
Objects.requireNonNull(predicates, "predicates");
for (final Predicate<?> predicate : predicates) {
Objects.requireNonNull(predicate, "predicate");
}
if (predicates.length < 1) {
// return the entire input collection as a single partition
final R singlePartition = partitionFactory.create();
CollectionUtils.addAll(singlePartition, iterable);
return Collections.singletonList(singlePartition);
}
// create the empty partitions
final int numberOfPredicates = predicates.length;
final int numberOfPartitions = numberOfPredicates + 1;
final List<R> partitions = new ArrayList<>(numberOfPartitions);
for (int i = 0; i < numberOfPartitions; ++i) {
partitions.add(partitionFactory.create());
}
// for each element in inputCollection:
// find the first predicate that evaluates to true.
// if there is a predicate, add the element to the corresponding partition.
// if there is no predicate, add it to the last, catch-all partition.
for (final O element : iterable) {
boolean elementAssigned = false;
for (int i = 0; i < numberOfPredicates; ++i) {
if (predicates[i].evaluate(element)) {
partitions.get(i).add(element);
elementAssigned = true;
break;
}
}
if (!elementAssigned) {
// no predicates evaluated to true
// add element to last partition
partitions.get(numberOfPredicates).add(element);
}
}
return partitions;