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

io.reactivex.observable.RxJavaObservablePlugins 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.observable;

import java.util.concurrent.*;

import io.reactivex.common.*;
import io.reactivex.common.annotations.*;
import io.reactivex.common.functions.*;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.common.internal.schedulers.*;
import io.reactivex.common.internal.utils.ExceptionHelper;
/**
 * Utility class to inject handlers to certain standard RxJava operations.
 */
public final class RxJavaObservablePlugins {

    @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;

    static volatile Function onCompletableAssembly;

    @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;

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

    /**
     * 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; } /** * Removes all handlers and resets to default behavior. */ public static void reset() { setOnObservableAssembly(null); setOnObservableSubscribe(null); setOnSingleAssembly(null); setOnSingleSubscribe(null); setOnCompletableAssembly(null); setOnCompletableSubscribe(null); setOnConnectableObservableAssembly(null); setOnMaybeAssembly(null); setOnMaybeSubscribe(null); } /** * 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 */ @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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.onCompletableSubscribe = onCompletableSubscribe; } /** * 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"); } RxJavaObservablePlugins.onMaybeAssembly = onMaybeAssembly; } /** * 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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.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"); } RxJavaObservablePlugins.onSingleSubscribe = onSingleSubscribe; } /** * 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 subscriber the subscriber * @return the value returned by the hook */ @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull public static MaybeObserver onSubscribe(@NonNull Maybe source, @NonNull MaybeObserver subscriber) { BiFunction f = onMaybeSubscribe; 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 * @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 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; } /** * Create an instance of the default {@link Scheduler} used for {@link Schedulers#computation()} * except using {@code threadFactory} for thread creation. * @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.0.5 - experimental */ @Experimental @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. * @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.0.5 - experimental */ @Experimental @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. * @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.0.5 - experimental */ @Experimental @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. * @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.0.5 - experimental */ @Experimental @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 RxJavaObservablePlugins() { throw new IllegalStateException("No instances!"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy