-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIx.java
More file actions
2819 lines (2635 loc) · 107 KB
/
Copy pathIx.java
File metadata and controls
2819 lines (2635 loc) · 107 KB
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 2011-2016 David Karnok
*
* Licensed 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 ix;
import java.util.*;
import java.util.concurrent.Callable;
/**
* Base class and entry point for fluent Iterables.
* <p>
* All operators tolerate {@code null} elements.
* <p>
* The Iterables have to be run in a single-threaded manner and none of
* the participating operators expect or support concurrency.
* @param <T> the value type
* @since 1.0
*/
public abstract class Ix<T> implements Iterable<T> {
/**
* Emits all characters from the given CharSequence as integer values.
* <p>
* The result's iterator() doesn't support remove().
* @param cs the source character sequence, not null
* @return the new Ix instance
* @throws NullPointerException if cs is null
* @since 1.0
*/
public static Ix<Integer> characters(CharSequence cs) {
return new IxCharacters(cs, 0, cs.length());
}
/**
* Emits a range of characters from the given CharSequence as integer values.
* <p>
* The result's iterator() doesn't support remove().
* @param cs the source character sequence, not null
* @param start the start character index, inclusive, non-negative
* @param end the end character index, exclusive, non-negative
* @return the new Ix instance
* @throws NullPointerException if cs is null
* @throws IndexOutOfBoundsException if start is out of range [0, cs.length]
* @since 1.0
*/
public static Ix<Integer> characters(CharSequence cs, int start, int end) {
int len = cs.length();
if (start < 0 || end < 0 || start > len || end > len) {
throw new IndexOutOfBoundsException("start=" + start + ", end=" + end + ", length=" + len);
}
return new IxCharacters(cs, start, end);
}
/**
* Concatenates the elements of Iterable sources, provided as an Iterable itself, sequentially.
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the common base type
* @param sources the Iterable sequence of source Iterables
* @return the new Ix instance
* @throws NullPointerException if sources is null
* @since 1.0
* @see #merge(Iterable)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Ix<T> concat(Iterable<? extends Iterable<? extends T>> sources) {
return new IxFlattenIterable<Iterable<? extends T>, T>(
(Iterable)nullCheck(sources, "sources is null"),
IdentityHelper.<Iterable<? extends T>>instance());
}
/**
* Concatenates the elements of two Iterable sources sequentially
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the value type
* @param source1 the first source, not null
* @param source2 the second source, not null
* @return the new Iterable source
* @throws NullPointerException if any of the sources is null
* @since 1.0
*/
@SuppressWarnings("unchecked")
public static <T> Ix<T> concat(Iterable<? extends T> source1, Iterable<? extends T> source2) {
return concatArray(nullCheck(source1, "source1 is null"), nullCheck(source2, "source2 is null"));
}
/**
* Concatenates the elements of three Iterable sources sequentially
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the value type
* @param source1 the first source, not null
* @param source2 the second source, not null
* @param source3 the third source, not null
* @return the new Iterable source
* @throws NullPointerException if any of the sources is null
* @since 1.0
*/
@SuppressWarnings("unchecked")
public static <T> Ix<T> concat(Iterable<? extends T> source1, Iterable<? extends T> source2,
Iterable<? extends T> source3) {
return concatArray(nullCheck(source1, "source1 is null"),
nullCheck(source2, "source2 is null"), nullCheck(source3, "source3 is null"));
}
/**
* Concatenates the elements of three Iterable sources sequentially
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the value type
* @param source1 the first source, not null
* @param source2 the second source, not null
* @param source3 the third source, not null
* @param source4 the fourth source, not null
* @return the new Iterable source
* @throws NullPointerException if any of the sources is null
* @since 1.0
*/
@SuppressWarnings("unchecked")
public static <T> Ix<T> concat(Iterable<? extends T> source1, Iterable<? extends T> source2,
Iterable<? extends T> source3, Iterable<? extends T> source4) {
return concatArray(nullCheck(source1, "source1 is null"), nullCheck(source2, "source2 is null"),
nullCheck(source3, "source3 is null"), nullCheck(source4, "source4 is null"));
}
/**
* Concatenates the elements of Iterable sources, provided as an array, sequentially.
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the common base type
* @param sources the array of source Iterables
* @return the new Ix instance
* @throws NullPointerException if sources is null
* @since 1.0
* @see #mergeArray(Iterable...)
*/
@SuppressWarnings("unchecked")
public static <T> Ix<T> concatArray(Iterable<? extends T>... sources) {
int n = sources.length;
if (n == 0) {
return empty();
}
if (n == 1) {
return from((Iterable<T>)sources[0]);
}
return new IxFlattenArrayIterable<T>((Iterable<T>[])sources);
}
/**
* Defers the generation of the actual Iterable till the iterator() is called on
* the resulting Ix.
* <p>
* The result's iterator() forwards the remove() calls to the generated Iterable's Iterator.
* @param <T> the value type
* @param factory the function that returns an Iterable when the resulting Ix.iterator() is called
* @return the new Ix source
* @throws NullPointerException if factory is null
* @since 1.0
*/
public static <T> Ix<T> defer(IxSupplier<? extends Iterable<? extends T>> factory) {
return new IxDefer<T>(nullCheck(factory, "factory is null"));
}
/**
* No elements are emitted.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> empty() {
return IxEmpty.instance();
}
/**
* Wraps the given Iterable source into an Ix instance (if
* not already an Ix subclass).
* <p>
* The result's iterator() forwards the remove() calls to the source's
* iterator().
* @param <T> the value type
* @param source the Iterable to wrap, not null
* @return the new Ix instance
* @throws NullPointerException if source is null
* @since 1.0
*/
public static <T> Ix<T> from(Iterable<T> source) {
if (source instanceof Ix) {
return (Ix<T>)source;
}
return new IxWrapper<T>(nullCheck(source, "source"));
}
/**
* Emits all the elements of the given array.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param values the array of values, not null
* @return the new Ix instance
* @throws NullPointerException if values is null
* @since 1.0
*/
public static <T> Ix<T> fromArray(T... values) {
int n = values.length;
if (n == 0) {
return empty();
}
if (n == 1) {
return just(values[0]);
}
return new IxFromArray<T>(0, values.length, values);
}
/**
* Emits a range of elements from the given array.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param start the staring index, inclusive, non-negative
* @param end the end index, exclusive, non-negative
* @param values the array of values
* @return the new Ix instance
* @throws NullPointerException if values is null
* @throws IndexOutOfBoundsException if either start or end are not in [0, values.length]
* @since 1.0
*/
public static <T> Ix<T> fromArrayRange(int start, int end, T... values) {
if (start < 0 || end < 0 || start > values.length || end > values.length) {
throw new IndexOutOfBoundsException("start=" + start + ", end=" + end + ", length=" + values.length);
}
return new IxFromArray<T>(start, end, values);
}
/**
* Generates a sequence of values via a generic indexed for-loop style construct;
* the index starts with the given seed, checked via a condition (to terminate),
* generated from the index via the selector and then a new index is generated via next.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the index value type
* @param <R> the result value type
* @param seed the initial value of the index
* @param condition the receives the current index (before selector is called) and if it
* returns false, the sequence terminates
* @param next the function that receives the current index and returns the next index
* @param selector the function that receives the current index and returns the value
* to be emitted.
* @return the new Ix instance
* @throws NullPointerException if condition, next or selector is null
* @since 1.0
*/
public static <T, R> Ix<R> forloop(T seed, IxPredicate<? super T> condition,
IxFunction<? super T, ? extends T> next,
IxFunction<? super T, ? extends R> selector) {
return new IxForloop<T, R>(seed, nullCheck(condition, "condition is null"),
nullCheck(selector, "selector is null"), nullCheck(next, "next is null"));
}
/**
* Calls the given action to generate a value or terminate whenever the next()
* is called on the resulting Ix.iterator().
* <p>
* The result's iterator() doesn't support remove().
* <p>
* The action may call {@code onNext} at most once to signal the next value per action invocation.
* The {@code onCompleted} should be called to indicate no further values will be generated (may be
* called with an onNext in the same action invocation). Calling {@code onError} will immediately
* throw the given exception (as is if it's a RuntimeException or Error; or wrapped into a RuntimeException).
* @param <T> the value type
* @param nextSupplier the action called with an IxEmitter API to receive value, not null
* @return the new Ix instance
* @throws NullPointerException if nextSupplier is null
* @since 1.0
*/
public static <T> Ix<T> generate(IxConsumer<IxEmitter<T>> nextSupplier) {
return new IxGenerateStateless<T>(nullCheck(nextSupplier, "nextSupplier is null"));
}
/**
* Calls the given function (with per-iterator state) to generate a value or terminate
* whenever the next() is called on the resulting Ix.iterator().
* <p>
* The result's iterator() doesn't support remove().
* <p>
* The action may call {@code onNext} at most once to signal the next value per action invocation.
* The {@code onCompleted} should be called to indicate no further values will be generated (may be
* called with an onNext in the same action invocation). Calling {@code onError} will immediately
* throw the given exception (as is if it's a RuntimeException or Error; or wrapped into a RuntimeException).
* @param <T> the value type
* @param <S> the state type supplied to and returned by the nextSupplier function
* @param stateSupplier the function that returns a state for each invocation of iterator()
* @param nextSupplier the action called with an IxEmitter API to receive value, not null
* @return the new Ix instance
* @throws NullPointerException if stateSupplier or nextSupplier is null
* @since 1.0
*/
public static <T, S> Ix<T> generate(IxSupplier<S> stateSupplier, IxFunction2<S, IxEmitter<T>, S> nextSupplier) {
return generate(stateSupplier, nextSupplier, IxEmptyAction.instance1());
}
/**
* Calls the given function (with per-iterator state) to generate a value or terminate
* whenever the next() is called on the resulting Ix.iterator().
* <p>
* The result's iterator() doesn't support remove().
* <p>
* The action may call {@code onNext} at most once to signal the next value per action invocation.
* The {@code onCompleted} should be called to indicate no further values will be generated (may be
* called with an onNext in the same action invocation). Calling {@code onError} will immediately
* throw the given exception (as is if it's a RuntimeException or Error; or wrapped into a RuntimeException).
* <p>
* Note that since there is no direct way to cancel an Iterator, the stateDisposer is only invoked
* when the nextSupplier calls a terminal method.
* @param <T> the value type
* @param <S> the state type supplied to and returned by the nextSupplier function
* @param stateSupplier the function that returns a state for each invocation of iterator()
* @param nextSupplier the action called with an IxEmitter API to receive value, not null
* @param stateDisposer the action called when the nextSupplier signals an {@code onError} or {@code onCompleted}.
* @return the new Ix instance
* @throws NullPointerException if stateSupplier, nextSupplier or stateDisposer is null
* @since 1.0
*/
public static <T, S> Ix<T> generate(IxSupplier<S> stateSupplier, IxFunction2<S, IxEmitter<T>, S> nextSupplier, IxConsumer<? super S> stateDisposer) {
return new IxGenerate<T, S>(nullCheck(stateSupplier, "stateSupplier is null"),
nullCheck(nextSupplier, "nextSupplier is null"), nullCheck(stateDisposer, "stateDisposer is null"));
}
/**
* Emits a single constant value.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param value the constant value to emit
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> just(T value) {
return new IxJust<T>(value);
}
/**
* Concatenates the elements of Iterable sources, provided as an Iterable itself, sequentially.
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the common base type
* @param sources the Iterable sequence of source Iterables
* @return the new Ix instance
* @throws NullPointerException if sources is null
* @since 1.0
* @see #concat(Iterable)
*/
public static <T> Ix<T> merge(Iterable<? extends Iterable<? extends T>> sources) {
return concat(sources);
}
/**
* Concatenates the elements of Iterable sources, provided as an array, sequentially.
* <p>
* The result's iterator() forwards the remove() calls to the current iterator.
* <p>
* Note that merge and concat operations are the same in the Iterable world.
* @param <T> the common base type
* @param sources the array of source Iterables
* @return the new Ix instance
* @throws NullPointerException if sources is null
* @since 1.0
* @see #concatArray(Iterable...)
*/
public static <T> Ix<T> mergeArray(Iterable<? extends T>... sources) {
return concatArray(sources); // concat and merge are the same in the Iterable world
}
/**
* Merges self-comparable items from an Iterable sequence of Iterable sequences, picking
* the smallest item from all those inner Iterables until all sources complete.
* @param <T> the value type
* @param sources the Iterable sequence of Iterables of self-comparable items
* @return the new Ix instance
* @since 1.0
*/
public static <T extends Comparable<? super T>> Ix<T> orderedMerge(Iterable<? extends Iterable<? extends T>> sources) {
return orderedMerge(sources, SelfComparator.INSTANCE);
}
/**
* Merges items from an Iterable sequence of Iterable sequences, picking
* the smallest item (according to a custom comparator) from all those inner
* Iterables until all sources complete.
* @param <T> the value type
* @param sources the Iterable sequence of Iterables
* @param comparator the comparator to compare items and pick the one that returns negative will be picked
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> orderedMerge(Iterable<? extends Iterable<? extends T>> sources, Comparator<? super T> comparator) {
return new IxOrderedMergeIterable<T>(nullCheck(sources, "sources is null"), nullCheck(comparator, "comparator is null"));
}
/**
* Merges self-comparable items from an Iterable sequence of Iterable sequences, picking
* the smallest item from all those inner Iterables until all sources complete.
* @param <T> the value type
* @param sources the Iterable sequence of Iterables of self-comparable items
* @return the new Ix instance
* @since 1.0
*/
public static <T extends Comparable<? super T>> Ix<T> orderedMergeArray(Iterable<? extends T>... sources) {
return orderedMergeArray(SelfComparator.INSTANCE, sources);
}
/**
* Merges items from an array of Iterable sequences, picking
* the smallest item (according to a custom comparator) from all those inner
* Iterables until all sources complete.
* @param <T> the value type
* @param sources the Iterable sequence of Iterables
* @param comparator the comparator to compare items and pick the one that returns negative will be picked
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> orderedMergeArray(Comparator<? super T> comparator, Iterable<? extends T>... sources) {
return new IxOrderedMergeArray<T>(nullCheck(sources, "sources is null"), nullCheck(comparator, "comparator is null"));
}
/**
* Emits a range of incrementing integer values, starting from {@code start} and
* up to {@code count} times.
* @param start the starting value
* @param count the number of integers to emit, non-negative
* @return the new Ix instance
* @throws IllegalArgumentException if count is negative
* @since 1.0
*/
public static Ix<Integer> range(int start, int count) {
if (count == 0) {
return empty();
}
if (count == 1) {
return just(start);
}
if (count < 0) {
throw new IllegalArgumentException("count >= 0 required but it was " + count);
}
return new IxRange(start, count);
}
/**
* Prevents the downstream from calling remove() and throws
* an UnsupportedOperationException instead.
* @return the new Ix instance
* @see #readOnly(boolean)
* @since 1.0
*/
public final Ix<T> readOnly() {
return new IxReadOnly<T>(this, false);
}
/**
* Prevents the downstream from calling remove() by optionally
* ignoring it or throwing an UnsupportedOperationException.
* @param silent if true, remove() calls are ignored; if false,
* remove() calls throw an UnsupportedOperationException
* @return the new Ix instance
* @since 1.0
*/
public final Ix<T> readOnly(boolean silent) {
return new IxReadOnly<T>(this, silent);
}
/**
* Repeatedly calls the given callable indefinitely and
* emits the returned value.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param callable the callable to call
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> repeatCallable(Callable<T> callable) {
return new IxRepeatCallable<T>(nullCheck(callable, "callable is null"));
}
/**
* Repeats the given value indefinitely.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param value the value to emit whenever next() is called
* @return the new Ix instance
* @since 1.0
*/
public static <T> Ix<T> repeatValue(T value) {
return new IxRepeat<T>(value);
}
/**
* Repeats the given value at most count times.
* <p>
* A count of zero will yield an empty sequence, a count of one
* will yield a sequence with only one element and so forth.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param value the value to emit
* @param count the number of times to emit the value, non-negative
* @return the new Ix instance
* @throws IllegalArgumentException if count is negative
* @since 1.0
*/
public static <T> Ix<T> repeatValue(T value, long count) {
return new IxRepeatCount<T>(value, nonNegative(count, "count"));
}
/**
* Repeats the given value until the given predicate returns true.
* <p>
* A count of zero will yield an empty sequence, a count of one
* will yield a sequence with only one element and so forth.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param value the value to emit
* @param stopPredicate the predicate called before any emission; returning
* false keeps repeating the value, returning true terminates the sequence
* @return the new Ix instance
* @throws NullPointerException if stopPredicate is null
* @since 1.0
*/
public static <T> Ix<T> repeatValue(T value, IxBooleanSupplier stopPredicate) {
return repeatValue(value, Long.MAX_VALUE, stopPredicate);
}
/**
* Repeats the given value at most count times or until the given predicate returns true.
* <p>
* A count of zero will yield an empty sequence, a count of one
* will yield a sequence with only one element and so forth.
* <p>
* The result's iterator() doesn't support remove().
* @param <T> the value type
* @param value the value to emit
* @param count the number of times to emit the value, non-negative
* @param stopPredicate the predicate called before any emission; returning
* false keeps repeating the value, returning true terminates the sequence
* @return the new Ix instance
* @throws IllegalArgumentException if count is negative
* @throws NullPointerException if stopPredicate is null
* @since 1.0
*/
public static <T> Ix<T> repeatValue(T value, long count, IxBooleanSupplier stopPredicate) {
return new IxRepeatPredicate<T>(value, nonNegative(count, "count"), nullCheck(stopPredicate, "stopPredicate is null"));
}
/**
* Emits a sequence of substring of a string split by the given separator.
* <p>
* The result's iterator() doesn't support remove().
* @param string the string to split, not null
* @param by the separator to split along, not null
* @return the new Ix instance
* @throws NullPointerException if string or by is null
* @since 1.0
*/
public static Ix<String> split(String string, String by) {
return new IxSplit(nullCheck(string, "string is null"), nullCheck(by, "by is null"));
}
/**
* Combines the next element from each source Iterable via a zipper function.
* <p>
* If one of the source Iterables is sorter the sequence terminates eagerly.
* <p>
* The result's iterator() doesn't support remove().
*
* @param <T> the common element type of the sources
* @param <R> the result value type
* @param sources the array of Iterable sources, not null
* @param zipper the function that takes an array of values and returns a value
* to be emitted, one from each source, not null
* @return the new Ix instance
* @throws NullPointerException if sources or zipper is null
* @since 1.0
*/
public static <T, R> Ix<R> zip(Iterable<? extends T>[] sources, IxFunction<? super Object[], R> zipper) {
return new IxZipArray<T, R>(nullCheck(sources, "sources is null"), nullCheck(zipper, "zipper is null"));
}
/**
* Combines the next element from each source Iterable, provided as an Iterable itself,
* via a zipper function.
* <p>
* If one of the source Iterables is sorter the sequence terminates eagerly.
* <p>
* The result's iterator() doesn't support remove().
*
* @param <T> the common element type of the sources
* @param <R> the result value type
* @param sources the Iterable of Iterable sources, not null
* @param zipper the function that takes an array of values and returns a value
* to be emitted, one from each source, not null
* @return the new Ix instance
* @throws NullPointerException if sources or zipper is null
* @since 1.0
*/
public static <T, R> Ix<R> zip(Iterable<? extends Iterable<? extends T>> sources, IxFunction<? super Object[], R> zipper) {
return new IxZipIterable<T, R>(nullCheck(sources, "sources is null"), nullCheck(zipper, "zipper is null"));
}
/**
* Combines the next element from each source Iterable via a zipper function.
* <p>
* If one of the source Iterables is sorter the sequence terminates eagerly.
* <p>
* The result's iterator() doesn't support remove().
*
* @param <T1> the first source's element type
* @param <T2> the second source's element type
* @param <R> the result value type
* @param source1 the first source Iterable
* @param source2 the second source Iterable
* @param zipper the function that takes one from each source, not null
* @return the new Ix instance
* @throws NullPointerException if any of the sources or zipper is null
* @since 1.0
*/
public static <T1, T2, R> Ix<R> zip(
Iterable<T1> source1, Iterable<T2> source2,
IxFunction2<? super T1, ? super T2, ? extends R> zipper) {
return new IxZip2<T1, T2, R>(nullCheck(source1, "source1 is null"),
nullCheck(source2, "source2 is null"), nullCheck(zipper, "zipper is null"));
}
/**
* Combines the next element from each source Iterable via a zipper function.
* <p>
* If one of the source Iterables is sorter the sequence terminates eagerly.
* <p>
* The result's iterator() doesn't support remove().
*
* @param <T1> the first source's element type
* @param <T2> the second source's element type
* @param <T3> the third source's element type
* @param <R> the result value type
* @param source1 the first source Iterable
* @param source2 the second source Iterable
* @param source3 the third source Iterable
* @param zipper the function that takes one from each source, not null
* @return the new Ix instance
* @throws NullPointerException if any of the sources or zipper is null
* @since 1.0
*/
public static <T1, T2, T3, R> Ix<R> zip(
Iterable<T1> source1, Iterable<T2> source2,
Iterable<T3> source3,
IxFunction3<? super T1, ? super T2, ? super T3, ? extends R> zipper) {
return new IxZip3<T1, T2, T3, R>(nullCheck(source1, "source1 is null"), nullCheck(source2, "source2 is null"),
nullCheck(source3, "source3 is null"), nullCheck(zipper, "zipper is null"));
}
/**
* Combines the next element from each source Iterable via a zipper function.
* <p>
* If one of the source Iterables is sorter the sequence terminates eagerly.
* <p>
* The result's iterator() doesn't support remove().
*
* @param <T1> the first source's element type
* @param <T2> the second source's element type
* @param <T3> the third source's element type
* @param <T4> the fourth source's element type
* @param <R> the result value type
* @param source1 the first source Iterable
* @param source2 the second source Iterable
* @param source3 the third source Iterable
* @param source4 the fourth source Iterable
* @param zipper the function that takes one from each source, not null
* @return the new Ix instance
* @throws NullPointerException if any of the sources or zipper is null
* @since 1.0
*/
public static <T1, T2, T3, T4, R> Ix<R> zip(
Iterable<T1> source1, Iterable<T2> source2,
Iterable<T3> source3, Iterable<T4> source4,
IxFunction4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipper) {
return new IxZip4<T1, T2, T3, T4, R>(nullCheck(source1, "source1 is null"),
nullCheck(source2, "source2 is null"), nullCheck(source3, "source3 is null"),
nullCheck(source4, "source4 is null"), nullCheck(zipper, "zipper is null"));
}
//---------------------------------------------------------------------------------------
// Instance operators
//---------------------------------------------------------------------------------------
/**
* Emits true if all elements of this sequence match a given predicate (including empty).
* <p>
* The result's iterator() doesn't support remove().
* @param predicate the predicate receiving each element
* @return the new Ix instance
* @throws NullPointerException if predicate is null
* @since 1.0
*/
public final Ix<Boolean> all(IxPredicate<? super T> predicate) {
return new IxAll<T>(this, nullCheck(predicate, "predicate is null"));
}
/**
* Emits true if any element of this sequence matches the given predicate,
* false otherwise (or for empty sequences).
* <p>
* The result's iterator() doesn't support remove().
* @param predicate the predicate receiving each element
* @return the new Ix instance
* @throws NullPointerException if predicate is null
* @since 1.0
*/
public final Ix<Boolean> any(IxPredicate<? super T> predicate) {
return new IxAny<T>(this, nullCheck(predicate, "predicate is null"));
}
/**
* Calls the given transformers with this and returns its value allowing
* fluent conversions to non-Ix types.
* @param <R> the result type
* @param transformer the function receiving this Ix instance and returns a value
* @return the value returned by the transformer function
* @throws NullPointerException if transformer is null
* @since 1.0
*/
public final <R> R as(IxFunction<? super Ix<T>, R> transformer) {
return transformer.apply(this);
}
/**
* Calculates the float-based average of this sequence of numbers.
* <p>The returned sequence is empty if this sequence is empty.
* <p>This operator force-casts this sequence which may lead
* to ClassCastException if any of this sequence's elements is not
* a subclass of Number.
* <p>
* The result's iterator() doesn't support remove().
* @return the new Ix instance
* @since 1.0
*/
@SuppressWarnings("unchecked")
public final Ix<Float> averageFloat() {
return new IxAverageFloat((Iterable<Number>)this);
}
/**
* Calculates the double-based average of this sequence of numbers.
* <p>The returned sequence is empty if this sequence is empty.
* <p>This operator force-casts this sequence which may lead
* to ClassCastException if any of this sequence's elements is not
* a subclass of Number.
* <p>
* The result's iterator() doesn't support remove().
* @return the new Ix instance
* @since 1.0
*/
@SuppressWarnings("unchecked")
public final Ix<Double> averageDouble() {
return new IxAverageDouble((Iterable<Number>)this);
}
/**
* Buffers the subsequent {@code size} elements into a sequence of
* non-overlapping Lists.
* <p>
* The result's iterator() doesn't support remove().
* @param size the number of elements to group together, positive
* @return the new Ix instance
* @throws IllegalArgumentException if size is non-positive
* @since 1.0
*/
public final Ix<List<T>> buffer(int size) {
return new IxBuffer<T>(this, positive(size, "size"));
}
/**
* Buffers the subsequent {@code size} elements into a sequence of
* potentially overlapping Lists.
* <p>
* The result's iterator() doesn't support remove().
* @param size the number of elements to group together, positive
* @param skip specifies how often to start a new list
* @return the new Ix instance
* @throws IllegalArgumentException if size or skip is non-positive
* @since 1.0
*/
public final Ix<List<T>> buffer(int size, int skip) {
if (size == skip) {
return buffer(size);
}
if (size < skip) {
return new IxBufferSkip<T>(this, positive(size, "size"), positive(skip, "skip"));
}
return new IxBufferOverlap<T>(this, positive(size, "size"), positive(skip, "skip"));
}
/**
* Buffer until an item is encountered for which the predicate returns true,
* triggering a new buffer.
* <p>Neither the previous nor the next buffer will contain the item that caused the
* split
* @param predicate the predicate called with each item and should return false
* to trigger a new buffer
* @return the new Ix instance
* @see #bufferUntil(IxPredicate)
* @see #bufferWhile(IxPredicate)
* @since 1.0
*/
public final Ix<List<T>> bufferSplit(IxPredicate<? super T> predicate) {
return new IxBufferSplit<T>(this, nullCheck(predicate, "predicate is null"));
}
/**
* Buffer until an item is encountered after which the predicate returns true
* to start a new buffer.
* <p>The item will be part of the previous buffer.
* @param predicate the predicate called with each item after the item
* has been added to the current buffer and should return true to start a new buffer
* @return the new Ix instance
* @see #bufferSplit(IxPredicate)
* @see #bufferWhile(IxPredicate)
* @since 1.0
*/
public final Ix<List<T>> bufferUntil(IxPredicate<? super T> predicate) {
return new IxBufferUntil<T>(this, nullCheck(predicate, "predicate is null"));
}
/**
* Buffer while an item is encountered before which the predicate returns false
* to start a new buffer.
* <p>The item will be part of the next buffer
* @param predicate the predicate called with each item after the item
* has been added to the current buffer and should return true to start a new buffer
* @return the new Ix instance
* @see #bufferSplit(IxPredicate)
* @see #bufferUntil(IxPredicate)
* @since 1.0
*/
public final Ix<List<T>> bufferWhile(IxPredicate<? super T> predicate) {
return new IxBufferWhile<T>(this, nullCheck(predicate, "predicate is null"));
}
/**
* Cast the elements to the specified class.
* <p>
* Note that this is a forced cast on this Ix instance and if
* not compatible, a ClassCastException might be thrown downstream.
* @param <R> the target type
* @param clazz the type token to capture the target type
* @return the new Ix instance
* @since 1.0
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public final <R> Ix<R> cast(Class<R> clazz) {
return (Ix)this;
}
/**
* Collect the elements into a collection via collector action and emit that collection
* as a single item.
* <p>
* The result's iterator() doesn't support remove().
* @param <C> the collection type
* @param initialFactory the function returning a collection for each iterator() call
* @param collector the action called with the collection and the current item
* @return the new Ix instance
* @throws NullPointerException if initialFactory or collector is null
* @since 1.0
*/
public final <C> Ix<C> collect(IxSupplier<C> initialFactory, IxConsumer2<C, T> collector) {
return new IxCollect<T, C>(this, nullCheck(initialFactory, "initialFactory is null"), nullCheck(collector, "collector"));
}
/**
* Collects the elements of this sequence into an Object array.
* <p>
* The result's iterator() doesn't support remove().
* @return the new Ix instance
* @since 1.0
*/
public final Ix<Object[]> collectToArray() {
return collect(ToListHelper.<T>initialFactory(), ToListHelper.<T>collector())
.map(ToListHelper.<T>toArray());
}
/**
* Collects the elements of this sequence into a List.
* <p>
* The result's iterator() doesn't support remove().
* @return the new Ix instance
* @since 1.0
*/
public final Ix<List<T>> collectToList() {
return collect(ToListHelper.<T>initialFactory(), ToListHelper.<T>collector());
}
/**
* Collects the elements of this sequence into a Map where the key is
* determined from each element via the keySelector function; duplicates are
* overwritten.
* <p>
* The result's iterator() doesn't support remove().
* @param <K> the key type
* @param keySelector the function that receives the current element and returns
* a key for it to be used as the Map key.
* @return the new Ix instance
* @throws NullPointerException if keySelector is null
* @since 1.0
*/
public final <K> Ix<Map<K, T>> collectToMap(IxFunction<? super T, ? extends K> keySelector) {
IxFunction<T, T> f = IdentityHelper.instance();
return this.collectToMap(keySelector, f);
}
/**
* Collects the elements of this sequence into a Map where the key is
* determined from each element via the keySelector function and
* the value is derived from the same element via the valueSelector function; duplicates are
* overwritten.
* <p>
* The result's iterator() doesn't support remove().
* @param <K> the key type
* @param <V> the value type
* @param keySelector the function that receives the current element and returns
* a key for it to be used as the Map key.
* @param valueSelector the function that receives the current element and returns
* a value for it to be used as the Map value
* @return the new Ix instance
* @throws NullPointerException if keySelector or valueSelector is null
* @since 1.0
*/
public final <K, V> Ix<Map<K, V>> collectToMap(IxFunction<? super T, ? extends K> keySelector, IxFunction<? super T, ? extends V> valueSelector) {
return new IxToMap<T, K, V>(this, nullCheck(keySelector, "keySelector is null"), nullCheck(valueSelector, "valueSelector is null"));
}
/**
* Collects the elements of this sequence into a multi-Map where the key is
* determined from each element via the keySelector function.
* <p>
* The result's iterator() doesn't support remove().
* @param <K> the key type
* @param keySelector the function that receives the current element and returns
* a key for it to be used as the Map key.
* @return the new Ix instance
* @throws NullPointerException if keySelector is null
* @since 1.0
*/
public final <K> Ix<Map<K, Collection<T>>> collectToMultimap(IxFunction<? super T, ? extends K> keySelector) {
IxFunction<T, T> f = IdentityHelper.instance();
return this.collectToMultimap(keySelector, f);
}
/**
* Collects the elements of this sequence into a multi-Map where the key is
* determined from each element via the keySelector function and
* the value is derived from the same element via the valueSelector function.
* <p>
* The result's iterator() doesn't support remove().
* @param <K> the key type
* @param <V> the value type
* @param keySelector the function that receives the current element and returns
* a key for it to be used as the Map key.
* @param valueSelector the function that receives the current element and returns
* a value for it to be used as the Map value
* @return the new Ix instance
* @throws NullPointerException if keySelector or valueSelector is null
* @since 1.0