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

io.reactivex.flowable.processors.PublishProcessor 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.flowable.processors;

import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

import io.reactivex.common.RxJavaCommonPlugins;
import io.reactivex.common.annotations.*;
import io.reactivex.common.exceptions.MissingBackpressureException;
import io.reactivex.flowable.internal.subscriptions.SubscriptionHelper;
import io.reactivex.flowable.internal.utils.BackpressureHelper;

/**
 * A Subject that multicasts events to Subscribers that are currently subscribed to it.
 *
 * 

* * *

The subject does not coordinate backpressure for its subscribers and implements a weaker onSubscribe which * calls requests Long.MAX_VALUE from the incoming Subscriptions. This makes it possible to subscribe the PublishSubject * to multiple sources (note on serialization though) unlike the standard contract on Subscriber. Child subscribers, however, are not overflown but receive an * IllegalStateException in case their requested amount is zero. * *

The implementation of onXXX methods are technically thread-safe but non-serialized calls * to them may lead to undefined state in the currently subscribed Subscribers. * *

Due to the nature Flowables are constructed, the PublishProcessor can't be instantiated through * {@code new} but must be created via the {@link #create()} method. * * Example usage: *

*

 {@code

  PublishProcessor processor = PublishProcessor.create();
  // subscriber1 will receive all onNext and onComplete events
  processor.subscribe(subscriber1);
  processor.onNext("one");
  processor.onNext("two");
  // subscriber2 will only receive "three" and onComplete
  processor.subscribe(subscriber2);
  processor.onNext("three");
  processor.onComplete();

  } 
 * @param  the value type multicast to Subscribers.
 */
public final class PublishProcessor extends FlowableProcessor {
    /** The terminated indicator for the subscribers array. */
    @SuppressWarnings("rawtypes")
    static final PublishSubscription[] TERMINATED = new PublishSubscription[0];
    /** An empty subscribers array to avoid allocating it all the time. */
    @SuppressWarnings("rawtypes")
    static final PublishSubscription[] EMPTY = new PublishSubscription[0];

    /** The array of currently subscribed subscribers. */
    final AtomicReference[]> subscribers;

    /** The error, write before terminating and read after checking subscribers. */
    Throwable error;

    /**
     * Constructs a PublishProcessor.
     * @param  the value type
     * @return the new PublishProcessor
     */
    @CheckReturnValue
    public static  PublishProcessor create() {
        return new PublishProcessor();
    }

    /**
     * Constructs a PublishProcessor.
     * @since 2.0
     */
    @SuppressWarnings("unchecked")
    PublishProcessor() {
        subscribers = new AtomicReference[]>(EMPTY);
    }


    @Override
    public void subscribeActual(Subscriber t) {
        PublishSubscription ps = new PublishSubscription(t, this);
        t.onSubscribe(ps);
        if (add(ps)) {
            // if cancellation happened while a successful add, the remove() didn't work
            // so we need to do it again
            if (ps.isCancelled()) {
                remove(ps);
            }
        } else {
            Throwable ex = error;
            if (ex != null) {
                t.onError(ex);
            } else {
                t.onComplete();
            }
        }
    }

    /**
     * Tries to add the given subscriber to the subscribers array atomically
     * or returns false if the subject has terminated.
     * @param ps the subscriber to add
     * @return true if successful, false if the subject has terminated
     */
    boolean add(PublishSubscription ps) {
        for (;;) {
            PublishSubscription[] a = subscribers.get();
            if (a == TERMINATED) {
                return false;
            }

            int n = a.length;
            @SuppressWarnings("unchecked")
            PublishSubscription[] b = new PublishSubscription[n + 1];
            System.arraycopy(a, 0, b, 0, n);
            b[n] = ps;

            if (subscribers.compareAndSet(a, b)) {
                return true;
            }
        }
    }

    /**
     * Atomically removes the given subscriber if it is subscribed to the subject.
     * @param ps the subject to remove
     */
    @SuppressWarnings("unchecked")
    void remove(PublishSubscription ps) {
        for (;;) {
            PublishSubscription[] a = subscribers.get();
            if (a == TERMINATED || a == EMPTY) {
                return;
            }

            int n = a.length;
            int j = -1;
            for (int i = 0; i < n; i++) {
                if (a[i] == ps) {
                    j = i;
                    break;
                }
            }

            if (j < 0) {
                return;
            }

            PublishSubscription[] b;

            if (n == 1) {
                b = EMPTY;
            } else {
                b = new PublishSubscription[n - 1];
                System.arraycopy(a, 0, b, 0, j);
                System.arraycopy(a, j + 1, b, j, n - j - 1);
            }
            if (subscribers.compareAndSet(a, b)) {
                return;
            }
        }
    }

    @Override
    public void onSubscribe(Subscription s) {
        if (subscribers.get() == TERMINATED) {
            s.cancel();
            return;
        }
        // PublishSubject doesn't bother with request coordination.
        s.request(Long.MAX_VALUE);
    }

    @Override
    public void onNext(T t) {
        if (subscribers.get() == TERMINATED) {
            return;
        }
        if (t == null) {
            onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
            return;
        }
        for (PublishSubscription s : subscribers.get()) {
            s.onNext(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onError(Throwable t) {
        if (subscribers.get() == TERMINATED) {
            RxJavaCommonPlugins.onError(t);
            return;
        }
        if (t == null) {
            t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
        }
        error = t;

        for (PublishSubscription s : subscribers.getAndSet(TERMINATED)) {
            s.onError(t);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onComplete() {
        if (subscribers.get() == TERMINATED) {
            return;
        }
        for (PublishSubscription s : subscribers.getAndSet(TERMINATED)) {
            s.onComplete();
        }
    }

    /**
     * Tries to emit the item to all currently subscribed Subscribers if all of them
     * has requested some value, returns false otherwise.
     * 

* This method should be called in a sequential manner just like the onXXX methods * of the PublishProcessor. *

* Calling with null will terminate the PublishProcessor and a NullPointerException * is signalled to the Subscribers. * @param t the item to emit, not null * @return true if the item was emitted to all Subscribers * @since 2.0.8 - experimental */ @Experimental public boolean offer(T t) { if (t == null) { onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources.")); return true; } PublishSubscription[] array = subscribers.get(); for (PublishSubscription s : array) { if (s.isFull()) { return false; } } for (PublishSubscription s : array) { s.onNext(t); } return true; } @Override public boolean hasSubscribers() { return subscribers.get().length != 0; } @Override public Throwable getThrowable() { if (subscribers.get() == TERMINATED) { return error; } return null; } @Override public boolean hasThrowable() { return subscribers.get() == TERMINATED && error != null; } @Override public boolean hasComplete() { return subscribers.get() == TERMINATED && error == null; } /** * Wraps the actual subscriber, tracks its requests and makes cancellation * to remove itself from the current subscribers array. * * @param the value type */ static final class PublishSubscription extends AtomicLong implements Subscription { private static final long serialVersionUID = 3562861878281475070L; /** The actual subscriber. */ final Subscriber actual; /** The subject state. */ final PublishProcessor parent; /** * Constructs a PublishSubscriber, wraps the actual subscriber and the state. * @param actual the actual subscriber * @param parent the parent PublishProcessor */ PublishSubscription(Subscriber actual, PublishProcessor parent) { this.actual = actual; this.parent = parent; } public void onNext(T t) { long r = get(); if (r == Long.MIN_VALUE) { return; } if (r != 0L) { actual.onNext(t); if (r != Long.MAX_VALUE) { decrementAndGet(); } } else { cancel(); actual.onError(new MissingBackpressureException("Could not emit value due to lack of requests")); } } public void onError(Throwable t) { if (get() != Long.MIN_VALUE) { actual.onError(t); } else { RxJavaCommonPlugins.onError(t); } } public void onComplete() { if (get() != Long.MIN_VALUE) { actual.onComplete(); } } @Override public void request(long n) { if (SubscriptionHelper.validate(n)) { BackpressureHelper.addCancel(this, n); } } @Override public void cancel() { if (getAndSet(Long.MIN_VALUE) != Long.MIN_VALUE) { parent.remove(this); } } public boolean isCancelled() { return get() == Long.MIN_VALUE; } boolean isFull() { return get() == 0L; } } }