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

hu.akarnokd.rxjava2.plugins.RxJavaPlugins Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and Netflix, Inc.
 * 
 * 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 hu.akarnokd.rxjava2.plugins;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.NbpObservable.NbpSubscriber;
import hu.akarnokd.rxjava2.Scheduler;
import hu.akarnokd.rxjava2.functions.*;

/**
 * Utility class to inject handlers to certain standard RxJava operations.
 */
public final class RxJavaPlugins {
    
    static volatile Consumer errorHandler;
    
    static volatile Function, Subscriber> onSubscribeHandler;

    static volatile Function, NbpSubscriber> onNbpSubscribeHandler;

    static volatile Function, Publisher> onCreateHandler;

    static volatile Function onScheduleHandler;

    static volatile Function onInitComputationHandler;
    
    static volatile Function onInitSingleHandler;
    
    static volatile Function onInitIOHandler;

    static volatile Function onInitNewThreadHandler;
    
    static volatile Function onComputationHandler;
    
    static volatile Function onSingleHandler;
    
    static volatile Function onIOHandler;

    static volatile Function onNewThreadHandler;

    /** Prevents changing the plugins. */
    private 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; } public static Function getComputationSchedulerHandler() { return onComputationHandler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static Function, Publisher> getCreateHandler() { return (Function)onCreateHandler; } public static Consumer getErrorHandler() { return errorHandler; } public static Function getInitComputationSchedulerHandler() { return onInitComputationHandler; } public static Function getInitIOSchedulerHandler() { return onInitIOHandler; } public static Function getInitNewThreadSchedulerHandler() { return onInitNewThreadHandler; } public static Function getInitSingleSchedulerHandler() { return onInitSingleHandler; } public static Function getIOSchedulerHandler() { return onIOHandler; } public static Function getNewThreadSchedulerHandler() { return onNewThreadHandler; } public static Function getScheduleHandler() { return onScheduleHandler; } public static Function getSingleSchedulerHandler() { return onSingleHandler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static Function, Subscriber> getSubscribeHandler() { return (Function)onSubscribeHandler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static Function, NbpSubscriber> getNbpSubscribeHandler() { return (Function)onNbpSubscribeHandler; } public static Scheduler initComputationScheduler(Scheduler defaultScheduler) { Function f = onInitComputationHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); // JIT will skip this } public static Scheduler initIOScheduler(Scheduler defaultScheduler) { Function f = onInitIOHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } public static Scheduler initNewThreadScheduler(Scheduler defaultScheduler) { Function f = onInitNewThreadHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } public static Scheduler initSingleScheduler(Scheduler defaultScheduler) { Function f = onInitSingleHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } public static Scheduler onComputationScheduler(Scheduler defaultScheduler) { Function f = onComputationHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } /** * Called when an Observable is created. * @param the value type * @param publisher the original publisher * @return the replacement publisher */ @SuppressWarnings({ "unchecked", "rawtypes"}) public static Publisher onCreate(Publisher publisher) { Function, Publisher> f = onCreateHandler; if (f == null) { return publisher; } return (Publisher)((Function)f).apply(publisher); } /** * Called when an undeliverable error occurs. * @param error the error to report */ public static void onError(Throwable error) { Consumer f = errorHandler; if (f != null) { try { f.accept(error); return; } catch (Throwable e) { if (error == null) { error = new NullPointerException(); } e.printStackTrace(); } } else { if (error == null) { error = new NullPointerException(); } } error.printStackTrace(); } public static Scheduler onIOScheduler(Scheduler defaultScheduler) { Function f = onIOHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } public static Scheduler onNewThreadScheduler(Scheduler defaultScheduler) { Function f = onNewThreadHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } /** * Called when a task is scheduled. * @param run the runnable instance * @return the replacement runnable */ public static Runnable onSchedule(Runnable run) { Function f = onScheduleHandler; if (f == null) { return run; } return f.apply(run); } public static Scheduler onSingleScheduler(Scheduler defaultScheduler) { Function f = onSingleHandler; if (f == null) { return defaultScheduler; } return f.apply(defaultScheduler); } /** * Called when a subscriber subscribes to an observable. * @param the value type * @param subscriber the original subscriber * @return the subscriber replacement */ @SuppressWarnings({ "unchecked", "rawtypes"}) public static Subscriber onSubscribe(Subscriber subscriber) { Function, Subscriber> f = onSubscribeHandler; if (f == null) { return subscriber; } return (Subscriber)((Function)f).apply(subscriber); } /** * Called when a subscriber subscribes to an observable. * @param the value type * @param subscriber the original NbpSubscriber * @return the replacement NbpSubscriber */ @SuppressWarnings({ "unchecked", "rawtypes"}) public static NbpSubscriber onNbpSubscribe(NbpSubscriber subscriber) { Function, NbpSubscriber> f = onNbpSubscribeHandler; if (f == null) { return subscriber; } return (NbpSubscriber)((Function)f).apply(subscriber); } /** * Called when a subscriber subscribes to an observable. * @param the value type * @param subscriber the original subscriber * @return the replacement subscriber */ @SuppressWarnings({ "unchecked", "rawtypes"}) public static NbpSubscriber onSubscribe(NbpSubscriber subscriber) { Function, NbpSubscriber> f = onNbpSubscribeHandler; if (f == null) { return subscriber; } return (NbpSubscriber)((Function)f).apply(subscriber); } /** * Removes all handlers and resets the default behavior. */ public static void reset() { setCreateHandler(null); setErrorHandler(null); setScheduleHandler(null); setSubscribeHandler(null); setComputationSchedulerHandler(null); setInitComputationSchedulerHandler(null); setIOSchedulerHandler(null); setInitIOSchedulerHandler(null); setSingleSchedulerHandler(null); setInitSingleSchedulerHandler(null); setNewThreadSchedulerHandler(null); setInitNewThreadSchedulerHandler(null); } public static void setComputationSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onComputationHandler = handler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void setCreateHandler(Function, Publisher> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onCreateHandler = (Function)handler; } public static void setErrorHandler(Consumer handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } errorHandler = handler; } public static void setInitComputationSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitComputationHandler = handler; } public static void setInitIOSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitIOHandler = handler; } public static void setInitNewThreadSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitNewThreadHandler = handler; } public static void setInitSingleSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onInitSingleHandler = handler; } public static void setIOSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onIOHandler = handler; } public static void setNewThreadSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onNewThreadHandler = handler; } public static void setScheduleHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onScheduleHandler = handler; } public static void setSingleSchedulerHandler(Function handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onSingleHandler = handler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void setSubscribeHandler(Function, Subscriber> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onSubscribeHandler = (Function)handler; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void setNbpSubscribeHandler(Function, NbpSubscriber> handler) { if (lockdown) { throw new IllegalStateException("Plugins can't be changed anymore"); } onNbpSubscribeHandler = (Function)handler; } /** * Rewokes the lockdown, only for testing purposes. */ /* test. */static void unlock() { lockdown = false; } /** Singleton consumer that calls RxJavaPlugins.onError. */ static final Consumer CONSUME_BY_RXJAVA_PLUGIN = new Consumer() { @Override public void accept(Throwable e) { RxJavaPlugins.onError(e); } }; /** * Returns a consumer which relays the received Throwable to RxJavaPlugins.onError(). * @return the consumer */ public static final Consumer errorConsumer() { return CONSUME_BY_RXJAVA_PLUGIN; } /** Helper class, no instances. */ private RxJavaPlugins() { throw new IllegalStateException("No instances!"); } }