All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.plugins.RxJavaPlugins Maven / Gradle / Ivy

The newest version!
/**
 * 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.plugins;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.*;

import org.reactivestreams.Subscriber;

import io.reactivex.*;
import io.reactivex.annotations.*;
import io.reactivex.exceptions.*;
import io.reactivex.flowables.ConnectableFlowable;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.schedulers.*;
import io.reactivex.internal.util.ExceptionHelper;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.parallel.ParallelFlowable;
import io.reactivex.schedulers.Schedulers;
/**
 * Utility class to inject handlers to certain standard RxJava operations.
 */
public final class RxJavaPlugins {
    @Nullable
    static volatile Consumer errorHandler;

    @Nullable
    static volatile Function onScheduleHandler;

    @Nullable
    static volatile Function, ? extends Scheduler> onInitComputationHandler;

    @Nullable
    static volatile Function, ? extends Scheduler> onInitSingleHandler;

    @Nullable
    static volatile Function, ? extends Scheduler> onInitIoHandler;

    @Nullable
    static volatile Function, ? extends Scheduler> onInitNewThreadHandler;

    @Nullable
    static volatile Function onComputationHandler;

    @Nullable
    static volatile Function onSingleHandler;

    @Nullable
    static volatile Function onIoHandler;

    @Nullable
    static volatile Function onNewThreadHandler;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onFlowableAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onConnectableFlowableAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onObservableAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onConnectableObservableAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onMaybeAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onSingleAssembly;

    @Nullable
    static volatile Function onCompletableAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile Function onParallelAssembly;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile BiFunction onFlowableSubscribe;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile BiFunction onMaybeSubscribe;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile BiFunction onObservableSubscribe;

    @SuppressWarnings("rawtypes")
    @Nullable
    static volatile BiFunction onSingleSubscribe;

    @Nullable
    static volatile BiFunction onCompletableSubscribe;

    @Nullable
    static volatile BooleanSupplier onBeforeBlocking;

    /** Prevents changing the plugins. */
    static volatile boolean lockdown;

    /**
     * If true, attempting to run a blockingX operation on a (by default)
     * computation or single scheduler will throw an IllegalStateException.
     */
    static volatile boolean failNonBlockingScheduler;

