-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
EnumerableDefaults.java
4117 lines (3769 loc) · 138 KB
/
EnumerableDefaults.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.calcite.linq4j;
import org.apache.calcite.linq4j.function.BigDecimalFunction1;
import org.apache.calcite.linq4j.function.DoubleFunction1;
import org.apache.calcite.linq4j.function.EqualityComparer;
import org.apache.calcite.linq4j.function.FloatFunction1;
import org.apache.calcite.linq4j.function.Function0;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.calcite.linq4j.function.Function2;
import org.apache.calcite.linq4j.function.Functions;
import org.apache.calcite.linq4j.function.IntegerFunction1;
import org.apache.calcite.linq4j.function.LongFunction1;
import org.apache.calcite.linq4j.function.NullableBigDecimalFunction1;
import org.apache.calcite.linq4j.function.NullableDoubleFunction1;
import org.apache.calcite.linq4j.function.NullableFloatFunction1;
import org.apache.calcite.linq4j.function.NullableIntegerFunction1;
import org.apache.calcite.linq4j.function.NullableLongFunction1;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.calcite.linq4j.function.Predicate2;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import java.math.BigDecimal;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.RandomAccess;
import java.util.Set;
import java.util.TreeMap;
import static org.apache.calcite.linq4j.Linq4j.CollectionEnumerable;
import static org.apache.calcite.linq4j.Linq4j.ListEnumerable;
import static org.apache.calcite.linq4j.function.Functions.adapt;
/**
* Default implementations of methods in the {@link Enumerable} interface.
*/
public abstract class EnumerableDefaults {
/**
* Applies an accumulator function over a sequence.
*/
public static <TSource> TSource aggregate(Enumerable<TSource> source,
Function2<TSource, TSource, TSource> func) {
TSource result = null;
try (Enumerator<TSource> os = source.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
result = func.apply(result, o);
}
return result;
}
}
/**
* Applies an accumulator function over a
* sequence. The specified seed value is used as the initial
* accumulator value.
*/
public static <TSource, TAccumulate> TAccumulate aggregate(
Enumerable<TSource> source, TAccumulate seed,
Function2<TAccumulate, TSource, TAccumulate> func) {
TAccumulate result = seed;
try (Enumerator<TSource> os = source.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
result = func.apply(result, o);
}
return result;
}
}
/**
* Applies an accumulator function over a
* sequence. The specified seed value is used as the initial
* accumulator value, and the specified function is used to select
* the result value.
*/
public static <TSource, TAccumulate, TResult> TResult aggregate(
Enumerable<TSource> source, TAccumulate seed,
Function2<TAccumulate, TSource, TAccumulate> func,
Function1<TAccumulate, TResult> selector) {
TAccumulate accumulate = seed;
try (Enumerator<TSource> os = source.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
accumulate = func.apply(accumulate, o);
}
return selector.apply(accumulate);
}
}
/**
* Determines whether all elements of a sequence
* satisfy a condition.
*/
public static <TSource> boolean all(Enumerable<TSource> enumerable,
Predicate1<TSource> predicate) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
if (!predicate.apply(o)) {
return false;
}
}
return true;
}
}
/**
* Determines whether a sequence contains any
* elements.
*/
public static boolean any(Enumerable enumerable) {
return enumerable.enumerator().moveNext();
}
/**
* Determines whether any element of a sequence
* satisfies a condition.
*/
public static <TSource> boolean any(Enumerable<TSource> enumerable,
Predicate1<TSource> predicate) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
if (predicate.apply(o)) {
return true;
}
}
return false;
}
}
/**
* Returns the input typed as {@code Enumerable<TSource>}.
*
* <p>This method has no effect other than to change the compile-time type of
* source from a type that implements {@code Enumerable<TSource>} to
* {@code Enumerable<TSource>} itself.
*
* <p>{@code AsEnumerable<TSource>(Enumerable<TSource>)} can be used to choose
* between query implementations when a sequence implements
* {@code Enumerable<TSource>} but also has a different set of public query
* methods available. For example, given a generic class {@code Table} that
* implements {@code Enumerable<TSource>} and has its own methods such as
* {@code where}, {@code select}, and {@code selectMany}, a call to
* {@code where} would invoke the public {@code where} method of
* {@code Table}. A {@code Table} type that represents a database table could
* have a {@code where} method that takes the predicate argument as an
* expression tree and converts the tree to SQL for remote execution. If
* remote execution is not desired, for example because the predicate invokes
* a local method, the {@code asEnumerable<TSource>} method can be used to
* hide the custom methods and instead make the standard query operators
* available.
*/
public static <TSource> Enumerable<TSource> asEnumerable(
Enumerable<TSource> enumerable) {
return enumerable;
}
/**
* Converts an Enumerable to an IQueryable.
*
* <p>Analogous to the LINQ's Enumerable.AsQueryable extension method.</p>
*
* @param enumerable Enumerable
* @param <TSource> Element type
*
* @return A queryable
*/
public static <TSource> Queryable<TSource> asQueryable(
Enumerable<TSource> enumerable) {
throw Extensions.todo();
}
/**
* Computes the average of a sequence of Decimal
* values that are obtained by invoking a transform function on
* each element of the input sequence.
*/
public static <TSource> BigDecimal average(Enumerable<TSource> source,
BigDecimalFunction1<TSource> selector) {
return sum(source, selector).divide(BigDecimal.valueOf(longCount(source)));
}
/**
* Computes the average of a sequence of nullable
* Decimal values that are obtained by invoking a transform
* function on each element of the input sequence.
*/
public static <TSource> BigDecimal average(Enumerable<TSource> source,
NullableBigDecimalFunction1<TSource> selector) {
return sum(source, selector).divide(BigDecimal.valueOf(longCount(source)));
}
/**
* Computes the average of a sequence of Double
* values that are obtained by invoking a transform function on
* each element of the input sequence.
*/
public static <TSource> double average(Enumerable<TSource> source,
DoubleFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* Computes the average of a sequence of nullable
* Double values that are obtained by invoking a transform
* function on each element of the input sequence.
*/
public static <TSource> Double average(Enumerable<TSource> source,
NullableDoubleFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* Computes the average of a sequence of int values
* that are obtained by invoking a transform function on each
* element of the input sequence.
*/
public static <TSource> int average(Enumerable<TSource> source,
IntegerFunction1<TSource> selector) {
return sum(source, selector) / count(source);
}
/**
* Computes the average of a sequence of nullable
* int values that are obtained by invoking a transform function
* on each element of the input sequence.
*/
public static <TSource> Integer average(Enumerable<TSource> source,
NullableIntegerFunction1<TSource> selector) {
return sum(source, selector) / count(source);
}
/**
* Computes the average of a sequence of long values
* that are obtained by invoking a transform function on each
* element of the input sequence.
*/
public static <TSource> long average(Enumerable<TSource> source,
LongFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* Computes the average of a sequence of nullable
* long values that are obtained by invoking a transform function
* on each element of the input sequence.
*/
public static <TSource> Long average(Enumerable<TSource> source,
NullableLongFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* Computes the average of a sequence of Float
* values that are obtained by invoking a transform function on
* each element of the input sequence.
*/
public static <TSource> float average(Enumerable<TSource> source,
FloatFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* Computes the average of a sequence of nullable
* Float values that are obtained by invoking a transform
* function on each element of the input sequence.
*/
public static <TSource> Float average(Enumerable<TSource> source,
NullableFloatFunction1<TSource> selector) {
return sum(source, selector) / longCount(source);
}
/**
* <p>Analogous to LINQ's Enumerable.Cast extension method.</p>
*
* @param clazz Target type
* @param <T2> Target type
*
* @return Collection of T2
*/
public static <TSource, T2> Enumerable<T2> cast(
final Enumerable<TSource> source, final Class<T2> clazz) {
return new AbstractEnumerable<T2>() {
public Enumerator<T2> enumerator() {
return new CastingEnumerator<>(source.enumerator(), clazz);
}
};
}
/**
* Concatenates two sequences.
*/
public static <TSource> Enumerable<TSource> concat(
Enumerable<TSource> enumerable0, Enumerable<TSource> enumerable1) {
//noinspection unchecked
return Linq4j.concat(
Arrays.asList(enumerable0, enumerable1));
}
/**
* Determines whether a sequence contains a specified
* element by using the default equality comparer.
*/
public static <TSource> boolean contains(Enumerable<TSource> enumerable,
TSource element) {
// Implementations of Enumerable backed by a Collection call
// Collection.contains, which may be more efficient, not this method.
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
if (o.equals(element)) {
return true;
}
}
return false;
}
}
/**
* Determines whether a sequence contains a specified
* element by using a specified {@code EqualityComparer<TSource>}.
*/
public static <TSource> boolean contains(Enumerable<TSource> enumerable,
TSource element, EqualityComparer<TSource> comparer) {
for (TSource o : enumerable) {
if (comparer.equal(o, element)) {
return true;
}
}
return false;
}
/**
* Returns the number of elements in a
* sequence.
*/
public static <TSource> int count(Enumerable<TSource> enumerable) {
return (int) longCount(enumerable, Functions.truePredicate1());
}
/**
* Returns a number that represents how many elements
* in the specified sequence satisfy a condition.
*/
public static <TSource> int count(Enumerable<TSource> enumerable,
Predicate1<TSource> predicate) {
return (int) longCount(enumerable, predicate);
}
/**
* Returns the elements of the specified sequence or
* the type parameter's default value in a singleton collection if
* the sequence is empty.
*/
public static <TSource> Enumerable<TSource> defaultIfEmpty(
Enumerable<TSource> enumerable) {
return defaultIfEmpty(enumerable, null);
}
/**
* Returns the elements of the specified sequence or
* the specified value in a singleton collection if the sequence
* is empty.
*/
public static <TSource> Enumerable<TSource> defaultIfEmpty(
Enumerable<TSource> enumerable,
TSource value) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
if (os.moveNext()) {
return Linq4j.asEnumerable(() -> new Iterator<TSource>() {
private boolean nonFirst;
private Iterator<TSource> rest;
public boolean hasNext() {
return !nonFirst || rest.hasNext();
}
public TSource next() {
if (nonFirst) {
return rest.next();
} else {
final TSource first = os.current();
nonFirst = true;
rest = Linq4j.enumeratorIterator(os);
return first;
}
}
public void remove() {
throw new UnsupportedOperationException("remove");
}
});
} else {
return Linq4j.singletonEnumerable(value);
}
}
}
/**
* Returns distinct elements from a sequence by using
* the default {@link EqualityComparer} to compare values.
*/
public static <TSource> Enumerable<TSource> distinct(
Enumerable<TSource> enumerable) {
final Enumerator<TSource> os = enumerable.enumerator();
final Set<TSource> set = new HashSet<>();
while (os.moveNext()) {
set.add(os.current());
}
os.close();
return Linq4j.asEnumerable(set);
}
/**
* Returns distinct elements from a sequence by using
* a specified {@link EqualityComparer} to compare values.
*/
public static <TSource> Enumerable<TSource> distinct(
Enumerable<TSource> enumerable, EqualityComparer<TSource> comparer) {
if (comparer == Functions.identityComparer()) {
return distinct(enumerable);
}
final Set<Wrapped<TSource>> set = new HashSet<>();
Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer);
Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper();
enumerable.select(wrapper).into(set);
return Linq4j.asEnumerable(set).select(unwrapper);
}
/**
* Returns the element at a specified index in a
* sequence.
*/
public static <TSource> TSource elementAt(Enumerable<TSource> enumerable,
int index) {
final ListEnumerable<TSource> list = enumerable instanceof ListEnumerable
? ((ListEnumerable<TSource>) enumerable)
: null;
if (list != null) {
return list.toList().get(index);
}
if (index < 0) {
throw new IndexOutOfBoundsException();
}
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (true) {
if (!os.moveNext()) {
throw new IndexOutOfBoundsException();
}
if (index == 0) {
return os.current();
}
index--;
}
}
}
/**
* Returns the element at a specified index in a
* sequence or a default value if the index is out of
* range.
*/
public static <TSource> TSource elementAtOrDefault(
Enumerable<TSource> enumerable, int index) {
final ListEnumerable<TSource> list = enumerable instanceof ListEnumerable
? ((ListEnumerable<TSource>) enumerable)
: null;
if (index >= 0) {
if (list != null) {
final List<TSource> rawList = list.toList();
if (index < rawList.size()) {
return rawList.get(index);
}
} else {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (true) {
if (!os.moveNext()) {
break;
}
if (index == 0) {
return os.current();
}
index--;
}
}
}
}
return null;
}
/**
* Produces the set difference of two sequences by
* using the default equality comparer to compare values,
* eliminate duplicates. (Defined by Enumerable.)
*/
public static <TSource> Enumerable<TSource> except(
Enumerable<TSource> source0, Enumerable<TSource> source1) {
return except(source0, source1, false);
}
/**
* Produces the set difference of two sequences by
* using the default equality comparer to compare values,
* using {@code all} to indicate whether to eliminate duplicates.
* (Defined by Enumerable.)
*/
public static <TSource> Enumerable<TSource> except(
Enumerable<TSource> source0, Enumerable<TSource> source1, boolean all) {
Collection<TSource> collection = all ? HashMultiset.create() : new HashSet<>();
source0.into(collection);
try (Enumerator<TSource> os = source1.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
collection.remove(o);
}
return Linq4j.asEnumerable(collection);
}
}
/**
* Produces the set difference of two sequences by
* using the specified {@code EqualityComparer<TSource>} to compare
* values, eliminate duplicates.
*/
public static <TSource> Enumerable<TSource> except(
Enumerable<TSource> source0, Enumerable<TSource> source1,
EqualityComparer<TSource> comparer) {
return except(source0, source1, comparer, false);
}
/**
* Produces the set difference of two sequences by
* using the specified {@code EqualityComparer<TSource>} to compare
* values, using {@code all} to indicate whether to eliminate duplicates.
*/
public static <TSource> Enumerable<TSource> except(
Enumerable<TSource> source0, Enumerable<TSource> source1,
EqualityComparer<TSource> comparer, boolean all) {
if (comparer == Functions.identityComparer()) {
return except(source0, source1, all);
}
Collection<Wrapped<TSource>> collection = all ? HashMultiset.create() : new HashSet<>();
Function1<TSource, Wrapped<TSource>> wrapper = wrapperFor(comparer);
source0.select(wrapper).into(collection);
try (Enumerator<Wrapped<TSource>> os =
source1.select(wrapper).enumerator()) {
while (os.moveNext()) {
Wrapped<TSource> o = os.current();
collection.remove(o);
}
}
Function1<Wrapped<TSource>, TSource> unwrapper = unwrapper();
return Linq4j.asEnumerable(collection).select(unwrapper);
}
/**
* Returns the first element of a sequence. (Defined
* by Enumerable.)
*/
public static <TSource> TSource first(Enumerable<TSource> enumerable) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
if (os.moveNext()) {
return os.current();
}
throw new NoSuchElementException();
}
}
/**
* Returns the first element in a sequence that
* satisfies a specified condition.
*/
public static <TSource> TSource first(Enumerable<TSource> enumerable,
Predicate1<TSource> predicate) {
for (TSource o : enumerable) {
if (predicate.apply(o)) {
return o;
}
}
throw new NoSuchElementException();
}
/**
* Returns the first element of a sequence, or a
* default value if the sequence contains no elements.
*/
public static <TSource> TSource firstOrDefault(
Enumerable<TSource> enumerable) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
if (os.moveNext()) {
return os.current();
}
return null;
}
}
/**
* Returns the first element of the sequence that
* satisfies a condition or a default value if no such element is
* found.
*/
public static <TSource> TSource firstOrDefault(Enumerable<TSource> enumerable,
Predicate1<TSource> predicate) {
for (TSource o : enumerable) {
if (predicate.apply(o)) {
return o;
}
}
return null;
}
/**
* Groups the elements of a sequence according to a
* specified key selector function.
*/
public static <TSource, TKey> Enumerable<Grouping<TKey, TSource>> groupBy(
final Enumerable<TSource> enumerable,
final Function1<TSource, TKey> keySelector) {
return enumerable.toLookup(keySelector);
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and compares the keys by using
* a specified comparer.
*/
public static <TSource, TKey> Enumerable<Grouping<TKey, TSource>> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
EqualityComparer<TKey> comparer) {
return enumerable.toLookup(keySelector, comparer);
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and projects the elements for
* each group by using a specified function.
*/
public static <TSource, TKey, TElement> Enumerable<Grouping<TKey, TElement>> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function1<TSource, TElement> elementSelector) {
return enumerable.toLookup(keySelector, elementSelector);
}
/**
* Groups the elements of a sequence according to a
* key selector function. The keys are compared by using a
* comparer and each group's elements are projected by using a
* specified function.
*/
public static <TSource, TKey, TElement> Enumerable<Grouping<TKey, TElement>> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function1<TSource, TElement> elementSelector,
EqualityComparer<TKey> comparer) {
return enumerable.toLookup(keySelector, elementSelector, comparer);
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and creates a result value from
* each group and its key.
*/
public static <TSource, TKey, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
final Function2<TKey, Enumerable<TSource>, TResult> resultSelector) {
return enumerable.toLookup(keySelector)
.select(group -> resultSelector.apply(group.getKey(), group));
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and creates a result value from
* each group and its key. The keys are compared by using a
* specified comparer.
*/
public static <TSource, TKey, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
final Function2<TKey, Enumerable<TSource>, TResult> resultSelector,
EqualityComparer<TKey> comparer) {
return enumerable.toLookup(keySelector, comparer)
.select(group -> resultSelector.apply(group.getKey(), group));
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and creates a result value from
* each group and its key. The elements of each group are
* projected by using a specified function.
*/
public static <TSource, TKey, TElement, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function1<TSource, TElement> elementSelector,
final Function2<TKey, Enumerable<TElement>, TResult> resultSelector) {
return enumerable.toLookup(keySelector, elementSelector)
.select(group -> resultSelector.apply(group.getKey(), group));
}
/**
* Groups the elements of a sequence according to a
* specified key selector function and creates a result value from
* each group and its key. Key values are compared by using a
* specified comparer, and the elements of each group are
* projected by using a specified function.
*/
public static <TSource, TKey, TElement, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function1<TSource, TElement> elementSelector,
final Function2<TKey, Enumerable<TElement>, TResult> resultSelector,
EqualityComparer<TKey> comparer) {
return enumerable.toLookup(keySelector, elementSelector, comparer)
.select(group -> resultSelector.apply(group.getKey(), group));
}
/**
* Groups the elements of a sequence according to a
* specified key selector function, initializing an accumulator for each
* group and adding to it each time an element with the same key is seen.
* Creates a result value from each accumulator and its key using a
* specified function.
*/
public static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function0<TAccumulate> accumulatorInitializer,
Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
final Function2<TKey, TAccumulate, TResult> resultSelector) {
return groupBy_(new HashMap<>(), enumerable, keySelector,
accumulatorInitializer, accumulatorAdder, resultSelector);
}
/**
* Groups the elements of a sequence according to a list of
* specified key selector functions, initializing an accumulator for each
* group and adding to it each time an element with the same key is seen.
* Creates a result value from each accumulator and its key using a
* specified function.
*
* <p>This method exists to support SQL {@code GROUPING SETS}.
* It does not correspond to any method in {@link Enumerable}.
*/
public static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupByMultiple(
Enumerable<TSource> enumerable, List<Function1<TSource, TKey>> keySelectors,
Function0<TAccumulate> accumulatorInitializer,
Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
final Function2<TKey, TAccumulate, TResult> resultSelector) {
return groupByMultiple_(
new HashMap<>(),
enumerable,
keySelectors,
accumulatorInitializer,
accumulatorAdder,
resultSelector);
}
/**
* Groups the elements of a sequence according to a
* specified key selector function, initializing an accumulator for each
* group and adding to it each time an element with the same key is seen.
* Creates a result value from each accumulator and its key using a
* specified function. Key values are compared by using a
* specified comparer.
*/
public static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupBy(
Enumerable<TSource> enumerable, Function1<TSource, TKey> keySelector,
Function0<TAccumulate> accumulatorInitializer,
Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
Function2<TKey, TAccumulate, TResult> resultSelector,
EqualityComparer<TKey> comparer) {
return groupBy_(
new WrapMap<>(
// Java 8 cannot infer return type with HashMap::new is used
() -> new HashMap<Wrapped<TKey>, TAccumulate>(),
comparer),
enumerable,
keySelector,
accumulatorInitializer,
accumulatorAdder,
resultSelector);
}
private static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupBy_(
final Map<TKey, TAccumulate> map, Enumerable<TSource> enumerable,
Function1<TSource, TKey> keySelector,
Function0<TAccumulate> accumulatorInitializer,
Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
final Function2<TKey, TAccumulate, TResult> resultSelector) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
TKey key = keySelector.apply(o);
TAccumulate accumulator = map.get(key);
if (accumulator == null) {
accumulator = accumulatorInitializer.apply();
accumulator = accumulatorAdder.apply(accumulator, o);
map.put(key, accumulator);
} else {
TAccumulate accumulator0 = accumulator;
accumulator = accumulatorAdder.apply(accumulator, o);
if (accumulator != accumulator0) {
map.put(key, accumulator);
}
}
}
}
return new LookupResultEnumerable<>(map, resultSelector);
}
private static <TSource, TKey, TAccumulate, TResult> Enumerable<TResult> groupByMultiple_(
final Map<TKey, TAccumulate> map, Enumerable<TSource> enumerable,
List<Function1<TSource, TKey>> keySelectors,
Function0<TAccumulate> accumulatorInitializer,
Function2<TAccumulate, TSource, TAccumulate> accumulatorAdder,
final Function2<TKey, TAccumulate, TResult> resultSelector) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
for (Function1<TSource, TKey> keySelector : keySelectors) {
TSource o = os.current();
TKey key = keySelector.apply(o);
TAccumulate accumulator = map.get(key);
if (accumulator == null) {
accumulator = accumulatorInitializer.apply();
accumulator = accumulatorAdder.apply(accumulator, o);
map.put(key, accumulator);
} else {
TAccumulate accumulator0 = accumulator;
accumulator = accumulatorAdder.apply(accumulator, o);
if (accumulator != accumulator0) {
map.put(key, accumulator);
}
}
}
}
}
return new LookupResultEnumerable<>(map, resultSelector);
}
private static <TSource, TKey, TResult> Enumerable<TResult> groupBy_(
final Set<TKey> map, Enumerable<TSource> enumerable,
Function1<TSource, TKey> keySelector,
final Function1<TKey, TResult> resultSelector) {
try (Enumerator<TSource> os = enumerable.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
TKey key = keySelector.apply(o);
map.add(key);
}
}
return Linq4j.asEnumerable(map).select(resultSelector);
}
/**
* Correlates the elements of two sequences based on
* equality of keys and groups the results. The default equality
* comparer is used to compare keys.
*/
public static <TSource, TInner, TKey, TResult> Enumerable<TResult> groupJoin(
final Enumerable<TSource> outer, final Enumerable<TInner> inner,
final Function1<TSource, TKey> outerKeySelector,
final Function1<TInner, TKey> innerKeySelector,
final Function2<TSource, Enumerable<TInner>, TResult> resultSelector) {
return new AbstractEnumerable<TResult>() {
final Map<TKey, TSource> outerMap = outer.toMap(outerKeySelector);
final Lookup<TKey, TInner> innerLookup = inner.toLookup(innerKeySelector);
final Enumerator<Map.Entry<TKey, TSource>> entries =
Linq4j.enumerator(outerMap.entrySet());
public Enumerator<TResult> enumerator() {
return new Enumerator<TResult>() {
public TResult current() {
final Map.Entry<TKey, TSource> entry = entries.current();
final Enumerable<TInner> inners = innerLookup.get(entry.getKey());
return resultSelector.apply(entry.getValue(),
inners == null ? Linq4j.emptyEnumerable() : inners);
}
public boolean moveNext() {
return entries.moveNext();
}
public void reset() {
entries.reset();
}
public void close() {
}
};
}
};
}
/**
* Correlates the elements of two sequences based on
* key equality and groups the results. A specified
* {@code EqualityComparer<TSource>} is used to compare keys.
*/
public static <TSource, TInner, TKey, TResult> Enumerable<TResult> groupJoin(
final Enumerable<TSource> outer, final Enumerable<TInner> inner,
final Function1<TSource, TKey> outerKeySelector,
final Function1<TInner, TKey> innerKeySelector,
final Function2<TSource, Enumerable<TInner>, TResult> resultSelector,
final EqualityComparer<TKey> comparer) {
return new AbstractEnumerable<TResult>() {
final Map<TKey, TSource> outerMap = outer.toMap(outerKeySelector, comparer);
final Lookup<TKey, TInner> innerLookup = inner.toLookup(innerKeySelector, comparer);
final Enumerator<Map.Entry<TKey, TSource>> entries =
Linq4j.enumerator(outerMap.entrySet());
public Enumerator<TResult> enumerator() {
return new Enumerator<TResult>() {
public TResult current() {
final Map.Entry<TKey, TSource> entry = entries.current();
final Enumerable<TInner> inners = innerLookup.get(entry.getKey());
return resultSelector.apply(entry.getValue(),
inners == null ? Linq4j.emptyEnumerable() : inners);
}
public boolean moveNext() {
return entries.moveNext();
}
public void reset() {
entries.reset();
}
public void close() {
}
};
}
};
}
/**
* Produces the set intersection of two sequences by
* using the default equality comparer to compare values,
* eliminate duplicates.(Defined by Enumerable.)
*/
public static <TSource> Enumerable<TSource> intersect(
Enumerable<TSource> source0, Enumerable<TSource> source1) {
return intersect(source0, source1, false);
}
/**
* Produces the set intersection of two sequences by
* using the default equality comparer to compare values,
* using {@code all} to indicate whether to eliminate duplicates.
* (Defined by Enumerable.)
*/
public static <TSource> Enumerable<TSource> intersect(
Enumerable<TSource> source0, Enumerable<TSource> source1, boolean all) {
Collection<TSource> set1 = all ? HashMultiset.create() : new HashSet<>();
source1.into(set1);
Collection<TSource> resultCollection = all ? HashMultiset.create() : new HashSet<>();
try (Enumerator<TSource> os = source0.enumerator()) {
while (os.moveNext()) {
TSource o = os.current();
if (set1.remove(o)) {
resultCollection.add(o);
}
}
}
return Linq4j.asEnumerable(resultCollection);
}