Debian Codegraph @

src/main/java/io/reactivex/rxjava3/plugins/RxJavaPlugins.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.plugins;
15
16 import java.lang.Thread.UncaughtExceptionHandler;
17 import java.util.Objects;
18 import java.util.concurrent.*;
19
20 import org.reactivestreams.Subscriber;
21
22 import io.reactivex.rxjava3.annotations.*;
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.internal.schedulers.*;
28 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
29 import io.reactivex.rxjava3.observables.ConnectableObservable;
30 import io.reactivex.rxjava3.parallel.ParallelFlowable;
31 import io.reactivex.rxjava3.schedulers.Schedulers;
32 /**
33 * Utility class to inject handlers to certain standard RxJava operations.
34 */
35 public final class RxJavaPlugins {
36 @Nullable
37 static volatile Consumer<? super Throwable> errorHandler;
38
39 @Nullable
40 static volatile Function<? super Runnable, ? extends Runnable> onScheduleHandler;
41
42 @Nullable
43 static volatile Function<? super Supplier<Scheduler>, ? extends Scheduler> onInitComputationHandler;
44
45 @Nullable
46 static volatile Function<? super Supplier<Scheduler>, ? extends Scheduler> onInitSingleHandler;
47
48 @Nullable
49 static volatile Function<? super Supplier<Scheduler>, ? extends Scheduler> onInitIoHandler;
50
51 @Nullable
52 static volatile Function<? super Supplier<Scheduler>, ? extends Scheduler> onInitNewThreadHandler;
53
54 @Nullable
55 static volatile Function<? super Scheduler, ? extends Scheduler> onComputationHandler;
56
57 @Nullable
58 static volatile Function<? super Scheduler, ? extends Scheduler> onSingleHandler;
59
60 @Nullable
61 static volatile Function<? super Scheduler, ? extends Scheduler> onIoHandler;
62
63 @Nullable
64 static volatile Function<? super Scheduler, ? extends Scheduler> onNewThreadHandler;
65
66 @SuppressWarnings("rawtypes")
67 @Nullable
68 static volatile Function<? super Flowable, ? extends Flowable> onFlowableAssembly;
69
70 @SuppressWarnings("rawtypes")
71 @Nullable
72 static volatile Function<? super ConnectableFlowable, ? extends ConnectableFlowable> onConnectableFlowableAssembly;
73
74 @SuppressWarnings("rawtypes")
75 @Nullable
76 static volatile Function<? super Observable, ? extends Observable> onObservableAssembly;
77
78 @SuppressWarnings("rawtypes")
79 @Nullable
80 static volatile Function<? super ConnectableObservable, ? extends ConnectableObservable> onConnectableObservableAssembly;
81
82 @SuppressWarnings("rawtypes")
83 @Nullable
84 static volatile Function<? super Maybe, ? extends Maybe> onMaybeAssembly;
85
86 @SuppressWarnings("rawtypes")
87 @Nullable
88 static volatile Function<? super Single, ? extends Single> onSingleAssembly;
89
90 @Nullable
91 static volatile Function<? super Completable, ? extends Completable> onCompletableAssembly;
92
93 @SuppressWarnings("rawtypes")
94 @Nullable
95 static volatile Function<? super ParallelFlowable, ? extends ParallelFlowable> onParallelAssembly;
96
97 @SuppressWarnings("rawtypes")
98 @Nullable
99 static volatile BiFunction<? super Flowable, @NonNull ? super Subscriber, @NonNull ? extends Subscriber> onFlowableSubscribe;
100
101 @SuppressWarnings("rawtypes")
102 @Nullable
103 static volatile BiFunction<? super Maybe, @NonNull ? super MaybeObserver, @NonNull ? extends MaybeObserver> onMaybeSubscribe;
104
105 @SuppressWarnings("rawtypes")
106 @Nullable
107 static volatile BiFunction<? super Observable, @NonNull ? super Observer, @NonNull ? extends Observer> onObservableSubscribe;
108
109 @SuppressWarnings("rawtypes")
110 @Nullable
111 static volatile BiFunction<? super Single, @NonNull ? super SingleObserver, @NonNull ? extends SingleObserver> onSingleSubscribe;
112
113 @Nullable
114 static volatile BiFunction<? super Completable, @NonNull ? super CompletableObserver, @NonNull ? extends CompletableObserver> onCompletableSubscribe;
115
116 @SuppressWarnings("rawtypes")
117 @Nullable
118 static volatile BiFunction<? super ParallelFlowable, @NonNull ? super Subscriber[], @NonNull ? extends Subscriber[]> onParallelSubscribe;
119
120 @Nullable
121 static volatile BooleanSupplier onBeforeBlocking;
122
123 /** Prevents changing the plugins. */
124 static volatile boolean lockdown;
125
126 /**
127 * If true, attempting to run a blockingX operation on a (by default)
128 * computation or single scheduler will throw an IllegalStateException.
129 */
130 static volatile boolean failNonBlockingScheduler;
131
132 /**
133 * Prevents changing the plugins from then on.
134 * <p>This allows container-like environments to prevent clients
135 * messing with plugins.
136 */
137 public static void lockdown() {
138 lockdown = true;
139 }
140
141 /**
142 * Returns true if the plugins were locked down.
143 * @return true if the plugins were locked down
144 */
145 public static boolean isLockdown() {
146 return lockdown;
147 }
148
149 /**
150 * Enables or disables the blockingX operators to fail
151 * with an IllegalStateException on a non-blocking
152 * scheduler such as computation or single.
153 * <p>History: 2.0.5 - experimental
154 * @param enable enable or disable the feature
155 * @since 2.1
156 */
157 public static void setFailOnNonBlockingScheduler(boolean enable) {
158 if (lockdown) {
159 throw new IllegalStateException("Plugins can't be changed anymore");
160 }
161 failNonBlockingScheduler = enable;
162 }
163
164 /**
165 * Returns true if the blockingX operators fail
166 * with an IllegalStateException on a non-blocking scheduler
167 * such as computation or single.
168 * <p>History: 2.0.5 - experimental
169 * @return true if the blockingX operators fail on a non-blocking scheduler
170 * @since 2.1
171 */
172 public static boolean isFailOnNonBlockingScheduler() {
173 return failNonBlockingScheduler;
174 }
175
176 /**
177 * Returns the current hook function.
178 * @return the hook function, may be null
179 */
180 @Nullable
181 public static Function<? super Scheduler, ? extends Scheduler> getComputationSchedulerHandler() {
182 return onComputationHandler;
183 }
184
185 /**
186 * Returns the a hook consumer.
187 * @return the hook consumer, may be null
188 */
189 @Nullable
190 public static Consumer<? super Throwable> getErrorHandler() {
191 return errorHandler;
192 }
193
194 /**
195 * Returns the current hook function.
196 * @return the hook function, may be null
197 */
198 @Nullable
199 public static Function<? super Supplier<Scheduler>, ? extends Scheduler> getInitComputationSchedulerHandler() {
200 return onInitComputationHandler;
201 }
202
203 /**
204 * Returns the current hook function.
205 * @return the hook function, may be null
206 */
207 @Nullable
208 public static Function<? super Supplier<Scheduler>, ? extends Scheduler> getInitIoSchedulerHandler() {
209 return onInitIoHandler;
210 }
211
212 /**
213 * Returns the current hook function.
214 * @return the hook function, may be null
215 */
216 @Nullable
217 public static Function<? super Supplier<Scheduler>, ? extends Scheduler> getInitNewThreadSchedulerHandler() {
218 return onInitNewThreadHandler;
219 }
220
221 /**
222 * Returns the current hook function.
223 * @return the hook function, may be null
224 */
225 @Nullable
226 public static Function<? super Supplier<Scheduler>, ? extends Scheduler> getInitSingleSchedulerHandler() {
227 return onInitSingleHandler;
228 }
229
230 /**
231 * Returns the current hook function.
232 * @return the hook function, may be null
233 */
234 @Nullable
235 public static Function<? super Scheduler, ? extends Scheduler> getIoSchedulerHandler() {
236 return onIoHandler;
237 }
238
239 /**
240 * Returns the current hook function.
241 * @return the hook function, may be null
242 */
243 @Nullable
244 public static Function<? super Scheduler, ? extends Scheduler> getNewThreadSchedulerHandler() {
245 return onNewThreadHandler;
246 }
247
248 /**
249 * Returns the current hook function.
250 * @return the hook function, may be null
251 */
252 @Nullable
253 public static Function<? super Runnable, ? extends Runnable> getScheduleHandler() {
254 return onScheduleHandler;
255 }
256
257 /**
258 * Returns the current hook function.
259 * @return the hook function, may be null
260 */
261 @Nullable
262 public static Function<? super Scheduler, ? extends Scheduler> getSingleSchedulerHandler() {
263 return onSingleHandler;
264 }
265
266 /**
267 * Calls the associated hook function.
268 * @param defaultScheduler a {@link Supplier} which returns the hook's input value
269 * @return the value returned by the hook, not null
270 * @throws NullPointerException if the supplier parameter or its result are null
271 */
272 @NonNull
273 public static Scheduler initComputationScheduler(@NonNull Supplier<Scheduler> defaultScheduler) {
274 Objects.requireNonNull(defaultScheduler, "Scheduler Supplier can't be null");
275 Function<? super Supplier<Scheduler>, ? extends Scheduler> f = onInitComputationHandler;
276 if (f == null) {
277 return callRequireNonNull(defaultScheduler);
278 }
279 return applyRequireNonNull(f, defaultScheduler); // JIT will skip this
280 }
281
282 /**
283 * Calls the associated hook function.
284 * @param defaultScheduler a {@link Supplier} which returns the hook's input value
285 * @return the value returned by the hook, not null
286 * @throws NullPointerException if the supplier parameter or its result are null
287 */
288 @NonNull
289 public static Scheduler initIoScheduler(@NonNull Supplier<Scheduler> defaultScheduler) {
290 Objects.requireNonNull(defaultScheduler, "Scheduler Supplier can't be null");
291 Function<? super Supplier<Scheduler>, ? extends Scheduler> f = onInitIoHandler;
292 if (f == null) {
293 return callRequireNonNull(defaultScheduler);
294 }
295 return applyRequireNonNull(f, defaultScheduler);
296 }
297
298 /**
299 * Calls the associated hook function.
300 * @param defaultScheduler a {@link Supplier} which returns the hook's input value
301 * @return the value returned by the hook, not null
302 * @throws NullPointerException if the supplier parameter or its result are null
303 */
304 @NonNull
305 public static Scheduler initNewThreadScheduler(@NonNull Supplier<Scheduler> defaultScheduler) {
306 Objects.requireNonNull(defaultScheduler, "Scheduler Supplier can't be null");
307 Function<? super Supplier<Scheduler>, ? extends Scheduler> f = onInitNewThreadHandler;
308 if (f == null) {
309 return callRequireNonNull(defaultScheduler);
310 }
311 return applyRequireNonNull(f, defaultScheduler);
312 }
313
314 /**
315 * Calls the associated hook function.
316 * @param defaultScheduler a {@link Supplier} which returns the hook's input value
317 * @return the value returned by the hook, not null
318 * @throws NullPointerException if the supplier parameter or its result are null
319 */
320 @NonNull
321 public static Scheduler initSingleScheduler(@NonNull Supplier<Scheduler> defaultScheduler) {
322 Objects.requireNonNull(defaultScheduler, "Scheduler Supplier can't be null");
323 Function<? super Supplier<Scheduler>, ? extends Scheduler> f = onInitSingleHandler;
324 if (f == null) {
325 return callRequireNonNull(defaultScheduler);
326 }
327 return applyRequireNonNull(f, defaultScheduler);
328 }
329
330 /**
331 * Calls the associated hook function.
332 * @param defaultScheduler the hook's input value
333 * @return the value returned by the hook
334 */
335 @NonNull
336 public static Scheduler onComputationScheduler(@NonNull Scheduler defaultScheduler) {
337 Function<? super Scheduler, ? extends Scheduler> f = onComputationHandler;
338 if (f == null) {
339 return defaultScheduler;
340 }
341 return apply(f, defaultScheduler);
342 }
343
344 /**
345 * Called when an undeliverable error occurs.
346 * <p>
347 * Undeliverable errors are those {@code Observer.onError()} invocations that are not allowed to happen on
348 * the given consumer type ({@code Observer}, {@code Subscriber}, etc.) due to protocol restrictions
349 * because the consumer has either disposed/cancelled its {@code Disposable}/{@code Subscription} or
350 * has already terminated with an {@code onError()} or {@code onComplete()} signal.
351 * <p>
352 * By default, this global error handler prints the stacktrace via {@link Throwable#printStackTrace()}
353 * and calls {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)}
354 * on the current thread.
355 * <p>
356 * Note that on some platforms, the platform runtime terminates the current application with an error if such
357 * uncaught exceptions happen. In this case, it is recommended the application installs a global error
358 * handler via the {@link #setErrorHandler(Consumer)} plugin method.
359 *
360 * @param error the error to report
361 * @see #getErrorHandler()
362 * @see #setErrorHandler(Consumer)
363 * @see <a href="https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling">Error handling Wiki</a>
364 */
365 public static void onError(@NonNull Throwable error) {
366 Consumer<? super Throwable> f = errorHandler;
367
368 if (error == null) {
369 error = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
370 } else {
371 if (!isBug(error)) {
372 error = new UndeliverableException(error);
373 }
374 }
375
376 if (f != null) {
377 try {
378 f.accept(error);
379 return;
380 } catch (Throwable e) {
381 // Exceptions.throwIfFatal(e); TODO decide
382 e.printStackTrace(); // NOPMD
383 uncaught(e);
384 }
385 }
386
387 error.printStackTrace(); // NOPMD
388 uncaught(error);
389 }
390
391 /**
392 * Checks if the given error is one of the already named
393 * bug cases that should pass through {@link #onError(Throwable)}
394 * as is.
395 * @param error the error to check
396 * @return true if the error should pass through, false if
397 * it may be wrapped into an UndeliverableException
398 */
399 static boolean isBug(Throwable error) {
400 // user forgot to add the onError handler in subscribe
401 if (error instanceof OnErrorNotImplementedException) {
402 return true;
403 }
404 // the sender didn't honor the request amount
405 // it's either due to an operator bug or concurrent onNext
406 if (error instanceof MissingBackpressureException) {
407 return true;
408 }
409 // general protocol violations
410 // it's either due to an operator bug or concurrent onNext
411 if (error instanceof IllegalStateException) {
412 return true;
413 }
414 // nulls are generally not allowed
415 // likely an operator bug or missing null-check
416 if (error instanceof NullPointerException) {
417 return true;
418 }
419 // bad arguments, likely invalid user input
420 if (error instanceof IllegalArgumentException) {
421 return true;
422 }
423 // Crash while handling an exception
424 if (error instanceof CompositeException) {
425 return true;
426 }
427 // everything else is probably due to lifecycle limits
428 return false;
429 }
430
431 static void uncaught(@NonNull Throwable error) {
432 Thread currentThread = Thread.currentThread();
433 UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
434 handler.uncaughtException(currentThread, error);
435 }
436
437 /**
438 * Calls the associated hook function.
439 * @param defaultScheduler the hook's input value
440 * @return the value returned by the hook
441 */
442 @NonNull
443 public static Scheduler onIoScheduler(@NonNull Scheduler defaultScheduler) {
444 Function<? super Scheduler, ? extends Scheduler> f = onIoHandler;
445 if (f == null) {
446 return defaultScheduler;
447 }
448 return apply(f, defaultScheduler);
449 }
450
451 /**
452 * Calls the associated hook function.
453 * @param defaultScheduler the hook's input value
454 * @return the value returned by the hook
455 */
456 @NonNull
457 public static Scheduler onNewThreadScheduler(@NonNull Scheduler defaultScheduler) {
458 Function<? super Scheduler, ? extends Scheduler> f = onNewThreadHandler;
459 if (f == null) {
460 return defaultScheduler;
461 }
462 return apply(f, defaultScheduler);
463 }
464
465 /**
466 * Called when a task is scheduled.
467 * @param run the runnable instance
468 * @return the replacement runnable
469 */
470 @NonNull
471 public static Runnable onSchedule(@NonNull Runnable run) {
472 Objects.requireNonNull(run, "run is null");
473
474 Function<? super Runnable, ? extends Runnable> f = onScheduleHandler;
475 if (f == null) {
476 return run;
477 }
478 return apply(f, run);
479 }
480
481 /**
482 * Calls the associated hook function.
483 * @param defaultScheduler the hook's input value
484 * @return the value returned by the hook
485 */
486 @NonNull
487 public static Scheduler onSingleScheduler(@NonNull Scheduler defaultScheduler) {
488 Function<? super Scheduler, ? extends Scheduler> f = onSingleHandler;
489 if (f == null) {
490 return defaultScheduler;
491 }
492 return apply(f, defaultScheduler);
493 }
494
495 /**
496 * Removes all handlers and resets to default behavior.
497 */
498 public static void reset() {
499 setErrorHandler(null);
500 setScheduleHandler(null);
501
502 setComputationSchedulerHandler(null);
503 setInitComputationSchedulerHandler(null);
504
505 setIoSchedulerHandler(null);
506 setInitIoSchedulerHandler(null);
507
508 setSingleSchedulerHandler(null);
509 setInitSingleSchedulerHandler(null);
510
511 setNewThreadSchedulerHandler(null);
512 setInitNewThreadSchedulerHandler(null);
513
514 setOnFlowableAssembly(null);
515 setOnFlowableSubscribe(null);
516
517 setOnObservableAssembly(null);
518 setOnObservableSubscribe(null);
519
520 setOnSingleAssembly(null);
521 setOnSingleSubscribe(null);
522
523 setOnCompletableAssembly(null);
524 setOnCompletableSubscribe(null);
525
526 setOnConnectableFlowableAssembly(null);
527 setOnConnectableObservableAssembly(null);
528
529 setOnMaybeAssembly(null);
530 setOnMaybeSubscribe(null);
531
532 setOnParallelAssembly(null);
533 setOnParallelSubscribe(null);
534
535 setFailOnNonBlockingScheduler(false);
536 setOnBeforeBlocking(null);
537 }
538
539 /**
540 * Sets the specific hook function.
541 * @param handler the hook function to set, null allowed
542 */
543 public static void setComputationSchedulerHandler(@Nullable Function<? super Scheduler, ? extends Scheduler> handler) {
544 if (lockdown) {
545 throw new IllegalStateException("Plugins can't be changed anymore");
546 }
547 onComputationHandler = handler;
548 }
549
550 /**
551 * Sets the specific hook function.
552 * @param handler the hook function to set, null allowed
553 */
554 public static void setErrorHandler(@Nullable Consumer<? super Throwable> handler) {
555 if (lockdown) {
556 throw new IllegalStateException("Plugins can't be changed anymore");
557 }
558 errorHandler = handler;
559 }
560
561 /**
562 * Sets the specific hook function.
563 * @param handler the hook function to set, null allowed, but the function may not return null
564 */
565 public static void setInitComputationSchedulerHandler(@Nullable Function<? super Supplier<Scheduler>, ? extends Scheduler> handler) {
566 if (lockdown) {
567 throw new IllegalStateException("Plugins can't be changed anymore");
568 }
569 onInitComputationHandler = handler;
570 }
571
572 /**
573 * Sets the specific hook function.
574 * @param handler the hook function to set, null allowed, but the function may not return null
575 */
576 public static void setInitIoSchedulerHandler(@Nullable Function<? super Supplier<Scheduler>, ? extends Scheduler> handler) {
577 if (lockdown) {
578 throw new IllegalStateException("Plugins can't be changed anymore");
579 }
580 onInitIoHandler = handler;
581 }
582
583 /**
584 * Sets the specific hook function.
585 * @param handler the hook function to set, null allowed, but the function may not return null
586 */
587 public static void setInitNewThreadSchedulerHandler(@Nullable Function<? super Supplier<Scheduler>, ? extends Scheduler> handler) {
588 if (lockdown) {
589 throw new IllegalStateException("Plugins can't be changed anymore");
590 }
591 onInitNewThreadHandler = handler;
592 }
593
594 /**
595 * Sets the specific hook function.
596 * @param handler the hook function to set, null allowed, but the function may not return null
597 */
598 public static void setInitSingleSchedulerHandler(@Nullable Function<? super Supplier<Scheduler>, ? extends Scheduler> handler) {
599 if (lockdown) {
600 throw new IllegalStateException("Plugins can't be changed anymore");
601 }
602 onInitSingleHandler = handler;
603 }
604
605 /**
606 * Sets the specific hook function.
607 * @param handler the hook function to set, null allowed
608 */
609 public static void setIoSchedulerHandler(@Nullable Function<? super Scheduler, ? extends Scheduler> handler) {
610 if (lockdown) {
611 throw new IllegalStateException("Plugins can't be changed anymore");
612 }
613 onIoHandler = handler;
614 }
615
616 /**
617 * Sets the specific hook function.
618 * @param handler the hook function to set, null allowed
619 */
620 public static void setNewThreadSchedulerHandler(@Nullable Function<? super Scheduler, ? extends Scheduler> handler) {
621 if (lockdown) {
622 throw new IllegalStateException("Plugins can't be changed anymore");
623 }
624 onNewThreadHandler = handler;
625 }
626
627 /**
628 * Sets the specific hook function.
629 * @param handler the hook function to set, null allowed
630 */
631 public static void setScheduleHandler(@Nullable Function<? super Runnable, ? extends Runnable> handler) {
632 if (lockdown) {
633 throw new IllegalStateException("Plugins can't be changed anymore");
634 }
635 onScheduleHandler = handler;
636 }
637
638 /**
639 * Sets the specific hook function.
640 * @param handler the hook function to set, null allowed
641 */
642 public static void setSingleSchedulerHandler(@Nullable Function<? super Scheduler, ? extends Scheduler> handler) {
643 if (lockdown) {
644 throw new IllegalStateException("Plugins can't be changed anymore");
645 }
646 onSingleHandler = handler;
647 }
648
649 /**
650 * Revokes the lockdown, only for testing purposes.
651 */
652 /* test. */static void unlock() {
653 lockdown = false;
654 }
655
656 /**
657 * Returns the current hook function.
658 * @return the hook function, may be null
659 */
660 @Nullable
661 public static Function<? super Completable, ? extends Completable> getOnCompletableAssembly() {
662 return onCompletableAssembly;
663 }
664
665 /**
666 * Returns the current hook function.
667 * @return the hook function, may be null
668 */
669 @Nullable
670 public static BiFunction<? super Completable, @NonNull ? super CompletableObserver, @NonNull ? extends CompletableObserver> getOnCompletableSubscribe() {
671 return onCompletableSubscribe;
672 }
673
674 /**
675 * Returns the current hook function.
676 * @return the hook function, may be null
677 */
678 @SuppressWarnings("rawtypes")
679 @Nullable
680 public static Function<? super Flowable, ? extends Flowable> getOnFlowableAssembly() {
681 return onFlowableAssembly;
682 }
683
684 /**
685 * Returns the current hook function.
686 * @return the hook function, may be null
687 */
688 @SuppressWarnings("rawtypes")
689 @Nullable
690 public static Function<? super ConnectableFlowable, ? extends ConnectableFlowable> getOnConnectableFlowableAssembly() {
691 return onConnectableFlowableAssembly;
692 }
693
694 /**
695 * Returns the current hook function.
696 * @return the hook function, may be null
697 */
698 @Nullable
699 @SuppressWarnings("rawtypes")
700 public static BiFunction<? super Flowable, @NonNull ? super Subscriber, @NonNull ? extends Subscriber> getOnFlowableSubscribe() {
701 return onFlowableSubscribe;
702 }
703
704 /**
705 * Returns the current hook function.
706 * @return the hook function, may be null
707 */
708 @Nullable
709 @SuppressWarnings("rawtypes")
710 public static BiFunction<? super Maybe, @NonNull ? super MaybeObserver, @NonNull ? extends MaybeObserver> getOnMaybeSubscribe() {
711 return onMaybeSubscribe;
712 }
713
714 /**
715 * Returns the current hook function.
716 * @return the hook function, may be null
717 */
718 @Nullable
719 @SuppressWarnings("rawtypes")
720 public static Function<? super Maybe, ? extends Maybe> getOnMaybeAssembly() {
721 return onMaybeAssembly;
722 }
723
724 /**
725 * Returns the current hook function.
726 * @return the hook function, may be null
727 */
728 @Nullable
729 @SuppressWarnings("rawtypes")
730 public static Function<? super Single, ? extends Single> getOnSingleAssembly() {
731 return onSingleAssembly;
732 }
733
734 /**
735 * Returns the current hook function.
736 * @return the hook function, may be null
737 */
738 @Nullable
739 @SuppressWarnings("rawtypes")
740 public static BiFunction<? super Single, @NonNull ? super SingleObserver, @NonNull ? extends SingleObserver> getOnSingleSubscribe() {
741 return onSingleSubscribe;
742 }
743
744 /**
745 * Returns the current hook function.
746 * @return the hook function, may be null
747 */
748 @Nullable
749 @SuppressWarnings("rawtypes")
750 public static Function<? super Observable, ? extends Observable> getOnObservableAssembly() {
751 return onObservableAssembly;
752 }
753
754 /**
755 * Returns the current hook function.
756 * @return the hook function, may be null
757 */
758 @Nullable
759 @SuppressWarnings("rawtypes")
760 public static Function<? super ConnectableObservable, ? extends ConnectableObservable> getOnConnectableObservableAssembly() {
761 return onConnectableObservableAssembly;
762 }
763
764 /**
765 * Returns the current hook function.
766 * @return the hook function, may be null
767 */
768 @Nullable
769 @SuppressWarnings("rawtypes")
770 public static BiFunction<? super Observable, @NonNull ? super Observer, @NonNull ? extends Observer> getOnObservableSubscribe() {
771 return onObservableSubscribe;
772 }
773
774 /**
775 * Sets the specific hook function.
776 * @param onCompletableAssembly the hook function to set, null allowed
777 */
778 public static void setOnCompletableAssembly(@Nullable Function<? super Completable, ? extends Completable> onCompletableAssembly) {
779 if (lockdown) {
780 throw new IllegalStateException("Plugins can't be changed anymore");
781 }
782 RxJavaPlugins.onCompletableAssembly = onCompletableAssembly;
783 }
784
785 /**
786 * Sets the specific hook function.
787 * @param onCompletableSubscribe the hook function to set, null allowed
788 */
789 public static void setOnCompletableSubscribe(
790 @Nullable BiFunction<? super Completable, @NonNull ? super CompletableObserver, @NonNull ? extends CompletableObserver> onCompletableSubscribe) {
791 if (lockdown) {
792 throw new IllegalStateException("Plugins can't be changed anymore");
793 }
794 RxJavaPlugins.onCompletableSubscribe = onCompletableSubscribe;
795 }
796
797 /**
798 * Sets the specific hook function.
799 * @param onFlowableAssembly the hook function to set, null allowed
800 */
801 @SuppressWarnings("rawtypes")
802 public static void setOnFlowableAssembly(@Nullable Function<? super Flowable, ? extends Flowable> onFlowableAssembly) {
803 if (lockdown) {
804 throw new IllegalStateException("Plugins can't be changed anymore");
805 }
806 RxJavaPlugins.onFlowableAssembly = onFlowableAssembly;
807 }
808
809 /**
810 * Sets the specific hook function.
811 * @param onMaybeAssembly the hook function to set, null allowed
812 */
813 @SuppressWarnings("rawtypes")
814 public static void setOnMaybeAssembly(@Nullable Function<? super Maybe, ? extends Maybe> onMaybeAssembly) {
815 if (lockdown) {
816 throw new IllegalStateException("Plugins can't be changed anymore");
817 }
818 RxJavaPlugins.onMaybeAssembly = onMaybeAssembly;
819 }
820
821 /**
822 * Sets the specific hook function.
823 * @param onConnectableFlowableAssembly the hook function to set, null allowed
824 */
825 @SuppressWarnings("rawtypes")
826 public static void setOnConnectableFlowableAssembly(@Nullable Function<? super ConnectableFlowable, ? extends ConnectableFlowable> onConnectableFlowableAssembly) {
827 if (lockdown) {
828 throw new IllegalStateException("Plugins can't be changed anymore");
829 }
830 RxJavaPlugins.onConnectableFlowableAssembly = onConnectableFlowableAssembly;
831 }
832
833 /**
834 * Sets the specific hook function.
835 * @param onFlowableSubscribe the hook function to set, null allowed
836 */
837 @SuppressWarnings("rawtypes")
838 public static void setOnFlowableSubscribe(@Nullable BiFunction<? super Flowable, @NonNull ? super Subscriber, @NonNull ? extends Subscriber> onFlowableSubscribe) {
839 if (lockdown) {
840 throw new IllegalStateException("Plugins can't be changed anymore");
841 }
842 RxJavaPlugins.onFlowableSubscribe = onFlowableSubscribe;
843 }
844
845 /**
846 * Sets the specific hook function.
847 * @param onMaybeSubscribe the hook function to set, null allowed
848 */
849 @SuppressWarnings("rawtypes")
850 public static void setOnMaybeSubscribe(@Nullable BiFunction<? super Maybe, @NonNull MaybeObserver, @NonNull ? extends MaybeObserver> onMaybeSubscribe) {
851 if (lockdown) {
852 throw new IllegalStateException("Plugins can't be changed anymore");
853 }
854 RxJavaPlugins.onMaybeSubscribe = onMaybeSubscribe;
855 }
856
857 /**
858 * Sets the specific hook function.
859 * @param onObservableAssembly the hook function to set, null allowed
860 */
861 @SuppressWarnings("rawtypes")
862 public static void setOnObservableAssembly(@Nullable Function<? super Observable, ? extends Observable> onObservableAssembly) {
863 if (lockdown) {
864 throw new IllegalStateException("Plugins can't be changed anymore");
865 }
866 RxJavaPlugins.onObservableAssembly = onObservableAssembly;
867 }
868
869 /**
870 * Sets the specific hook function.
871 * @param onConnectableObservableAssembly the hook function to set, null allowed
872 */
873 @SuppressWarnings("rawtypes")
874 public static void setOnConnectableObservableAssembly(@Nullable Function<? super ConnectableObservable, ? extends ConnectableObservable> onConnectableObservableAssembly) {
875 if (lockdown) {
876 throw new IllegalStateException("Plugins can't be changed anymore");
877 }
878 RxJavaPlugins.onConnectableObservableAssembly = onConnectableObservableAssembly;
879 }
880
881 /**
882 * Sets the specific hook function.
883 * @param onObservableSubscribe the hook function to set, null allowed
884 */
885 @SuppressWarnings("rawtypes")
886 public static void setOnObservableSubscribe(
887 @Nullable BiFunction<? super Observable, @NonNull ? super Observer, @NonNull ? extends Observer> onObservableSubscribe) {
888 if (lockdown) {
889 throw new IllegalStateException("Plugins can't be changed anymore");
890 }
891 RxJavaPlugins.onObservableSubscribe = onObservableSubscribe;
892 }
893
894 /**
895 * Sets the specific hook function.
896 * @param onSingleAssembly the hook function to set, null allowed
897 */
898 @SuppressWarnings("rawtypes")
899 public static void setOnSingleAssembly(@Nullable Function<? super Single, ? extends Single> onSingleAssembly) {
900 if (lockdown) {
901 throw new IllegalStateException("Plugins can't be changed anymore");
902 }
903 RxJavaPlugins.onSingleAssembly = onSingleAssembly;
904 }
905
906 /**
907 * Sets the specific hook function.
908 * @param onSingleSubscribe the hook function to set, null allowed
909 */
910 @SuppressWarnings("rawtypes")
911 public static void setOnSingleSubscribe(@Nullable BiFunction<? super Single, @NonNull ? super SingleObserver, @NonNull ? extends SingleObserver> onSingleSubscribe) {
912 if (lockdown) {
913 throw new IllegalStateException("Plugins can't be changed anymore");
914 }
915 RxJavaPlugins.onSingleSubscribe = onSingleSubscribe;
916 }
917
918 /**
919 * Calls the associated hook function.
920 * @param <T> the value type
921 * @param source the hook's input value
922 * @param subscriber the subscriber
923 * @return the value returned by the hook
924 */
925 @SuppressWarnings({ "rawtypes", "unchecked" })
926 @NonNull
927 public static <@NonNull T> Subscriber<? super T> onSubscribe(@NonNull Flowable<T> source, @NonNull Subscriber<? super T> subscriber) {
928 BiFunction<? super Flowable, @NonNull ? super Subscriber, @NonNull ? extends Subscriber> f = onFlowableSubscribe;
929 if (f != null) {
930 return apply(f, source, subscriber);
931 }
932 return subscriber;
933 }
934
935 /**
936 * Calls the associated hook function.
937 * @param <T> the value type
938 * @param source the hook's input value
939 * @param observer the observer
940 * @return the value returned by the hook
941 */
942 @SuppressWarnings({ "rawtypes", "unchecked" })
943 @NonNull
944 public static <@NonNull T> Observer<? super T> onSubscribe(@NonNull Observable<T> source, @NonNull Observer<? super T> observer) {
945 BiFunction<? super Observable, @NonNull ? super Observer, @NonNull ? extends Observer> f = onObservableSubscribe;
946 if (f != null) {
947 return apply(f, source, observer);
948 }
949 return observer;
950 }
951
952 /**
953 * Calls the associated hook function.
954 * @param <T> the value type
955 * @param source the hook's input value
956 * @param observer the observer
957 * @return the value returned by the hook
958 */
959 @SuppressWarnings({ "rawtypes", "unchecked" })
960 @NonNull
961 public static <@NonNull T> SingleObserver<? super T> onSubscribe(@NonNull Single<T> source, @NonNull SingleObserver<? super T> observer) {
962 BiFunction<? super Single, @NonNull ? super SingleObserver, @NonNull ? extends SingleObserver> f = onSingleSubscribe;
963 if (f != null) {
964 return apply(f, source, observer);
965 }
966 return observer;
967 }
968
969 /**
970 * Calls the associated hook function.
971 * @param source the hook's input value
972 * @param observer the observer
973 * @return the value returned by the hook
974 */
975 @NonNull
976 public static CompletableObserver onSubscribe(@NonNull Completable source, @NonNull CompletableObserver observer) {
977 BiFunction<? super Completable, @NonNull ? super CompletableObserver, @NonNull ? extends CompletableObserver> f = onCompletableSubscribe;
978 if (f != null) {
979 return apply(f, source, observer);
980 }
981 return observer;
982 }
983
984 /**
985 * Calls the associated hook function.
986 * @param <T> the value type
987 * @param source the hook's input value
988 * @param observer the subscriber
989 * @return the value returned by the hook
990 */
991 @SuppressWarnings({ "rawtypes", "unchecked" })
992 @NonNull
993 public static <@NonNull T> MaybeObserver<? super T> onSubscribe(@NonNull Maybe<T> source, @NonNull MaybeObserver<? super T> observer) {
994 BiFunction<? super Maybe, @NonNull ? super MaybeObserver, @NonNull ? extends MaybeObserver> f = onMaybeSubscribe;
995 if (f != null) {
996 return apply(f, source, observer);
997 }
998 return observer;
999 }
1000
1001 /**
1002 * Calls the associated hook function.
1003 * @param <T> the value type
1004 * @param source the hook's input value
1005 * @param subscribers the array of subscribers
1006 * @return the value returned by the hook
1007 */
1008 @SuppressWarnings({ "rawtypes" })
1009 @NonNull
1010 public static <@NonNull T> Subscriber<? super T>[] onSubscribe(@NonNull ParallelFlowable<T> source, @NonNull Subscriber<? super T>[] subscribers) {
1011 BiFunction<? super ParallelFlowable, @NonNull ? super Subscriber[], @NonNull ? extends Subscriber[]> f = onParallelSubscribe;
1012 if (f != null) {
1013 return apply(f, source, subscribers);
1014 }
1015 return subscribers;
1016 }
1017
1018 /**
1019 * Calls the associated hook function.
1020 * @param <T> the value type
1021 * @param source the hook's input value
1022 * @return the value returned by the hook
1023 */
1024 @SuppressWarnings({ "rawtypes", "unchecked" })
1025 @NonNull
1026 public static <@NonNull T> Maybe<T> onAssembly(@NonNull Maybe<T> source) {
1027 Function<? super Maybe, ? extends Maybe> f = onMaybeAssembly;
1028 if (f != null) {
1029 return apply(f, source);
1030 }
1031 return source;
1032 }
1033
1034 /**
1035 * Calls the associated hook function.
1036 * @param <T> the value type
1037 * @param source the hook's input value
1038 * @return the value returned by the hook
1039 */
1040 @SuppressWarnings({ "rawtypes", "unchecked" })
1041 @NonNull
1042 public static <@NonNull T> Flowable<T> onAssembly(@NonNull Flowable<T> source) {
1043 Function<? super Flowable, ? extends Flowable> f = onFlowableAssembly;
1044 if (f != null) {
1045 return apply(f, source);
1046 }
1047 return source;
1048 }
1049
1050 /**
1051 * Calls the associated hook function.
1052 * @param <T> the value type
1053 * @param source the hook's input value
1054 * @return the value returned by the hook
1055 */
1056 @SuppressWarnings({ "rawtypes", "unchecked" })
1057 @NonNull
1058 public static <@NonNull T> ConnectableFlowable<T> onAssembly(@NonNull ConnectableFlowable<T> source) {
1059 Function<? super ConnectableFlowable, ? extends ConnectableFlowable> f = onConnectableFlowableAssembly;
1060 if (f != null) {
1061 return apply(f, source);
1062 }
1063 return source;
1064 }
1065
1066 /**
1067 * Calls the associated hook function.
1068 * @param <T> the value type
1069 * @param source the hook's input value
1070 * @return the value returned by the hook
1071 */
1072 @SuppressWarnings({ "rawtypes", "unchecked" })
1073 @NonNull
1074 public static <@NonNull T> Observable<T> onAssembly(@NonNull Observable<T> source) {
1075 Function<? super Observable, ? extends Observable> f = onObservableAssembly;
1076 if (f != null) {
1077 return apply(f, source);
1078 }
1079 return source;
1080 }
1081
1082 /**
1083 * Calls the associated hook function.
1084 * @param <T> the value type
1085 * @param source the hook's input value
1086 * @return the value returned by the hook
1087 */
1088 @SuppressWarnings({ "rawtypes", "unchecked" })
1089 @NonNull
1090 public static <@NonNull T> ConnectableObservable<T> onAssembly(@NonNull ConnectableObservable<T> source) {
1091 Function<? super ConnectableObservable, ? extends ConnectableObservable> f = onConnectableObservableAssembly;
1092 if (f != null) {
1093 return apply(f, source);
1094 }
1095 return source;
1096 }
1097
1098 /**
1099 * Calls the associated hook function.
1100 * @param <T> the value type
1101 * @param source the hook's input value
1102 * @return the value returned by the hook
1103 */
1104 @SuppressWarnings({ "rawtypes", "unchecked" })
1105 @NonNull
1106 public static <@NonNull T> Single<T> onAssembly(@NonNull Single<T> source) {
1107 Function<? super Single, ? extends Single> f = onSingleAssembly;
1108 if (f != null) {
1109 return apply(f, source);
1110 }
1111 return source;
1112 }
1113
1114 /**
1115 * Calls the associated hook function.
1116 * @param source the hook's input value
1117 * @return the value returned by the hook
1118 */
1119 @NonNull
1120 public static Completable onAssembly(@NonNull Completable source) {
1121 Function<? super Completable, ? extends Completable> f = onCompletableAssembly;
1122 if (f != null) {
1123 return apply(f, source);
1124 }
1125 return source;
1126 }
1127
1128 /**
1129 * Sets the specific hook function.
1130 * <p>History: 2.0.6 - experimental; 2.1 - beta
1131 * @param handler the hook function to set, null allowed
1132 * @since 2.2
1133 */
1134 @SuppressWarnings("rawtypes")
1135 public static void setOnParallelAssembly(@Nullable Function<? super ParallelFlowable, ? extends ParallelFlowable> handler) {
1136 if (lockdown) {
1137 throw new IllegalStateException("Plugins can't be changed anymore");
1138 }
1139 onParallelAssembly = handler;
1140 }
1141
1142 /**
1143 * Returns the current hook function.
1144 * <p>History: 2.0.6 - experimental; 2.1 - beta
1145 * @return the hook function, may be null
1146 * @since 2.2
1147 */
1148 @SuppressWarnings("rawtypes")
1149 @Nullable
1150 public static Function<? super ParallelFlowable, ? extends ParallelFlowable> getOnParallelAssembly() {
1151 return onParallelAssembly;
1152 }
1153
1154 /**
1155 * Sets the specific hook function.
1156 * <p>History: 3.0.11 - experimental
1157 * @param handler the hook function to set, null allowed
1158 * @since 3.1.0
1159 */
1160 @SuppressWarnings("rawtypes")
1161 public static void setOnParallelSubscribe(@Nullable BiFunction<? super ParallelFlowable, @NonNull ? super Subscriber[], @NonNull ? extends Subscriber[]> handler) {
1162 if (lockdown) {
1163 throw new IllegalStateException("Plugins can't be changed anymore");
1164 }
1165 onParallelSubscribe = handler;
1166 }
1167
1168 /**
1169 * Returns the current hook function.
1170 * <p>History: 3.0.11 - experimental
1171 * @return the hook function, may be null
1172 * @since 3.1.0
1173 */
1174 @SuppressWarnings("rawtypes")
1175 @Nullable
1176 public static BiFunction<? super ParallelFlowable, @NonNull ? super Subscriber[], @NonNull ? extends Subscriber[]> getOnParallelSubscribe() {
1177 return onParallelSubscribe;
1178 }
1179
1180 /**
1181 * Calls the associated hook function.
1182 * <p>History: 2.0.6 - experimental; 2.1 - beta
1183 * @param <T> the value type of the source
1184 * @param source the hook's input value
1185 * @return the value returned by the hook
1186 * @since 2.2
1187 */
1188 @SuppressWarnings({ "rawtypes", "unchecked" })
1189 @NonNull
1190 public static <@NonNull T> ParallelFlowable<T> onAssembly(@NonNull ParallelFlowable<T> source) {
1191 Function<? super ParallelFlowable, ? extends ParallelFlowable> f = onParallelAssembly;
1192 if (f != null) {
1193 return apply(f, source);
1194 }
1195 return source;
1196 }
1197
1198 /**
1199 * Called before an operator attempts a blocking operation
1200 * such as awaiting a condition or signal
1201 * and should return true to indicate the operator
1202 * should not block but throw an IllegalArgumentException.
1203 * <p>History: 2.0.5 - experimental
1204 * @return true if the blocking should be prevented
1205 * @see #setFailOnNonBlockingScheduler(boolean)
1206 * @since 2.1
1207 */
1208 public static boolean onBeforeBlocking() {
1209 BooleanSupplier f = onBeforeBlocking;
1210 if (f != null) {
1211 try {
1212 return f.getAsBoolean();
1213 } catch (Throwable ex) {
1214 throw ExceptionHelper.wrapOrThrow(ex);
1215 }
1216 }
1217 return false;
1218 }
1219
1220 /**
1221 * Set the handler that is called when an operator attempts a blocking
1222 * await; the handler should return true to prevent the blocking
1223 * and to signal an IllegalStateException instead.
1224 * <p>History: 2.0.5 - experimental
1225 * @param handler the handler to set, null resets to the default handler
1226 * that always returns false
1227 * @see #onBeforeBlocking()
1228 * @since 2.1
1229 */
1230 public static void setOnBeforeBlocking(@Nullable BooleanSupplier handler) {
1231 if (lockdown) {
1232 throw new IllegalStateException("Plugins can't be changed anymore");
1233 }
1234 onBeforeBlocking = handler;
1235 }
1236
1237 /**
1238 * Returns the current blocking handler or null if no custom handler
1239 * is set.
1240 * <p>History: 2.0.5 - experimental
1241 * @return the current blocking handler or null if not specified
1242 * @since 2.1
1243 */
1244 @Nullable
1245 public static BooleanSupplier getOnBeforeBlocking() {
1246 return onBeforeBlocking;
1247 }
1248
1249 /**
1250 * Create an instance of the default {@link Scheduler} used for {@link Schedulers#computation()}
1251 * except using {@code threadFactory} for thread creation.
1252 * <p>History: 2.0.5 - experimental
1253 * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any
1254 * system properties for configuring new thread creation. Cannot be null.
1255 * @return the created Scheduler instance
1256 * @since 2.1
1257 */
1258 @NonNull
1259 public static Scheduler createComputationScheduler(@NonNull ThreadFactory threadFactory) {
1260 return new ComputationScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null"));
1261 }
1262
1263 /**
1264 * Create an instance of the default {@link Scheduler} used for {@link Schedulers#io()}
1265 * except using {@code threadFactory} for thread creation.
1266 * <p>History: 2.0.5 - experimental
1267 * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any
1268 * system properties for configuring new thread creation. Cannot be null.
1269 * @return the created Scheduler instance
1270 * @since 2.1
1271 */
1272 @NonNull
1273 public static Scheduler createIoScheduler(@NonNull ThreadFactory threadFactory) {
1274 return new IoScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null"));
1275 }
1276
1277 /**
1278 * Create an instance of the default {@link Scheduler} used for {@link Schedulers#newThread()}
1279 * except using {@code threadFactory} for thread creation.
1280 * <p>History: 2.0.5 - experimental
1281 * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any
1282 * system properties for configuring new thread creation. Cannot be null.
1283 * @return the created Scheduler instance
1284 * @since 2.1
1285 */
1286 @NonNull
1287 public static Scheduler createNewThreadScheduler(@NonNull ThreadFactory threadFactory) {
1288 return new NewThreadScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null"));
1289 }
1290
1291 /**
1292 * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()}
1293 * except using {@code threadFactory} for thread creation.
1294 * <p>History: 2.0.5 - experimental
1295 * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any
1296 * system properties for configuring new thread creation. Cannot be null.
1297 * @return the created Scheduler instance
1298 * @since 2.1
1299 */
1300 @NonNull
1301 public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) {
1302 return new SingleScheduler(Objects.requireNonNull(threadFactory, "threadFactory is null"));
1303 }
1304
1305 /**
1306 * Create an instance of a {@link Scheduler} by wrapping an existing {@link Executor}.
1307 * <p>
1308 * This method allows creating an {@code Executor}-backed {@code Scheduler} before the {@link Schedulers} class
1309 * would initialize the standard {@code Scheduler}s.
1310 *
1311 * @param executor the {@code Executor} to wrap and turn into a {@code Scheduler}.
1312 * @param interruptibleWorker if {@code true}, the tasks submitted to the {@link io.reactivex.rxjava3.core.Scheduler.Worker Scheduler.Worker} will
1313 * be interrupted when the task is disposed.
1314 * @param fair if {@code true}, tasks submitted to the {@code Scheduler} or {@code Worker} will be executed by the underlying {@code Executor} one after the other, still
1315 * in a FIFO and non-overlapping manner, but allows interleaving with other tasks submitted to the underlying {@code Executor}.
1316 * If {@code false}, the underlying FIFO scheme will execute as many tasks as it can before giving up the underlying {@code Executor} thread.
1317 * @return the new {@code Scheduler} wrapping the {@code Executor}
1318 * @since 3.1.0
1319 */
1320 @NonNull
1321 public static Scheduler createExecutorScheduler(@NonNull Executor executor, boolean interruptibleWorker, boolean fair) {
1322 return new ExecutorScheduler(executor, interruptibleWorker, fair);
1323 }
1324
1325 /**
1326 * Wraps the call to the function in try-catch and propagates thrown
1327 * checked exceptions as RuntimeException.
1328 * @param <T> the input type
1329 * @param <R> the output type
1330 * @param f the function to call, not null (not verified)
1331 * @param t the parameter value to the function
1332 * @return the result of the function call
1333 */
1334 @NonNull
1335 static <@NonNull T, @NonNull R> R apply(@NonNull Function<T, R> f, @NonNull T t) {
1336 try {
1337 return f.apply(t);
1338 } catch (Throwable ex) {
1339 throw ExceptionHelper.wrapOrThrow(ex);
1340 }
1341 }
1342
1343 /**
1344 * Wraps the call to the function in try-catch and propagates thrown
1345 * checked exceptions as RuntimeException.
1346 * @param <T> the first input type
1347 * @param <U> the second input type
1348 * @param <R> the output type
1349 * @param f the function to call, not null (not verified)
1350 * @param t the first parameter value to the function
1351 * @param u the second parameter value to the function
1352 * @return the result of the function call
1353 */
1354 @NonNull
1355 static <@NonNull T, @NonNull U, @NonNull R> R apply(@NonNull BiFunction<T, U, R> f, @NonNull T t, @NonNull U u) {
1356 try {
1357 return f.apply(t, u);
1358 } catch (Throwable ex) {
1359 throw ExceptionHelper.wrapOrThrow(ex);
1360 }
1361 }
1362
1363 /**
1364 * Wraps the call to the Scheduler creation supplier in try-catch and propagates thrown
1365 * checked exceptions as RuntimeException and enforces that result is not null.
1366 * @param s the {@link Supplier} which returns a {@link Scheduler}, not null (not verified). Cannot return null
1367 * @return the result of the supplier call, not null
1368 * @throws NullPointerException if the supplier parameter returns null
1369 */
1370 @NonNull
1371 static Scheduler callRequireNonNull(@NonNull Supplier<Scheduler> s) {
1372 try {
1373 return Objects.requireNonNull(s.get(), "Scheduler Supplier result can't be null");
1374 } catch (Throwable ex) {
1375 throw ExceptionHelper.wrapOrThrow(ex);
1376 }
1377 }
1378
1379 /**
1380 * Wraps the call to the Scheduler creation function in try-catch and propagates thrown
1381 * checked exceptions as RuntimeException and enforces that result is not null.
1382 * @param f the function to call, not null (not verified). Cannot return null
1383 * @param s the parameter value to the function
1384 * @return the result of the function call, not null
1385 * @throws NullPointerException if the function parameter returns null
1386 */
1387 @NonNull
1388 static Scheduler applyRequireNonNull(@NonNull Function<? super Supplier<Scheduler>, ? extends Scheduler> f, Supplier<Scheduler> s) {
1389 return Objects.requireNonNull(apply(f, s), "Scheduler Supplier result can't be null");
1390 }
1391
1392 /** Helper class, no instances. */
1393 private RxJavaPlugins() {
1394 throw new IllegalStateException("No instances!");
1395 }
1396 }