-
Notifications
You must be signed in to change notification settings - Fork 587
/
RandomAccessListIterate.java
1792 lines (1658 loc) · 55.4 KB
/
RandomAccessListIterate.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
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.utility.internal;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.RandomAccess;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.primitive.BooleanLists;
import org.eclipse.collections.api.factory.primitive.ByteLists;
import org.eclipse.collections.api.factory.primitive.CharLists;
import org.eclipse.collections.api.factory.primitive.DoubleLists;
import org.eclipse.collections.api.factory.primitive.FloatLists;
import org.eclipse.collections.api.factory.primitive.IntLists;
import org.eclipse.collections.api.factory.primitive.LongLists;
import org.eclipse.collections.api.factory.primitive.ObjectDoubleMaps;
import org.eclipse.collections.api.factory.primitive.ObjectLongMaps;
import org.eclipse.collections.api.factory.primitive.ShortLists;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableBooleanList;
import org.eclipse.collections.api.list.primitive.MutableByteList;
import org.eclipse.collections.api.list.primitive.MutableCharList;
import org.eclipse.collections.api.list.primitive.MutableDoubleList;
import org.eclipse.collections.api.list.primitive.MutableFloatList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.block.factory.Functions0;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.partition.list.PartitionFastList;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.ListIterate;
/**
* The ListIterate class provides a few of the methods from the Smalltalk Collection Protocol for use with ArrayLists.
* This includes do:, select:, reject:, collect:, inject:into:, detect:, detect:ifNone:, anySatisfy: and allSatisfy:
*/
public final class RandomAccessListIterate
{
private RandomAccessListIterate()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
public static <T> void toArray(List<T> list, T[] target, int startIndex, int sourceSize)
{
for (int i = 0; i < sourceSize; i++)
{
target[startIndex + i] = list.get(i);
}
}
/**
* @see Iterate#select(Iterable, Predicate)
*/
public static <T> MutableList<T> select(List<T> list, Predicate<? super T> predicate)
{
return RandomAccessListIterate.select(list, predicate, FastList.newList());
}
/**
* @see Iterate#selectWith(Iterable, Predicate2, Object)
*/
public static <T, IV> MutableList<T> selectWith(
List<T> list,
Predicate2<? super T, ? super IV> predicate,
IV injectedValue)
{
return RandomAccessListIterate.selectWith(list, predicate, injectedValue, FastList.newList());
}
/**
* @see Iterate#select(Iterable, Predicate, Collection)
*/
public static <T, R extends Collection<T>> R select(
List<T> list,
Predicate<? super T> predicate,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
*/
public static <T, P, R extends Collection<T>> R selectWith(
List<T> list,
Predicate2<? super T, ? super P> predicate,
P parameter,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item, parameter))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#selectInstancesOf(Iterable, Class)
*/
public static <T> MutableList<T> selectInstancesOf(
List<?> list,
Class<T> clazz)
{
int size = list.size();
FastList<T> result = FastList.newList(size);
for (int i = 0; i < size; i++)
{
Object item = list.get(i);
if (clazz.isInstance(item))
{
result.add((T) item);
}
}
result.trimToSize();
return result;
}
/**
* @see Iterate#count(Iterable, Predicate)
*/
public static <T> int count(
List<T> list,
Predicate<? super T> predicate)
{
int count = 0;
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)))
{
count++;
}
}
return count;
}
public static <T, IV> int countWith(
List<T> list,
Predicate2<? super T, ? super IV> predicate,
IV injectedValue)
{
int count = 0;
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i), injectedValue))
{
count++;
}
}
return count;
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function)
*/
public static <T, A> MutableList<A> collectIf(
List<T> list,
Predicate<? super T> predicate,
Function<? super T, ? extends A> function)
{
return RandomAccessListIterate.collectIf(list, predicate, function, FastList.newList());
}
/**
* @see Iterate#collectIf(Iterable, Predicate, Function, Collection)
*/
public static <T, A, R extends Collection<A>> R collectIf(
List<T> list,
Predicate<? super T> predicate,
Function<? super T, ? extends A> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (predicate.accept(item))
{
targetCollection.add(function.valueOf(item));
}
}
return targetCollection;
}
/**
* @see Iterate#reject(Iterable, Predicate)
*/
public static <T> MutableList<T> reject(List<T> list, Predicate<? super T> predicate)
{
return RandomAccessListIterate.reject(list, predicate, FastList.newList());
}
/**
* @see Iterate#rejectWith(Iterable, Predicate2, Object)
*/
public static <T, IV> MutableList<T> rejectWith(
List<T> list,
Predicate2<? super T, ? super IV> predicate,
IV injectedValue)
{
return RandomAccessListIterate.rejectWith(list, predicate, injectedValue, FastList.newList());
}
/**
* @see Iterate#reject(Iterable, Predicate, Collection)
*/
public static <T, R extends Collection<T>> R reject(
List<T> list,
Predicate<? super T> predicate,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (!predicate.accept(item))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#reject(Iterable, Predicate, Collection)
*/
public static <T, P, R extends Collection<T>> R rejectWith(
List<T> list,
Predicate2<? super T, ? super P> predicate,
P parameter,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (!predicate.accept(item, parameter))
{
targetCollection.add(item);
}
}
return targetCollection;
}
/**
* @see Iterate#collect(Iterable, Function)
*/
public static <T, A> MutableList<A> collect(
List<T> list,
Function<? super T, ? extends A> function)
{
return RandomAccessListIterate.collect(list, function, FastList.newList(list.size()));
}
/**
* @see Iterate#collect(Iterable, Function, Collection)
*/
public static <T, A, R extends Collection<A>> R collect(
List<T> list,
Function<? super T, ? extends A> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
targetCollection.add(function.valueOf(list.get(i)));
}
return targetCollection;
}
/**
* @since 9.1
*/
public static <T, A> MutableList<A> collectWithIndex(List<T> list, ObjectIntToObjectFunction<? super T, ? extends A> function)
{
return RandomAccessListIterate.collectWithIndex(list, function, FastList.newList(list.size()));
}
/**
* @since 9.1
*/
public static <T, A, R extends Collection<A>> R collectWithIndex(List<T> list, ObjectIntToObjectFunction<? super T, ? extends A> function, R targetCollection)
{
for (int i = 0; i < list.size(); i++)
{
targetCollection.add(function.valueOf(list.get(i), i));
}
return targetCollection;
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction)
*/
public static <T> MutableBooleanList collectBoolean(
List<T> list,
BooleanFunction<? super T> booleanFunction)
{
return RandomAccessListIterate.collectBoolean(list, booleanFunction, BooleanLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectBoolean(Iterable, BooleanFunction)
*/
public static <T, R extends MutableBooleanCollection> R collectBoolean(
List<T> list,
BooleanFunction<? super T> booleanFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(booleanFunction.booleanValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction)
*/
public static <T> MutableByteList collectByte(
List<T> list,
ByteFunction<? super T> byteFunction)
{
return RandomAccessListIterate.collectByte(list, byteFunction, ByteLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectByte(Iterable, ByteFunction)
*/
public static <T, R extends MutableByteCollection> R collectByte(
List<T> list,
ByteFunction<? super T> byteFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(byteFunction.byteValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectChar(Iterable, CharFunction)
*/
public static <T> MutableCharList collectChar(
List<T> list,
CharFunction<? super T> charFunction)
{
return RandomAccessListIterate.collectChar(list, charFunction, CharLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectChar(Iterable, CharFunction)
*/
public static <T, R extends MutableCharCollection> R collectChar(
List<T> list,
CharFunction<? super T> charFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(charFunction.charValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction)
*/
public static <T> MutableDoubleList collectDouble(
List<T> list,
DoubleFunction<? super T> doubleFunction)
{
return RandomAccessListIterate.collectDouble(list, doubleFunction, DoubleLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectDouble(Iterable, DoubleFunction)
*/
public static <T, R extends MutableDoubleCollection> R collectDouble(
List<T> list,
DoubleFunction<? super T> doubleFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(doubleFunction.doubleValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction)
*/
public static <T> MutableFloatList collectFloat(
List<T> list,
FloatFunction<? super T> floatFunction)
{
return RandomAccessListIterate.collectFloat(list, floatFunction, FloatLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectFloat(Iterable, FloatFunction)
*/
public static <T, R extends MutableFloatCollection> R collectFloat(
List<T> list,
FloatFunction<? super T> floatFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(floatFunction.floatValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectInt(Iterable, IntFunction)
*/
public static <T> MutableIntList collectInt(
List<T> list,
IntFunction<? super T> intFunction)
{
return RandomAccessListIterate.collectInt(list, intFunction, IntLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectInt(Iterable, IntFunction)
*/
public static <T, R extends MutableIntCollection> R collectInt(
List<T> list,
IntFunction<? super T> intFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(intFunction.intValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectLong(Iterable, LongFunction)
*/
public static <T> MutableLongList collectLong(
List<T> list,
LongFunction<? super T> longFunction)
{
return RandomAccessListIterate.collectLong(list, longFunction, LongLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectLong(Iterable, LongFunction)
*/
public static <T, R extends MutableLongCollection> R collectLong(
List<T> list,
LongFunction<? super T> longFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(longFunction.longValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction)
*/
public static <T> MutableShortList collectShort(
List<T> list,
ShortFunction<? super T> shortFunction)
{
return RandomAccessListIterate.collectShort(list, shortFunction, ShortLists.mutable.withInitialCapacity(list.size()));
}
/**
* @see Iterate#collectShort(Iterable, ShortFunction)
*/
public static <T, R extends MutableShortCollection> R collectShort(
List<T> list,
ShortFunction<? super T> shortFunction,
R target)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
target.add(shortFunction.shortValueOf(list.get(i)));
}
return target;
}
/**
* @see Iterate#flatCollect(Iterable, Function)
*/
public static <T, A> MutableList<A> flatCollect(
List<T> list,
Function<? super T, ? extends Iterable<A>> function)
{
return RandomAccessListIterate.flatCollect(list, function, FastList.newList(list.size()));
}
/**
* @see Iterate#flatCollect(Iterable, Function, Collection)
*/
public static <T, A, R extends Collection<A>> R flatCollect(
List<T> list,
Function<? super T, ? extends Iterable<A>> function,
R targetCollection)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
Iterate.addAllTo(function.valueOf(list.get(i)), targetCollection);
}
return targetCollection;
}
/**
* Returns the last element of a list.
*/
public static <T> T getLast(List<T> collection)
{
return Iterate.isEmpty(collection) ? null : collection.get(collection.size() - 1);
}
/**
* @see Iterate#forEach(Iterable, Procedure)
*/
public static <T> void forEach(List<T> list, Procedure<? super T> procedure)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
procedure.value(list.get(i));
}
}
/**
* Iterates over the section of the list covered by the specified indexes. The indexes are both inclusive. If the
* from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the
* list is iterated in the reverse order.
*
* <pre>e.g.
* MutableList<People> people = FastList.newListWith(ted, mary, bob, sally);
* ListIterate.forEach(people, 0, 1, new Procedure<Person>()
* {
* public void value(Person person)
* {
* LOGGER.info(person.getName());
* }
* });
* </pre>
* <p>
* This code would output ted and mary's names.
*/
public static <T> void forEach(List<T> list, int from, int to, Procedure<? super T> procedure)
{
ListIterate.rangeCheck(from, to, list.size());
if (from <= to)
{
for (int i = from; i <= to; i++)
{
procedure.value(list.get(i));
}
}
else
{
for (int i = from; i >= to; i--)
{
procedure.value(list.get(i));
}
}
}
/**
* Iterates over the section of the list covered by the specified indexes. The indexes are both inclusive. If the
* from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the
* list is iterated in the reverse order. The index passed into the ObjectIntProcedure is the actual index of the
* range.
*
* <pre>e.g.
* MutableList<People> people = FastList.newListWith(ted, mary, bob, sally);
* ListIterate.forEachWithIndex(people, 0, 1, new ObjectIntProcedure<Person>()
* {
* public void value(Person person, int index)
* {
* LOGGER.info(person.getName() + " at index: " + index);
* }
* });
* </pre>
* <p>
* This code would output ted and mary's names.
*/
public static <T> void forEachWithIndex(List<T> list, int from, int to, ObjectIntProcedure<? super T> objectIntProcedure)
{
ListIterate.rangeCheck(from, to, list.size());
if (from <= to)
{
for (int i = from; i <= to; i++)
{
objectIntProcedure.value(list.get(i), i);
}
}
else
{
for (int i = from; i >= to; i--)
{
objectIntProcedure.value(list.get(i), i);
}
}
}
/**
* For each element in both of the Lists, operation is evaluated with both elements as parameters.
*/
public static <T1, T2> void forEachInBoth(List<T1> list1, List<T2> list2, Procedure2<? super T1, ? super T2> procedure)
{
if (list1 != null && list2 != null)
{
int size1 = list1.size();
int size2 = list2.size();
if (size1 == size2)
{
for (int i = 0; i < size1; i++)
{
procedure.value(list1.get(i), list2.get(i));
}
}
else
{
throw new IllegalArgumentException("Attempt to call forEachInBoth with two Lists of different sizes :"
+ size1
+ ':'
+ size2);
}
}
}
/**
* Iterates over a collection passing each element and the current relative int index to the specified instance of
* ObjectIntProcedure.
*/
public static <T> void forEachWithIndex(List<T> list, ObjectIntProcedure<? super T> objectIntProcedure)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
objectIntProcedure.value(list.get(i), i);
}
}
public static <T, IV> IV injectInto(IV injectValue, List<T> list, Function2<? super IV, ? super T, ? extends IV> function)
{
IV result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.value(result, list.get(i));
}
return result;
}
public static <T> int injectInto(int injectValue, List<T> list, IntObjectToIntFunction<? super T> function)
{
int result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.intValueOf(result, list.get(i));
}
return result;
}
public static <T> long injectInto(long injectValue, List<T> list, LongObjectToLongFunction<? super T> function)
{
long result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.longValueOf(result, list.get(i));
}
return result;
}
public static <T> double injectInto(double injectValue, List<T> list, DoubleObjectToDoubleFunction<? super T> function)
{
double result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.doubleValueOf(result, list.get(i));
}
return result;
}
public static <T> float injectInto(float injectValue, List<T> list, FloatObjectToFloatFunction<? super T> function)
{
float result = injectValue;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = function.floatValueOf(result, list.get(i));
}
return result;
}
public static <T> long sumOfInt(List<T> list, IntFunction<? super T> function)
{
long result = 0;
int size = list.size();
for (int i = 0; i < size; i++)
{
result += (long) function.intValueOf(list.get(i));
}
return result;
}
public static <T> long sumOfLong(List<T> list, LongFunction<? super T> function)
{
long result = 0L;
int size = list.size();
for (int i = 0; i < size; i++)
{
result += function.longValueOf(list.get(i));
}
return result;
}
public static <T> double sumOfFloat(List<T> list, FloatFunction<? super T> function)
{
double sum = 0.0d;
double compensation = 0.0d;
int size = list.size();
for (int i = 0; i < size; i++)
{
double adjustedValue = (double) function.floatValueOf(list.get(i)) - compensation;
double nextSum = sum + adjustedValue;
compensation = nextSum - sum - adjustedValue;
sum = nextSum;
}
return sum;
}
public static <T> double sumOfDouble(List<T> list, DoubleFunction<? super T> function)
{
double sum = 0.0d;
double compensation = 0.0d;
int size = list.size();
for (int i = 0; i < size; i++)
{
double adjustedValue = function.doubleValueOf(list.get(i)) - compensation;
double nextSum = sum + adjustedValue;
compensation = nextSum - sum - adjustedValue;
sum = nextSum;
}
return sum;
}
public static <T> BigDecimal sumOfBigDecimal(List<T> list, Function<? super T, BigDecimal> function)
{
BigDecimal result = BigDecimal.ZERO;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = result.add(function.valueOf(list.get(i)));
}
return result;
}
public static <T> BigInteger sumOfBigInteger(List<T> list, Function<? super T, BigInteger> function)
{
BigInteger result = BigInteger.ZERO;
int size = list.size();
for (int i = 0; i < size; i++)
{
result = result.add(function.valueOf(list.get(i)));
}
return result;
}
public static <V, T> MutableMap<V, BigDecimal> sumByBigDecimal(
List<T> list,
Function<? super T, ? extends V> groupBy,
Function<? super T, BigDecimal> function)
{
MutableMap<V, BigDecimal> result = UnifiedMap.newMap();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
result.updateValue(groupBy.valueOf(item), Functions0.zeroBigDecimal(), original -> original.add(function.valueOf(item)));
}
return result;
}
public static <V, T> MutableMap<V, BigInteger> sumByBigInteger(
List<T> list,
Function<? super T, ? extends V> groupBy,
Function<? super T, BigInteger> function)
{
MutableMap<V, BigInteger> result = UnifiedMap.newMap();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
result.updateValue(groupBy.valueOf(item), Functions0.zeroBigInteger(), original -> original.add(function.valueOf(item)));
}
return result;
}
public static <T> boolean shortCircuit(
List<T> list,
Predicate<? super T> predicate,
boolean expected,
boolean onShortCircuit,
boolean atEnd)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate.accept(list.get(i)) == expected)
{
return onShortCircuit;
}
}
return atEnd;
}
public static <T, P> boolean shortCircuitWith(
List<T> list,
Predicate2<? super T, ? super P> predicate2,
P parameter,
boolean expected,
boolean onShortCircuit,
boolean atEnd)
{
int size = list.size();
for (int i = 0; i < size; i++)
{
if (predicate2.accept(list.get(i), parameter) == expected)
{
return onShortCircuit;
}
}
return atEnd;
}
public static <T, P> boolean corresponds(List<T> list, OrderedIterable<P> other, Predicate2<? super T, ? super P> predicate)
{
if (!(list instanceof RandomAccess))
{
throw new IllegalArgumentException();
}
int size = list.size();
if (size != other.size())
{
return false;
}
if (other instanceof RandomAccess)
{
List<P> otherList = (List<P>) other;
for (int index = 0; index < size; index++)
{
if (!predicate.accept(list.get(index), otherList.get(index)))
{
return false;
}
}
return true;
}
Iterator<P> iterator = other.iterator();
for (int index = 0; index < size; index++)
{
if (!predicate.accept(list.get(index), iterator.next()))
{
return false;
}
}
return true;
}
public static <T> boolean anySatisfy(List<T> list, Predicate<? super T> predicate)
{
return RandomAccessListIterate.shortCircuit(list, predicate, true, true, false);
}
public static <T, P> boolean anySatisfyWith(List<T> list, Predicate2<? super T, ? super P> predicate, P parameter)
{
return RandomAccessListIterate.shortCircuitWith(list, predicate, parameter, true, true, false);
}
public static <T> boolean allSatisfy(List<T> list, Predicate<? super T> predicate)
{
return RandomAccessListIterate.shortCircuit(list, predicate, false, false, true);
}
public static <T, P> boolean allSatisfyWith(List<T> list, Predicate2<? super T, ? super P> predicate, P parameter)
{
return RandomAccessListIterate.shortCircuitWith(list, predicate, parameter, false, false, true);
}
public static <T> boolean noneSatisfy(List<T> list, Predicate<? super T> predicate)
{
return RandomAccessListIterate.shortCircuit(list, predicate, true, false, true);
}