Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/functions/Functions.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.functions;
15
16 import java.util.*;
17 import java.util.concurrent.*;
18
19 import org.reactivestreams.Subscription;
20
21 import io.reactivex.rxjava3.annotations.NonNull;
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.schedulers.Timed;
27
28 /**
29 * Utility methods to convert the BiFunction, Function3..Function9 instances to Function of Object array.
30 */
31 public final class Functions {
32
33 /** Utility class. */
34 private Functions() {
35 throw new IllegalStateException("No instances!");
36 }
37
38 @NonNull
39 public static <T1, T2, R> Function<Object[], R> toFunction(@NonNull BiFunction<? super T1, ? super T2, ? extends R> f) {
40 return new Array2Func<>(f);
41 }
42
43 @NonNull
44 public static <T1, T2, T3, R> Function<Object[], R> toFunction(@NonNull Function3<T1, T2, T3, R> f) {
45 return new Array3Func<>(f);
46 }
47
48 @NonNull
49 public static <T1, T2, T3, T4, R> Function<Object[], R> toFunction(@NonNull Function4<T1, T2, T3, T4, R> f) {
50 return new Array4Func<>(f);
51 }
52
53 @NonNull
54 public static <T1, T2, T3, T4, T5, R> Function<Object[], R> toFunction(@NonNull Function5<T1, T2, T3, T4, T5, R> f) {
55 return new Array5Func<>(f);
56 }
57
58 @NonNull
59 public static <T1, T2, T3, T4, T5, T6, R> Function<Object[], R> toFunction(
60 @NonNull Function6<T1, T2, T3, T4, T5, T6, R> f) {
61 return new Array6Func<>(f);
62 }
63
64 @NonNull
65 public static <T1, T2, T3, T4, T5, T6, T7, R> Function<Object[], R> toFunction(
66 @NonNull Function7<T1, T2, T3, T4, T5, T6, T7, R> f) {
67 return new Array7Func<>(f);
68 }
69
70 @NonNull
71 public static <T1, T2, T3, T4, T5, T6, T7, T8, R> Function<Object[], R> toFunction(
72 @NonNull Function8<T1, T2, T3, T4, T5, T6, T7, T8, R> f) {
73 return new Array8Func<>(f);
74 }
75
76 @NonNull
77 public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Function<Object[], R> toFunction(
78 @NonNull Function9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> f) {
79 return new Array9Func<>(f);
80 }
81
82 /** A singleton identity function. */
83 static final Function<Object, Object> IDENTITY = new Identity();
84
85 /**
86 * Returns an identity function that simply returns its argument.
87 * @param <T> the input and output value type
88 * @return the identity function
89 */
90 @SuppressWarnings("unchecked")
91 @NonNull
92 public static <T> Function<T, T> identity() {
93 return (Function<T, T>)IDENTITY;
94 }
95
96 public static final Runnable EMPTY_RUNNABLE = new EmptyRunnable();
97
98 public static final Action EMPTY_ACTION = new EmptyAction();
99
100 static final Consumer<Object> EMPTY_CONSUMER = new EmptyConsumer();
101
102 /**
103 * Returns an empty consumer that does nothing.
104 * @param <T> the consumed value type, the value is ignored
105 * @return an empty consumer that does nothing.
106 */
107 @SuppressWarnings("unchecked")
108 public static <T> Consumer<T> emptyConsumer() {
109 return (Consumer<T>)EMPTY_CONSUMER;
110 }
111
112 public static final Consumer<Throwable> ERROR_CONSUMER = new ErrorConsumer();
113
114 /**
115 * Wraps the consumed Throwable into an OnErrorNotImplementedException and
116 * signals it to the plugin error handler.
117 */
118 public static final Consumer<Throwable> ON_ERROR_MISSING = new OnErrorMissingConsumer();
119
120 public static final LongConsumer EMPTY_LONG_CONSUMER = new EmptyLongConsumer();
121
122 static final Predicate<Object> ALWAYS_TRUE = new TruePredicate();
123
124 static final Predicate<Object> ALWAYS_FALSE = new FalsePredicate();
125
126 static final Supplier<Object> NULL_SUPPLIER = new NullProvider();
127
128 @SuppressWarnings("unchecked")
129 @NonNull
130 public static <T> Predicate<T> alwaysTrue() {
131 return (Predicate<T>)ALWAYS_TRUE;
132 }
133
134 @SuppressWarnings("unchecked")
135 @NonNull
136 public static <T> Predicate<T> alwaysFalse() {
137 return (Predicate<T>)ALWAYS_FALSE;
138 }
139
140 @SuppressWarnings("unchecked")
141 @NonNull
142 public static <T> Supplier<T> nullSupplier() {
143 return (Supplier<T>)NULL_SUPPLIER;
144 }
145
146 static final class FutureAction implements Action {
147 final Future<?> future;
148
149 FutureAction(Future<?> future) {
150 this.future = future;
151 }
152
153 @Override
154 public void run() throws Exception {
155 future.get();
156 }
157 }
158
159 /**
160 * Wraps the blocking get call of the Future into an Action.
161 * @param future the future to call get() on, not null
162 * @return the new Action instance
163 */
164 @NonNull
165 public static Action futureAction(@NonNull Future<?> future) {
166 return new FutureAction(future);
167 }
168
169 static final class JustValue<T, U> implements Callable<U>, Supplier<U>, Function<T, U> {
170 final U value;
171
172 JustValue(U value) {
173 this.value = value;
174 }
175
176 @Override
177 public U call() {
178 return value;
179 }
180
181 @Override
182 public U apply(T t) {
183 return value;
184 }
185
186 @Override
187 public U get() {
188 return value;
189 }
190 }
191
192 /**
193 * Returns a Callable that returns the given value.
194 * @param <T> the value type
195 * @param value the value to return
196 * @return the new Callable instance
197 */
198 @NonNull
199 public static <T> Callable<T> justCallable(@NonNull T value) {
200 return new JustValue<>(value);
201 }
202
203 /**
204 * Returns a Supplier that returns the given value.
205 * @param <T> the value type
206 * @param value the value to return
207 * @return the new Callable instance
208 */
209 @NonNull
210 public static <T> Supplier<T> justSupplier(@NonNull T value) {
211 return new JustValue<>(value);
212 }
213
214 /**
215 * Returns a Function that ignores its parameter and returns the given value.
216 * @param <T> the function's input type
217 * @param <U> the value and return type of the function
218 * @param value the value to return
219 * @return the new Function instance
220 */
221 @NonNull
222 public static <T, U> Function<T, U> justFunction(@NonNull U value) {
223 return new JustValue<>(value);
224 }
225
226 static final class CastToClass<T, U> implements Function<T, U> {
227 final Class<U> clazz;
228
229 CastToClass(Class<U> clazz) {
230 this.clazz = clazz;
231 }
232
233 @Override
234 public U apply(T t) {
235 return clazz.cast(t);
236 }
237 }
238
239 /**
240 * Returns a function that cast the incoming values via a Class object.
241 * @param <T> the input value type
242 * @param <U> the output and target type
243 * @param target the target class
244 * @return the new Function instance
245 */
246 @NonNull
247 public static <T, U> Function<T, U> castFunction(@NonNull Class<U> target) {
248 return new CastToClass<>(target);
249 }
250
251 static final class ArrayListCapacityCallable<T> implements Supplier<List<T>> {
252 final int capacity;
253
254 ArrayListCapacityCallable(int capacity) {
255 this.capacity = capacity;
256 }
257
258 @Override
259 public List<T> get() {
260 return new ArrayList<>(capacity);
261 }
262 }
263
264 public static <T> Supplier<List<T>> createArrayList(int capacity) {
265 return new ArrayListCapacityCallable<>(capacity);
266 }
267
268 static final class EqualsPredicate<T> implements Predicate<T> {
269 final T value;
270
271 EqualsPredicate(T value) {
272 this.value = value;
273 }
274
275 @Override
276 public boolean test(T t) {
277 return Objects.equals(t, value);
278 }
279 }
280
281 public static <T> Predicate<T> equalsWith(T value) {
282 return new EqualsPredicate<>(value);
283 }
284
285 enum HashSetSupplier implements Supplier<Set<Object>> {
286 INSTANCE;
287 @Override
288 public Set<Object> get() {
289 return new HashSet<>();
290 }
291 }
292
293 @SuppressWarnings({ "rawtypes", "unchecked" })
294 public static <T> Supplier<Set<T>> createHashSet() {
295 return (Supplier)HashSetSupplier.INSTANCE;
296 }
297
298 static final class NotificationOnNext<T> implements Consumer<T> {
299 final Consumer<? super Notification<T>> onNotification;
300
301 NotificationOnNext(Consumer<? super Notification<T>> onNotification) {
302 this.onNotification = onNotification;
303 }
304
305 @Override
306 public void accept(T v) throws Throwable {
307 onNotification.accept(Notification.createOnNext(v));
308 }
309 }
310
311 static final class NotificationOnError<T> implements Consumer<Throwable> {
312 final Consumer<? super Notification<T>> onNotification;
313
314 NotificationOnError(Consumer<? super Notification<T>> onNotification) {
315 this.onNotification = onNotification;
316 }
317
318 @Override
319 public void accept(Throwable v) throws Throwable {
320 onNotification.accept(Notification.createOnError(v));
321 }
322 }
323
324 static final class NotificationOnComplete<T> implements Action {
325 final Consumer<? super Notification<T>> onNotification;
326
327 NotificationOnComplete(Consumer<? super Notification<T>> onNotification) {
328 this.onNotification = onNotification;
329 }
330
331 @Override
332 public void run() throws Throwable {
333 onNotification.accept(Notification.createOnComplete());
334 }
335 }
336
337 public static <T> Consumer<T> notificationOnNext(Consumer<? super Notification<T>> onNotification) {
338 return new NotificationOnNext<>(onNotification);
339 }
340
341 public static <T> Consumer<Throwable> notificationOnError(Consumer<? super Notification<T>> onNotification) {
342 return new NotificationOnError<>(onNotification);
343 }
344
345 public static <T> Action notificationOnComplete(Consumer<? super Notification<T>> onNotification) {
346 return new NotificationOnComplete<>(onNotification);
347 }
348
349 static final class ActionConsumer<T> implements Consumer<T> {
350 final Action action;
351
352 ActionConsumer(Action action) {
353 this.action = action;
354 }
355
356 @Override
357 public void accept(T t) throws Throwable {
358 action.run();
359 }
360 }
361
362 public static <T> Consumer<T> actionConsumer(Action action) {
363 return new ActionConsumer<>(action);
364 }
365
366 static final class ClassFilter<T, U> implements Predicate<T> {
367 final Class<U> clazz;
368
369 ClassFilter(Class<U> clazz) {
370 this.clazz = clazz;
371 }
372
373 @Override
374 public boolean test(T t) {
375 return clazz.isInstance(t);
376 }
377 }
378
379 public static <T, U> Predicate<T> isInstanceOf(Class<U> clazz) {
380 return new ClassFilter<>(clazz);
381 }
382
383 static final class BooleanSupplierPredicateReverse<T> implements Predicate<T> {
384 final BooleanSupplier supplier;
385
386 BooleanSupplierPredicateReverse(BooleanSupplier supplier) {
387 this.supplier = supplier;
388 }
389
390 @Override
391 public boolean test(T t) throws Throwable {
392 return !supplier.getAsBoolean();
393 }
394 }
395
396 public static <T> Predicate<T> predicateReverseFor(BooleanSupplier supplier) {
397 return new BooleanSupplierPredicateReverse<>(supplier);
398 }
399
400 static final class TimestampFunction<T> implements Function<T, Timed<T>> {
401 final TimeUnit unit;
402
403 final Scheduler scheduler;
404
405 TimestampFunction(TimeUnit unit, Scheduler scheduler) {
406 this.unit = unit;
407 this.scheduler = scheduler;
408 }
409
410 @Override
411 public Timed<T> apply(T t) {
412 return new Timed<>(t, scheduler.now(unit), unit);
413 }
414 }
415
416 public static <T> Function<T, Timed<T>> timestampWith(TimeUnit unit, Scheduler scheduler) {
417 return new TimestampFunction<>(unit, scheduler);
418 }
419
420 static final class ToMapKeySelector<K, T> implements BiConsumer<Map<K, T>, T> {
421 private final Function<? super T, ? extends K> keySelector;
422
423 ToMapKeySelector(Function<? super T, ? extends K> keySelector) {
424 this.keySelector = keySelector;
425 }
426
427 @Override
428 public void accept(Map<K, T> m, T t) throws Throwable {
429 K key = keySelector.apply(t);
430 m.put(key, t);
431 }
432 }
433
434 public static <T, K> BiConsumer<Map<K, T>, T> toMapKeySelector(final Function<? super T, ? extends K> keySelector) {
435 return new ToMapKeySelector<>(keySelector);
436 }
437
438 static final class ToMapKeyValueSelector<K, V, T> implements BiConsumer<Map<K, V>, T> {
439 private final Function<? super T, ? extends V> valueSelector;
440 private final Function<? super T, ? extends K> keySelector;
441
442 ToMapKeyValueSelector(Function<? super T, ? extends V> valueSelector,
443 Function<? super T, ? extends K> keySelector) {
444 this.valueSelector = valueSelector;
445 this.keySelector = keySelector;
446 }
447
448 @Override
449 public void accept(Map<K, V> m, T t) throws Throwable {
450 K key = keySelector.apply(t);
451 V value = valueSelector.apply(t);
452 m.put(key, value);
453 }
454 }
455
456 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) {
457 return new ToMapKeyValueSelector<>(valueSelector, keySelector);
458 }
459
460 static final class ToMultimapKeyValueSelector<K, V, T> implements BiConsumer<Map<K, Collection<V>>, T> {
461 private final Function<? super K, ? extends Collection<? super V>> collectionFactory;
462 private final Function<? super T, ? extends V> valueSelector;
463 private final Function<? super T, ? extends K> keySelector;
464
465 ToMultimapKeyValueSelector(Function<? super K, ? extends Collection<? super V>> collectionFactory,
466 Function<? super T, ? extends V> valueSelector, Function<? super T, ? extends K> keySelector) {
467 this.collectionFactory = collectionFactory;
468 this.valueSelector = valueSelector;
469 this.keySelector = keySelector;
470 }
471
472 @SuppressWarnings("unchecked")
473 @Override
474 public void accept(Map<K, Collection<V>> m, T t) throws Throwable {
475 K key = keySelector.apply(t);
476
477 Collection<V> coll = m.get(key);
478 if (coll == null) {
479 coll = (Collection<V>)collectionFactory.apply(key);
480 m.put(key, coll);
481 }
482
483 V value = valueSelector.apply(t);
484
485 coll.add(value);
486 }
487 }
488
489 public static <T, K, V> BiConsumer<Map<K, Collection<V>>, T> toMultimapKeyValueSelector(
490 final Function<? super T, ? extends K> keySelector, final Function<? super T, ? extends V> valueSelector,
491 final Function<? super K, ? extends Collection<? super V>> collectionFactory) {
492 return new ToMultimapKeyValueSelector<>(collectionFactory, valueSelector, keySelector);
493 }
494
495 enum NaturalComparator implements Comparator<Object> {
496 INSTANCE;
497
498 @SuppressWarnings("unchecked")
499 @Override
500 public int compare(Object o1, Object o2) {
501 return ((Comparable<Object>)o1).compareTo(o2);
502 }
503 }
504
505 @SuppressWarnings("unchecked")
506 public static <T> Comparator<T> naturalComparator() {
507 return (Comparator<T>)NaturalComparator.INSTANCE;
508 }
509
510 static final class ListSorter<T> implements Function<List<T>, List<T>> {
511 final Comparator<? super T> comparator;
512
513 ListSorter(Comparator<? super T> comparator) {
514 this.comparator = comparator;
515 }
516
517 @Override
518 public List<T> apply(List<T> v) {
519 Collections.sort(v, comparator);
520 return v;
521 }
522 }
523
524 public static <T> Function<List<T>, List<T>> listSorter(final Comparator<? super T> comparator) {
525 return new ListSorter<>(comparator);
526 }
527
528 public static final Consumer<Subscription> REQUEST_MAX = new MaxRequestSubscription();
529
530 static final class Array2Func<T1, T2, R> implements Function<Object[], R> {
531 final BiFunction<? super T1, ? super T2, ? extends R> f;
532
533 Array2Func(BiFunction<? super T1, ? super T2, ? extends R> f) {
534 this.f = f;
535 }
536
537 @SuppressWarnings("unchecked")
538 @Override
539 public R apply(Object[] a) throws Throwable {
540 if (a.length != 2) {
541 throw new IllegalArgumentException("Array of size 2 expected but got " + a.length);
542 }
543 return f.apply((T1)a[0], (T2)a[1]);
544 }
545 }
546
547 static final class Array3Func<T1, T2, T3, R> implements Function<Object[], R> {
548 final Function3<T1, T2, T3, R> f;
549
550 Array3Func(Function3<T1, T2, T3, R> f) {
551 this.f = f;
552 }
553
554 @SuppressWarnings("unchecked")
555 @Override
556 public R apply(Object[] a) throws Throwable {
557 if (a.length != 3) {
558 throw new IllegalArgumentException("Array of size 3 expected but got " + a.length);
559 }
560 return f.apply((T1)a[0], (T2)a[1], (T3)a[2]);
561 }
562 }
563
564 static final class Array4Func<T1, T2, T3, T4, R> implements Function<Object[], R> {
565 final Function4<T1, T2, T3, T4, R> f;
566
567 Array4Func(Function4<T1, T2, T3, T4, R> f) {
568 this.f = f;
569 }
570
571 @SuppressWarnings("unchecked")
572 @Override
573 public R apply(Object[] a) throws Throwable {
574 if (a.length != 4) {
575 throw new IllegalArgumentException("Array of size 4 expected but got " + a.length);
576 }
577 return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3]);
578 }
579 }
580
581 static final class Array5Func<T1, T2, T3, T4, T5, R> implements Function<Object[], R> {
582 private final Function5<T1, T2, T3, T4, T5, R> f;
583
584 Array5Func(Function5<T1, T2, T3, T4, T5, R> f) {
585 this.f = f;
586 }
587
588 @SuppressWarnings("unchecked")
589 @Override
590 public R apply(Object[] a) throws Throwable {
591 if (a.length != 5) {
592 throw new IllegalArgumentException("Array of size 5 expected but got " + a.length);
593 }
594 return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4]);
595 }
596 }
597
598 static final class Array6Func<T1, T2, T3, T4, T5, T6, R> implements Function<Object[], R> {
599 final Function6<T1, T2, T3, T4, T5, T6, R> f;
600
601 Array6Func(Function6<T1, T2, T3, T4, T5, T6, R> f) {
602 this.f = f;
603 }
604
605 @SuppressWarnings("unchecked")
606 @Override
607 public R apply(Object[] a) throws Throwable {
608 if (a.length != 6) {
609 throw new IllegalArgumentException("Array of size 6 expected but got " + a.length);
610 }
611 return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5]);
612 }
613 }
614
615 static final class Array7Func<T1, T2, T3, T4, T5, T6, T7, R> implements Function<Object[], R> {
616 final Function7<T1, T2, T3, T4, T5, T6, T7, R> f;
617
618 Array7Func(Function7<T1, T2, T3, T4, T5, T6, T7, R> f) {
619 this.f = f;
620 }
621
622 @SuppressWarnings("unchecked")
623 @Override
624 public R apply(Object[] a) throws Throwable {
625 if (a.length != 7) {
626 throw new IllegalArgumentException("Array of size 7 expected but got " + a.length);
627 }
628 return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6]);
629 }
630 }
631
632 static final class Array8Func<T1, T2, T3, T4, T5, T6, T7, T8, R> implements Function<Object[], R> {
633 final Function8<T1, T2, T3, T4, T5, T6, T7, T8, R> f;
634
635 Array8Func(Function8<T1, T2, T3, T4, T5, T6, T7, T8, R> f) {
636 this.f = f;
637 }
638
639 @SuppressWarnings("unchecked")
640 @Override
641 public R apply(Object[] a) throws Throwable {
642 if (a.length != 8) {
643 throw new IllegalArgumentException("Array of size 8 expected but got " + a.length);
644 }
645 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]);
646 }
647 }
648
649 static final class Array9Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> implements Function<Object[], R> {
650 final Function9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> f;
651
652 Array9Func(Function9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> f) {
653 this.f = f;
654 }
655
656 @SuppressWarnings("unchecked")
657 @Override
658 public R apply(Object[] a) throws Throwable {
659 if (a.length != 9) {
660 throw new IllegalArgumentException("Array of size 9 expected but got " + a.length);
661 }
662 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]);
663 }
664 }
665
666 static final class Identity implements Function<Object, Object> {
667 @Override
668 public Object apply(Object v) {
669 return v;
670 }
671
672 @Override
673 public String toString() {
674 return "IdentityFunction";
675 }
676 }
677
678 static final class EmptyRunnable implements Runnable {
679 @Override
680 public void run() { }
681
682 @Override
683 public String toString() {
684 return "EmptyRunnable";
685 }
686 }
687
688 static final class EmptyAction implements Action {
689 @Override
690 public void run() { }
691
692 @Override
693 public String toString() {
694 return "EmptyAction";
695 }
696 }
697
698 static final class EmptyConsumer implements Consumer<Object> {
699 @Override
700 public void accept(Object v) { }
701
702 @Override
703 public String toString() {
704 return "EmptyConsumer";
705 }
706 }
707
708 static final class ErrorConsumer implements Consumer<Throwable> {
709 @Override
710 public void accept(Throwable error) {
711 RxJavaPlugins.onError(error);
712 }
713 }
714
715 static final class OnErrorMissingConsumer implements Consumer<Throwable> {
716 @Override
717 public void accept(Throwable error) {
718 RxJavaPlugins.onError(new OnErrorNotImplementedException(error));
719 }
720 }
721
722 static final class EmptyLongConsumer implements LongConsumer {
723 @Override
724 public void accept(long v) { }
725 }
726
727 static final class TruePredicate implements Predicate<Object> {
728 @Override
729 public boolean test(Object o) {
730 return true;
731 }
732 }
733
734 static final class FalsePredicate implements Predicate<Object> {
735 @Override
736 public boolean test(Object o) {
737 return false;
738 }
739 }
740
741 static final class NullProvider implements Supplier<Object> {
742 @Override
743 public Object get() {
744 return null;
745 }
746 }
747
748 static final class MaxRequestSubscription implements Consumer<Subscription> {
749 @Override
750 public void accept(Subscription t) {
751 t.request(Long.MAX_VALUE);
752 }
753 }
754
755 @SuppressWarnings("unchecked")
756 public static <T> Consumer<T> boundedConsumer(int bufferSize) {
757 return (Consumer<T>) new BoundedConsumer(bufferSize);
758 }
759
760 public static class BoundedConsumer implements Consumer<Subscription> {
761
762 final int bufferSize;
763
764 BoundedConsumer(int bufferSize) {
765 this.bufferSize = bufferSize;
766 }
767
768 @Override
769 public void accept(Subscription s) {
770 s.request(bufferSize);
771 }
772 }
773 }