-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathFunctions.java
648 lines (539 loc) · 21 KB
/
Functions.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
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex.internal.functions;
import java.util.*;
import java.util.concurrent.*;
import org.reactivestreams.Subscription;
import io.reactivex.*;
import io.reactivex.functions.*;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Timed;
/**
* Utility methods to convert the Function3..Function9 instances to Function of Object array.
*/
public final class Functions {
/** Utility class. */
private Functions() {
throw new IllegalStateException("No instances!");
}
@SuppressWarnings("unchecked")
public static <T1, T2, R> Function<Object[], R> toFunction(final BiFunction<? super T1, ? super T2, ? extends R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 2) {
throw new IllegalArgumentException("Array of size 2 expected but got " + a.length);
}
return ((BiFunction<Object, Object, R>)f).apply(a[0], a[1]);
}
};
}
public static <T1, T2, T3, R> Function<Object[], R> toFunction(final Function3<T1, T2, T3, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 3) {
throw new IllegalArgumentException("Array of size 3 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2]);
}
};
}
public static <T1, T2, T3, T4, R> Function<Object[], R> toFunction(final Function4<T1, T2, T3, T4, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 4) {
throw new IllegalArgumentException("Array of size 4 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3]);
}
};
}
public static <T1, T2, T3, T4, T5, R> Function<Object[], R> toFunction(final Function5<T1, T2, T3, T4, T5, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 5) {
throw new IllegalArgumentException("Array of size 5 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4]);
}
};
}
public static <T1, T2, T3, T4, T5, T6, R> Function<Object[], R> toFunction(
final Function6<T1, T2, T3, T4, T5, T6, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 6) {
throw new IllegalArgumentException("Array of size 6 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5]);
}
};
}
public static <T1, T2, T3, T4, T5, T6, T7, R> Function<Object[], R> toFunction(
final Function7<T1, T2, T3, T4, T5, T6, T7, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 7) {
throw new IllegalArgumentException("Array of size 7 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6]);
}
};
}
public static <T1, T2, T3, T4, T5, T6, T7, T8, R> Function<Object[], R> toFunction(
final Function8<T1, T2, T3, T4, T5, T6, T7, T8, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 8) {
throw new IllegalArgumentException("Array of size 8 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6], (T8)a[7]);
}
};
}
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Function<Object[], R> toFunction(
final Function9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> f) {
ObjectHelper.requireNonNull(f, "f is null");
return new Function<Object[], R>() {
@SuppressWarnings("unchecked")
@Override
public R apply(Object[] a) throws Exception {
if (a.length != 9) {
throw new IllegalArgumentException("Array of size 9 expected but got " + a.length);
}
return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6], (T8)a[7], (T9)a[8]);
}
};
}
/** A singleton identity function. */
static final Function<Object, Object> IDENTITY = new Function<Object, Object>() {
@Override
public Object apply(Object v) {
return v;
}
@Override
public String toString() {
return "IdentityFunction";
}
};
/**
* Returns an identity function that simply returns its argument.
* @param <T> the input and output value type
* @return the identity function
*/
@SuppressWarnings("unchecked")
public static <T> Function<T, T> identity() {
return (Function<T, T>)IDENTITY;
}
public static final Runnable EMPTY_RUNNABLE = new Runnable() {
@Override
public void run() { }
@Override
public String toString() {
return "EmptyRunnable";
}
};
public static final Action EMPTY_ACTION = new Action() {
@Override
public void run() { }
@Override
public String toString() {
return "EmptyAction";
}
};
static final Consumer<Object> EMPTY_CONSUMER = new Consumer<Object>() {
@Override
public void accept(Object v) { }
@Override
public String toString() {
return "EmptyConsumer";
}
};
/**
* Returns an empty consumer that does nothing.
* @param <T> the consumed value type, the value is ignored
* @return an empty consumer that does nothing.
*/
@SuppressWarnings("unchecked")
public static <T> Consumer<T> emptyConsumer() {
return (Consumer<T>)EMPTY_CONSUMER;
}
public static final Consumer<Throwable> ERROR_CONSUMER = new Consumer<Throwable>() {
@Override
public void accept(Throwable error) {
RxJavaPlugins.onError(error);
}
};
public static final LongConsumer EMPTY_LONG_CONSUMER = new LongConsumer() {
@Override
public void accept(long v) { }
};
static final Predicate<Object> ALWAYS_TRUE = new Predicate<Object>() {
@Override
public boolean test(Object o) {
return true;
}
};
static final Predicate<Object> ALWAYS_FALSE = new Predicate<Object>() {
@Override
public boolean test(Object o) {
return false;
}
};
static final Callable<Object> NULL_SUPPLIER = new Callable<Object>() {
@Override
public Object call() {
return null;
}
};
static final Comparator<Object> NATURAL_COMPARATOR = new Comparator<Object>() {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public int compare(Object a, Object b) {
return ((Comparable)a).compareTo(b);
}
};
@SuppressWarnings("unchecked")
public static <T> Predicate<T> alwaysTrue() {
return (Predicate<T>)ALWAYS_TRUE;
}
@SuppressWarnings("unchecked")
public static <T> Predicate<T> alwaysFalse() {
return (Predicate<T>)ALWAYS_FALSE;
}
@SuppressWarnings("unchecked")
public static <T> Callable<T> nullSupplier() {
return (Callable<T>)NULL_SUPPLIER;
}
/**
* Returns a natural order comparator which casts the parameters to Comparable.
* @param <T> the value type
* @return a natural order comparator which casts the parameters to Comparable
*/
@SuppressWarnings("unchecked")
public static <T> Comparator<T> naturalOrder() {
return (Comparator<T>)NATURAL_COMPARATOR;
}
static final class FutureAction implements Action {
final Future<?> future;
FutureAction(Future<?> future) {
this.future = future;
}
@Override
public void run() throws Exception {
future.get();
}
}
/**
* Wraps the blocking get call of the Future into an Action.
* @param future the future to call get() on, not null
* @return the new Action instance
*/
public static Action futureAction(Future<?> future) {
return new FutureAction(future);
}
static final class JustValue<T, U> implements Callable<U>, Function<T, U> {
final U value;
JustValue(U value) {
this.value = value;
}
@Override
public U call() throws Exception {
return value;
}
@Override
public U apply(T t) throws Exception {
return value;
}
}
/**
* Returns a Callable that returns the given value.
* @param <T> the value type
* @param value the value to return
* @return the new Callable instance
*/
public static <T> Callable<T> justCallable(T value) {
return new JustValue<Object, T>(value);
}
/**
* Returns a Function that ignores its parameter and returns the given value.
* @param <T> the function's input type
* @param <U> the value and return type of the function
* @param value the value to return
* @return the new Function instance
*/
public static <T, U> Function<T, U> justFunction(U value) {
return new JustValue<T, U>(value);
}
static final class CastToClass<T, U> implements Function<T, U> {
final Class<U> clazz;
CastToClass(Class<U> clazz) {
this.clazz = clazz;
}
@Override
public U apply(T t) throws Exception {
return clazz.cast(t);
}
}
/**
* Returns a function that cast the incoming values via a Class object.
* @param <T> the input value type
* @param <U> the output and target type
* @param target the target class
* @return the new Function instance
*/
public static <T, U> Function<T, U> castFunction(Class<U> target) {
return new CastToClass<T, U>(target);
}
static final class ArrayListCapacityCallable<T> implements Callable<List<T>> {
final int capacity;
ArrayListCapacityCallable(int capacity) {
this.capacity = capacity;
}
@Override
public List<T> call() throws Exception {
return new ArrayList<T>(capacity);
}
}
public static <T> Callable<List<T>> createArrayList(int capacity) {
return new ArrayListCapacityCallable<T>(capacity);
}
static final class EqualsPredicate<T> implements Predicate<T> {
final T value;
EqualsPredicate(T value) {
this.value = value;
}
@Override
public boolean test(T t) throws Exception {
return ObjectHelper.equals(t, value);
}
}
public static <T> Predicate<T> equalsWith(T value) {
return new EqualsPredicate<T>(value);
}
enum HashSetCallable implements Callable<Set<Object>> {
INSTANCE;
@Override
public Set<Object> call() throws Exception {
return new HashSet<Object>();
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Callable<Set<T>> createHashSet() {
return (Callable)HashSetCallable.INSTANCE;
}
static final class NotificationOnNext<T> implements Consumer<T> {
final Consumer<? super Notification<T>> onNotification;
NotificationOnNext(Consumer<? super Notification<T>> onNotification) {
this.onNotification = onNotification;
}
@Override
public void accept(T v) throws Exception {
onNotification.accept(Notification.createOnNext(v));
}
}
static final class NotificationOnError<T> implements Consumer<Throwable> {
final Consumer<? super Notification<T>> onNotification;
NotificationOnError(Consumer<? super Notification<T>> onNotification) {
this.onNotification = onNotification;
}
@Override
public void accept(Throwable v) throws Exception {
onNotification.accept(Notification.<T>createOnError(v));
}
}
static final class NotificationOnComplete<T> implements Action {
final Consumer<? super Notification<T>> onNotification;
NotificationOnComplete(Consumer<? super Notification<T>> onNotification) {
this.onNotification = onNotification;
}
@Override
public void run() throws Exception {
onNotification.accept(Notification.<T>createOnComplete());
}
}
public static <T> Consumer<T> notificationOnNext(Consumer<? super Notification<T>> onNotification) {
return new NotificationOnNext<T>(onNotification);
}
public static <T> Consumer<Throwable> notificationOnError(Consumer<? super Notification<T>> onNotification) {
return new NotificationOnError<T>(onNotification);
}
public static <T> Action notificationOnComplete(Consumer<? super Notification<T>> onNotification) {
return new NotificationOnComplete<T>(onNotification);
}
static final class ActionConsumer<T> implements Consumer<T> {
final Action action;
ActionConsumer(Action action) {
this.action = action;
}
@Override
public void accept(T t) throws Exception {
action.run();
}
}
public static <T> Consumer<T> actionConsumer(Action action) {
return new ActionConsumer<T>(action);
}
static final class ClassFilter<T, U> implements Predicate<T> {
final Class<U> clazz;
ClassFilter(Class<U> clazz) {
this.clazz = clazz;
}
@Override
public boolean test(T t) throws Exception {
return clazz.isInstance(t);
}
}
public static <T, U> Predicate<T> isInstanceOf(Class<U> clazz) {
return new ClassFilter<T, U>(clazz);
}
static final class BooleanSupplierPredicateReverse<T> implements Predicate<T> {
final BooleanSupplier supplier;
BooleanSupplierPredicateReverse(BooleanSupplier supplier) {
this.supplier = supplier;
}
@Override
public boolean test(T t) throws Exception {
return !supplier.getAsBoolean();
}
}
public static <T> Predicate<T> predicateReverseFor(BooleanSupplier supplier) {
return new BooleanSupplierPredicateReverse<T>(supplier);
}
static final class TimestampFunction<T> implements Function<T, Timed<T>> {
final TimeUnit unit;
final Scheduler scheduler;
TimestampFunction(TimeUnit unit, Scheduler scheduler) {
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public Timed<T> apply(T t) throws Exception {
return new Timed<T>(t, scheduler.now(unit), unit);
}
}
public static <T> Function<T, Timed<T>> timestampWith(TimeUnit unit, Scheduler scheduler) {
return new TimestampFunction<T>(unit, scheduler);
}
static final class ToMapKeySelector<K, T> implements BiConsumer<Map<K, T>, T> {
private final Function<? super T, ? extends K> keySelector;
ToMapKeySelector(Function<? super T, ? extends K> keySelector) {
this.keySelector = keySelector;
}
@Override
public void accept(Map<K, T> m, T t) throws Exception {
K key = keySelector.apply(t);
m.put(key, t);
}
}
public static <T, K> BiConsumer<Map<K, T>, T> toMapKeySelector(final Function<? super T, ? extends K> keySelector) {
return new ToMapKeySelector<K, T>(keySelector);
}
static final class ToMapKeyValueSelector<K, V, T> implements BiConsumer<Map<K, V>, T> {
private final Function<? super T, ? extends V> valueSelector;
private final Function<? super T, ? extends K> keySelector;
ToMapKeyValueSelector(Function<? super T, ? extends V> valueSelector,
Function<? super T, ? extends K> keySelector) {
this.valueSelector = valueSelector;
this.keySelector = keySelector;
}
@Override
public void accept(Map<K, V> m, T t) throws Exception {
K key = keySelector.apply(t);
V value = valueSelector.apply(t);
m.put(key, value);
}
}
public static <T, K, V> BiConsumer<Map<K, V>, T> toMapKeyValueSelector(final Function<? super T, ? extends K> keySelector, final Function<? super T, ? extends V> valueSelector) {
return new ToMapKeyValueSelector<K, V, T>(valueSelector, keySelector);
}
static final class ToMultimapKeyValueSelector<K, V, T> implements BiConsumer<Map<K, Collection<V>>, T> {
private final Function<? super K, ? extends Collection<? super V>> collectionFactory;
private final Function<? super T, ? extends V> valueSelector;
private final Function<? super T, ? extends K> keySelector;
ToMultimapKeyValueSelector(Function<? super K, ? extends Collection<? super V>> collectionFactory,
Function<? super T, ? extends V> valueSelector, Function<? super T, ? extends K> keySelector) {
this.collectionFactory = collectionFactory;
this.valueSelector = valueSelector;
this.keySelector = keySelector;
}
@SuppressWarnings("unchecked")
@Override
public void accept(Map<K, Collection<V>> m, T t) throws Exception {
K key = keySelector.apply(t);
Collection<V> coll = m.get(key);
if (coll == null) {
coll = (Collection<V>)collectionFactory.apply(key);
m.put(key, coll);
}
V value = valueSelector.apply(t);
coll.add(value);
}
}
public static <T, K, V> BiConsumer<Map<K, Collection<V>>, T> toMultimapKeyValueSelector(
final Function<? super T, ? extends K> keySelector, final Function<? super T, ? extends V> valueSelector,
final Function<? super K, ? extends Collection<? super V>> collectionFactory) {
return new ToMultimapKeyValueSelector<K, V, T>(collectionFactory, valueSelector, keySelector);
}
enum NaturalComparator implements Comparator<Object> {
INSTANCE;
@SuppressWarnings("unchecked")
@Override
public int compare(Object o1, Object o2) {
return ((Comparable<Object>)o1).compareTo(o2);
}
}
@SuppressWarnings("unchecked")
public static <T> Comparator<T> naturalComparator() {
return (Comparator<T>)NaturalComparator.INSTANCE;
}
static final class ListSorter<T> implements Function<List<T>, List<T>> {
private final Comparator<? super T> comparator;
ListSorter(Comparator<? super T> comparator) {
this.comparator = comparator;
}
@Override
public List<T> apply(List<T> v) {
Collections.sort(v, comparator);
return v;
}
}
public static <T> Function<List<T>, List<T>> listSorter(final Comparator<? super T> comparator) {
return new ListSorter<T>(comparator);
}
public static final Consumer<Subscription> REQUEST_MAX = new Consumer<Subscription>() {
@Override
public void accept(Subscription t) throws Exception {
t.request(Long.MAX_VALUE);
}
};
}