    /**
     * Prevents changing the plugins from then on.
     * 

This allows container-like environments to prevent clients * messing with plugins. */ public static void lockdown() { lockdown = true; } /** * Returns true if the plugins were locked down. * @return true if the plugins were locked down */ public static boolean isLockdown() { return lockdown; } /** * Enables or disables the blockingX operators to fail * with an IllegalStateException on a non-blocking * scheduler such as computation or single. *

History: 2.0.5 - experimental * @param enable enable or disable the feature * @since 2.1 */ public static void setFailOnNonBlockingScheduler(boolean enable) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } failNonBlockingScheduler = enable; } /** * Returns true if the blockingX operators fail * with an IllegalStateException on a non-blocking scheduler * such as computation or single. *

History: 2.0.5 - experimental * @return true if the blockingX operators fail on a non-blocking scheduler * @since 2.1 */ public static boolean isFailOnNonBlockingScheduler() { return failNonBlockingScheduler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getComputationSchedulerHandler() { return onComputationHandler; } /** * Returns the a hook consumer. * @return the hook consumer, may be null */ @Nullable public static Consumer getErrorHandler() { return errorHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function, ? extends Scheduler> getInitComputationSchedulerHandler() { return onInitComputationHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function, ? extends Scheduler> getInitIoSchedulerHandler() { return onInitIoHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function, ? extends Scheduler> getInitNewThreadSchedulerHandler() { return onInitNewThreadHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function, ? extends Scheduler> getInitSingleSchedulerHandler() { return onInitSingleHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getIoSchedulerHandler() { return onIoHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getNewThreadSchedulerHandler() { return onNewThreadHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getScheduleHandler() { return onScheduleHandler; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getSingleSchedulerHandler() { return onSingleHandler; } /** * Calls the associated hook function. * @param defaultScheduler a {@link Callable} which returns the hook's input value * @return the value returned by the hook, not null * @throws NullPointerException if the callable parameter or its result are null */ @NonNull public static Scheduler initComputationScheduler(@NonNull Callable defaultScheduler) { ObjectHelper.requireNonNull(defaultScheduler, "Scheduler Callable can't be null"); Function, ? extends Scheduler> f = onInitComputationHandler; if (f == null) { return callRequireNonNull(defaultScheduler); } return applyRequireNonNull(f, defaultScheduler); // JIT will skip this } /** * Calls the associated hook function. * @param defaultScheduler a {@link Callable} which returns the hook's input value * @return the value returned by the hook, not null * @throws NullPointerException if the callable parameter or its result are null */ @NonNull public static Scheduler initIoScheduler(@NonNull Callable defaultScheduler) { ObjectHelper.requireNonNull(defaultScheduler, "Scheduler Callable can't be null"); Function, ? extends Scheduler> f = onInitIoHandler; if (f == null) { return callRequireNonNull(defaultScheduler); } return applyRequireNonNull(f, defaultScheduler); } /** * Calls the associated hook function. * @param defaultScheduler a {@link Callable} which returns the hook's input value * @return the value returned by the hook, not null * @throws NullPointerException if the callable parameter or its result are null */ @NonNull public static Scheduler initNewThreadScheduler(@NonNull Callable defaultScheduler) { ObjectHelper.requireNonNull(defaultScheduler, "Scheduler Callable can't be null"); Function, ? extends Scheduler> f = onInitNewThreadHandler; if (f == null) { return callRequireNonNull(defaultScheduler); } return applyRequireNonNull(f, defaultScheduler); } /** * Calls the associated hook function. * @param defaultScheduler a {@link Callable} which returns the hook's input value * @return the value returned by the hook, not null * @throws NullPointerException if the callable parameter or its result are null */ @NonNull public static Scheduler initSingleScheduler(@NonNull Callable defaultScheduler) { ObjectHelper.requireNonNull(defaultScheduler, "Scheduler Callable can't be null"); Function, ? extends Scheduler> f = onInitSingleHandler; if (f == null) { return callRequireNonNull(defaultScheduler); } return applyRequireNonNull(f, defaultScheduler); } /** * Calls the associated hook function. * @param defaultScheduler the hook's input value * @return the value returned by the hook */ @NonNull public static Scheduler onComputationScheduler(@NonNull Scheduler defaultScheduler) { Function f = onComputationHandler; if (f == null) { return defaultScheduler; } return apply(f, defaultScheduler); } /** * Called when an undeliverable error occurs. *

* Undeliverable errors are those {@code Observer.onError()} invocations that are not allowed to happen on * the given consumer type ({@code Observer}, {@code Subscriber}, etc.) due to protocol restrictions * because the consumer has either disposed/cancelled its {@code Disposable}/{@code Subscription} or * has already terminated with an {@code onError()} or {@code onComplete()} signal. *

* By default, this global error handler prints the stacktrace via {@link Throwable#printStackTrace()} * and calls {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)} * on the current thread. *

* Note that on some platforms, the platform runtime terminates the current application with an error if such * uncaught exceptions happen. In this case, it is recommended the application installs a global error * handler via the {@link #setErrorHandler(Consumer)} plugin method. * * @param error the error to report * @see #getErrorHandler() * @see #setErrorHandler(Consumer) * @see Error handling Wiki */ public static void onError(@NonNull Throwable error) { Consumer f = errorHandler; if (error == null) { error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources."); } else { if (!isBug(error)) { error = new UndeliverableException(error); } } if (f != null) { try { f.accept(error); return; } catch (Throwable e) { // Exceptions.throwIfFatal(e); TODO decide e.printStackTrace(); // NOPMD uncaught(e); } } error.printStackTrace(); // NOPMD uncaught(error); } /** * Checks if the given error is one of the already named * bug cases that should pass through {@link #onError(Throwable)} * as is. * @param error the error to check * @return true if the error should pass through, false if * it may be wrapped into an UndeliverableException */ static boolean isBug(Throwable error) { // user forgot to add the onError handler in subscribe if (error instanceof OnErrorNotImplementedException) { return true; } // the sender didn't honor the request amount // it's either due to an operator bug or concurrent onNext if (error instanceof MissingBackpressureException) { return true; } // general protocol violations // it's either due to an operator bug or concurrent onNext if (error instanceof IllegalStateException) { return true; } // nulls are generally not allowed // likely an operator bug or missing null-check if (error instanceof NullPointerException) { return true; } // bad arguments, likely invalid user input if (error instanceof IllegalArgumentException) { return true; } // Crash while handling an exception if (error instanceof CompositeException) { return true; } // everything else is probably due to lifecycle limits return false; } static void uncaught(@NonNull Throwable error) { Thread currentThread = Thread.currentThread(); UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler(); handler.uncaughtException(currentThread, error); } /** * Calls the associated hook function. * @param defaultScheduler the hook's input value * @return the value returned by the hook */ @NonNull public static Scheduler onIoScheduler(@NonNull Scheduler defaultScheduler) { Function f = onIoHandler; if (f == null) { return defaultScheduler; } return apply(f, defaultScheduler); } /** * Calls the associated hook function. * @param defaultScheduler the hook's input value * @return the value returned by the hook */ @NonNull public static Scheduler onNewThreadScheduler(@NonNull Scheduler defaultScheduler) { Function f = onNewThreadHandler; if (f == null) { return defaultScheduler; } return apply(f, defaultScheduler); } /** * Called when a task is scheduled. * @param run the runnable instance * @return the replacement runnable */ @NonNull public static Runnable onSchedule(@NonNull Runnable run) { ObjectHelper.requireNonNull(run, "run is null"); Function f = onScheduleHandler; if (f == null) { return run; } return apply(f, run); } /** * Calls the associated hook function. * @param defaultScheduler the hook's input value * @return the value returned by the hook */ @NonNull public static Scheduler onSingleScheduler(@NonNull Scheduler defaultScheduler) { Function f = onSingleHandler; if (f == null) { return defaultScheduler; } return apply(f, defaultScheduler); } /** * Removes all handlers and resets to default behavior. */ public static void reset() { setErrorHandler(null); setScheduleHandler(null); setComputationSchedulerHandler(null); setInitComputationSchedulerHandler(null); setIoSchedulerHandler(null); setInitIoSchedulerHandler(null); setSingleSchedulerHandler(null); setInitSingleSchedulerHandler(null); setNewThreadSchedulerHandler(null); setInitNewThreadSchedulerHandler(null); setOnFlowableAssembly(null); setOnFlowableSubscribe(null); setOnObservableAssembly(null); setOnObservableSubscribe(null); setOnSingleAssembly(null); setOnSingleSubscribe(null); setOnCompletableAssembly(null); setOnCompletableSubscribe(null); setOnConnectableFlowableAssembly(null); setOnConnectableObservableAssembly(null); setOnMaybeAssembly(null); setOnMaybeSubscribe(null); setOnParallelAssembly(null); setFailOnNonBlockingScheduler(false); setOnBeforeBlocking(null); } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setComputationSchedulerHandler(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onComputationHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setErrorHandler(@Nullable Consumer handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } errorHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed, but the function may not return null */ public static void setInitComputationSchedulerHandler(@Nullable Function, ? extends Scheduler> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitComputationHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed, but the function may not return null */ public static void setInitIoSchedulerHandler(@Nullable Function, ? extends Scheduler> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitIoHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed, but the function may not return null */ public static void setInitNewThreadSchedulerHandler(@Nullable Function, ? extends Scheduler> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitNewThreadHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed, but the function may not return null */ public static void setInitSingleSchedulerHandler(@Nullable Function, ? extends Scheduler> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitSingleHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setIoSchedulerHandler(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onIoHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setNewThreadSchedulerHandler(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onNewThreadHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setScheduleHandler(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onScheduleHandler = handler; } /** * Sets the specific hook function. * @param handler the hook function to set, null allowed */ public static void setSingleSchedulerHandler(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onSingleHandler = handler; } /** * Revokes the lockdown, only for testing purposes. */ /* test. */static void unlock() { lockdown = false; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static Function getOnCompletableAssembly() { return onCompletableAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable public static BiFunction getOnCompletableSubscribe() { return onCompletableSubscribe; } /** * Returns the current hook function. * @return the hook function, may be null */ @SuppressWarnings("rawtypes") @Nullable public static Function getOnFlowableAssembly() { return onFlowableAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @SuppressWarnings("rawtypes") @Nullable public static Function getOnConnectableFlowableAssembly() { return onConnectableFlowableAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static BiFunction getOnFlowableSubscribe() { return onFlowableSubscribe; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static BiFunction getOnMaybeSubscribe() { return onMaybeSubscribe; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static Function getOnMaybeAssembly() { return onMaybeAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static Function getOnSingleAssembly() { return onSingleAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static BiFunction getOnSingleSubscribe() { return onSingleSubscribe; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static Function getOnObservableAssembly() { return onObservableAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static Function getOnConnectableObservableAssembly() { return onConnectableObservableAssembly; } /** * Returns the current hook function. * @return the hook function, may be null */ @Nullable @SuppressWarnings("rawtypes") public static BiFunction getOnObservableSubscribe() { return onObservableSubscribe; } /** * Sets the specific hook function. * @param onCompletableAssembly the hook function to set, null allowed */ public static void setOnCompletableAssembly(@Nullable Function onCompletableAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onCompletableAssembly = onCompletableAssembly; } /** * Sets the specific hook function. * @param onCompletableSubscribe the hook function to set, null allowed */ public static void setOnCompletableSubscribe( @Nullable BiFunction onCompletableSubscribe) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onCompletableSubscribe = onCompletableSubscribe; } /** * Sets the specific hook function. * @param onFlowableAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnFlowableAssembly(@Nullable Function onFlowableAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onFlowableAssembly = onFlowableAssembly; } /** * Sets the specific hook function. * @param onMaybeAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnMaybeAssembly(@Nullable Function onMaybeAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onMaybeAssembly = onMaybeAssembly; } /** * Sets the specific hook function. * @param onConnectableFlowableAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnConnectableFlowableAssembly(@Nullable Function onConnectableFlowableAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onConnectableFlowableAssembly = onConnectableFlowableAssembly; } /** * Sets the specific hook function. * @param onFlowableSubscribe the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnFlowableSubscribe(@Nullable BiFunction onFlowableSubscribe) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onFlowableSubscribe = onFlowableSubscribe; } /** * Sets the specific hook function. * @param onMaybeSubscribe the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnMaybeSubscribe(@Nullable BiFunction onMaybeSubscribe) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onMaybeSubscribe = onMaybeSubscribe; } /** * Sets the specific hook function. * @param onObservableAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnObservableAssembly(@Nullable Function onObservableAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onObservableAssembly = onObservableAssembly; } /** * Sets the specific hook function. * @param onConnectableObservableAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnConnectableObservableAssembly(@Nullable Function onConnectableObservableAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onConnectableObservableAssembly = onConnectableObservableAssembly; } /** * Sets the specific hook function. * @param onObservableSubscribe the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnObservableSubscribe( @Nullable BiFunction onObservableSubscribe) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onObservableSubscribe = onObservableSubscribe; } /** * Sets the specific hook function. * @param onSingleAssembly the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnSingleAssembly(@Nullable Function onSingleAssembly) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onSingleAssembly = onSingleAssembly; } /** * Sets the specific hook function. * @param onSingleSubscribe the hook function to set, null allowed */ @SuppressWarnings("rawtypes") public static void setOnSingleSubscribe(@Nullable BiFunction onSingleSubscribe) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } RxJavaPlugins.onSingleSubscribe = onSingleSubscribe; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @param subscriber the subscriber * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Subscriber onSubscribe(@NonNull Flowable source, @NonNull Subscriber subscriber) { BiFunction f = onFlowableSubscribe; if (f != null) { return apply(f, source, subscriber); } return subscriber; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @param observer the observer * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Observer onSubscribe(@NonNull Observable source, @NonNull Observer observer) { BiFunction f = onObservableSubscribe; if (f != null) { return apply(f, source, observer); } return observer; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @param observer the observer * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static SingleObserver onSubscribe(@NonNull Single source, @NonNull SingleObserver observer) { BiFunction f = onSingleSubscribe; if (f != null) { return apply(f, source, observer); } return observer; } /** * Calls the associated hook function. * @param source the hook's input value * @param observer the observer * @return the value returned by the hook */ @NonNull public static CompletableObserver onSubscribe(@NonNull Completable source, @NonNull CompletableObserver observer) { BiFunction f = onCompletableSubscribe; if (f != null) { return apply(f, source, observer); } return observer; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @param observer the subscriber * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static MaybeObserver onSubscribe(@NonNull Maybe source, @NonNull MaybeObserver observer) { BiFunction f = onMaybeSubscribe; if (f != null) { return apply(f, source, observer); } return observer; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Maybe onAssembly(@NonNull Maybe source) { Function f = onMaybeAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Flowable onAssembly(@NonNull Flowable source) { Function f = onFlowableAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static ConnectableFlowable onAssembly(@NonNull ConnectableFlowable source) { Function f = onConnectableFlowableAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Observable onAssembly(@NonNull Observable source) { Function f = onObservableAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static ConnectableObservable onAssembly(@NonNull ConnectableObservable source) { Function f = onConnectableObservableAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param the value type * @param source the hook's input value * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static Single onAssembly(@NonNull Single source) { Function f = onSingleAssembly; if (f != null) { return apply(f, source); } return source; } /** * Calls the associated hook function. * @param source the hook's input value * @return the value returned by the hook */ @NonNull public static Completable onAssembly(@NonNull Completable source) { Function f = onCompletableAssembly; if (f != null) { return apply(f, source); } return source; } /** * Sets the specific hook function. *

History: 2.0.6 - experimental; 2.1 - beta * @param handler the hook function to set, null allowed * @since 2.2 */ @SuppressWarnings("rawtypes") public static void setOnParallelAssembly(@Nullable Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onParallelAssembly = handler; } /** * Returns the current hook function. *

History: 2.0.6 - experimental; 2.1 - beta * @return the hook function, may be null * @since 2.2 */ @SuppressWarnings("rawtypes") @Nullable public static Function getOnParallelAssembly() { return onParallelAssembly; } /** * Calls the associated hook function. *

History: 2.0.6 - experimental; 2.1 - beta * @param the value type of the source * @param source the hook's input value * @return the value returned by the hook * @since 2.2 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static ParallelFlowable onAssembly(@NonNull ParallelFlowable source) { Function f = onParallelAssembly; if (f != null) { return apply(f, source); } return source; } /** * Called before an operator attempts a blocking operation * such as awaiting a condition or signal * and should return true to indicate the operator * should not block but throw an IllegalArgumentException. *

History: 2.0.5 - experimental * @return true if the blocking should be prevented * @see #setFailOnNonBlockingScheduler(boolean) * @since 2.1 */ public static boolean onBeforeBlocking() { BooleanSupplier f = onBeforeBlocking; if (f != null) { try { return f.getAsBoolean(); } catch (Throwable ex) { throw ExceptionHelper.wrapOrThrow(ex); } } return false; } /** * Set the handler that is called when an operator attempts a blocking * await; the handler should return true to prevent the blocking * and to signal an IllegalStateException instead. *

History: 2.0.5 - experimental * @param handler the handler to set, null resets to the default handler * that always returns false * @see #onBeforeBlocking() * @since 2.1 */ public static void setOnBeforeBlocking(@Nullable BooleanSupplier handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onBeforeBlocking = handler; } /** * Returns the current blocking handler or null if no custom handler * is set. *

History: 2.0.5 - experimental * @return the current blocking handler or null if not specified * @since 2.1 */ @Nullable public static BooleanSupplier getOnBeforeBlocking() { return onBeforeBlocking; } /** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#computation()} * except using {@code threadFactory} for thread creation. *

History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createComputationScheduler(@NonNull ThreadFactory threadFactory) { return new ComputationScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); } /** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#io()} * except using {@code threadFactory} for thread creation. *

History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createIoScheduler(@NonNull ThreadFactory threadFactory) { return new IoScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); } /** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#newThread()} * except using {@code threadFactory} for thread creation. *

History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createNewThreadScheduler(@NonNull ThreadFactory threadFactory) { return new NewThreadScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); } /** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#single()} * except using {@code threadFactory} for thread creation. *

History: 2.0.5 - experimental * @param threadFactory thread factory to use for creating worker threads. Note that this takes precedence over any * system properties for configuring new thread creation. Cannot be null. * @return the created Scheduler instance * @since 2.1 */ @NonNull public static Scheduler createSingleScheduler(@NonNull ThreadFactory threadFactory) { return new SingleScheduler(ObjectHelper.requireNonNull(threadFactory, "threadFactory is null")); } /** * Wraps the call to the function in try-catch and propagates thrown * checked exceptions as RuntimeException. * @param the input type * @param the output type * @param f the function to call, not null (not verified) * @param t the parameter value to the function * @return the result of the function call */ @NonNull static R apply(@NonNull Function f, @NonNull T t) { try { return f.apply(t); } catch (Throwable ex) { throw ExceptionHelper.wrapOrThrow(ex); } } /** * Wraps the call to the function in try-catch and propagates thrown * checked exceptions as RuntimeException. * @param the first input type * @param the second input type * @param the output type * @param f the function to call, not null (not verified) * @param t the first parameter value to the function * @param u the second parameter value to the function * @return the result of the function call */ @NonNull static R apply(@NonNull BiFunction f, @NonNull T t, @NonNull U u) { try { return f.apply(t, u); } catch (Throwable ex) { throw ExceptionHelper.wrapOrThrow(ex); } } /** * Wraps the call to the Scheduler creation callable in try-catch and propagates thrown * checked exceptions as RuntimeException and enforces that result is not null. * @param s the {@link Callable} which returns a {@link Scheduler}, not null (not verified). Cannot return null * @return the result of the callable call, not null * @throws NullPointerException if the callable parameter returns null */ @NonNull static Scheduler callRequireNonNull(@NonNull Callable s) { try { return ObjectHelper.requireNonNull(s.call(), "Scheduler Callable result can't be null"); } catch (Throwable ex) { throw ExceptionHelper.wrapOrThrow(ex); } } /** * Wraps the call to the Scheduler creation function in try-catch and propagates thrown * checked exceptions as RuntimeException and enforces that result is not null. * @param f the function to call, not null (not verified). Cannot return null * @param s the parameter value to the function * @return the result of the function call, not null * @throws NullPointerException if the function parameter returns null */ @NonNull static Scheduler applyRequireNonNull(@NonNull Function, ? extends Scheduler> f, Callable s) { return ObjectHelper.requireNonNull(apply(f, s), "Scheduler Callable result can't be null"); } /** Helper class, no instances. */ private RxJavaPlugins() { throw new IllegalStateException("No instances!"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy