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

io.reactivex.observable.internal.operators.ObservableSwitchMap 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.internal.operators;

import java.util.concurrent.atomic.*;

import io.reactivex.common.*;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.Function;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.common.internal.utils.AtomicThrowable;
import io.reactivex.observable.*;
import io.reactivex.observable.internal.queues.SpscLinkedArrayQueue;

public final class ObservableSwitchMap extends AbstractObservableWithUpstream {
    final Function> mapper;
    final int bufferSize;

    final boolean delayErrors;

    public ObservableSwitchMap(ObservableSource source,
                               Function> mapper, int bufferSize,
                                       boolean delayErrors) {
        super(source);
        this.mapper = mapper;
        this.bufferSize = bufferSize;
        this.delayErrors = delayErrors;
    }

    @Override
    public void subscribeActual(Observer t) {

        if (ObservableScalarXMap.tryScalarXMapSubscribe(source, t, mapper)) {
            return;
        }

        source.subscribe(new SwitchMapObserver(t, mapper, bufferSize, delayErrors));
    }

    static final class SwitchMapObserver extends AtomicInteger implements Observer, Disposable {

        private static final long serialVersionUID = -3491074160481096299L;
        final Observer actual;
        final Function> mapper;
        final int bufferSize;

        final boolean delayErrors;

        final AtomicThrowable errors;

        volatile boolean done;

        volatile boolean cancelled;

        Disposable s;

        final AtomicReference> active = new AtomicReference>();

        static final SwitchMapInnerObserver CANCELLED;
        static {
            CANCELLED = new SwitchMapInnerObserver(null, -1L, 1);
            CANCELLED.cancel();
        }

        volatile long unique;

        SwitchMapObserver(Observer actual,
                Function> mapper, int bufferSize,
                        boolean delayErrors) {
            this.actual = actual;
            this.mapper = mapper;
            this.bufferSize = bufferSize;
            this.delayErrors = delayErrors;
            this.errors = new AtomicThrowable();
        }

        @Override
        public void onSubscribe(Disposable s) {
            if (DisposableHelper.validate(this.s, s)) {
                this.s = s;
                actual.onSubscribe(this);
            }
        }

        @Override
        public void onNext(T t) {
            long c = unique + 1;
            unique = c;

            SwitchMapInnerObserver inner = active.get();
            if (inner != null) {
                inner.cancel();
            }

            ObservableSource p;
            try {
                p = ObjectHelper.requireNonNull(mapper.apply(t), "The ObservableSource returned is null");
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                s.dispose();
                onError(e);
                return;
            }

            SwitchMapInnerObserver nextInner = new SwitchMapInnerObserver(this, c, bufferSize);

            for (;;) {
                inner = active.get();
                if (inner == CANCELLED) {
                    break;
                }
                if (active.compareAndSet(inner, nextInner)) {
                    p.subscribe(nextInner);
                    break;
                }
            }
        }

        @Override
        public void onError(Throwable t) {
            if (done || !errors.addThrowable(t)) {
                if (!delayErrors) {
                    disposeInner();
                }
                RxJavaCommonPlugins.onError(t);
                return;
            }
            done = true;
            drain();
        }

        @Override
        public void onComplete() {
            if (!done) {
                done = true;
                drain();
            }
        }

        @Override
        public void dispose() {
            if (!cancelled) {
                cancelled = true;
                s.dispose();
                disposeInner();
            }
        }

        @Override
        public boolean isDisposed() {
            return cancelled;
        }

        @SuppressWarnings("unchecked")
        void disposeInner() {
            SwitchMapInnerObserver a = active.get();
            if (a != CANCELLED) {
                a = active.getAndSet((SwitchMapInnerObserver)CANCELLED);
                if (a != CANCELLED && a != null) {
                    a.cancel();
                }
            }
        }

        void drain() {
            if (getAndIncrement() != 0) {
                return;
            }

            final Observer a = actual;

            int missing = 1;

            for (;;) {

                if (cancelled) {
                    return;
                }

                if (done) {
                    boolean empty = active.get() == null;
                    if (delayErrors) {
                        if (empty) {
                            Throwable ex = errors.get();
                            if (ex != null) {
                                a.onError(ex);
                            } else {
                                a.onComplete();
                            }
                            return;
                        }
                    } else {
                        Throwable ex = errors.get();
                        if (ex != null) {
                            a.onError(errors.terminate());
                            return;
                        }
                        if (empty) {
                            a.onComplete();
                            return;
                        }
                    }
                }

                SwitchMapInnerObserver inner = active.get();

                if (inner != null) {
                    SpscLinkedArrayQueue q = inner.queue;

                    if (inner.done) {
                        boolean empty = q.isEmpty();
                        if (delayErrors) {
                            if (empty) {
                                active.compareAndSet(inner, null);
                                continue;
                            }
                        } else {
                            Throwable ex = errors.get();
                            if (ex != null) {
                                a.onError(errors.terminate());
                                return;
                            }
                            if (empty) {
                                active.compareAndSet(inner, null);
                                continue;
                            }
                        }
                    }

                    boolean retry = false;

                    for (;;) {
                        if (cancelled) {
                            return;
                        }
                        if (inner != active.get()) {
                            retry = true;
                            break;
                        }

                        if (!delayErrors) {
                            Throwable ex = errors.get();
                            if (ex != null) {
                                a.onError(errors.terminate());
                                return;
                            }
                        }

                        boolean d = inner.done;
                        R v = q.poll();
                        boolean empty = v == null;

                        if (d && empty) {
                            active.compareAndSet(inner, null);
                            retry = true;
                            break;
                        }

                        if (empty) {
                            break;
                        }

                        a.onNext(v);
                    }

                    if (retry) {
                        continue;
                    }
                }

                missing = addAndGet(-missing);
                if (missing == 0) {
                    break;
                }
            }
        }

        void innerError(SwitchMapInnerObserver inner, Throwable ex) {
            if (inner.index == unique && errors.addThrowable(ex)) {
                if (!delayErrors) {
                    s.dispose();
                }
                inner.done = true;
                drain();
            } else {
                RxJavaCommonPlugins.onError(ex);
            }
        }
    }

    static final class SwitchMapInnerObserver extends AtomicReference implements Observer {

        private static final long serialVersionUID = 3837284832786408377L;
        final SwitchMapObserver parent;
        final long index;
        final SpscLinkedArrayQueue queue;

        volatile boolean done;

        SwitchMapInnerObserver(SwitchMapObserver parent, long index, int bufferSize) {
            this.parent = parent;
            this.index = index;
            this.queue = new SpscLinkedArrayQueue(bufferSize);
        }

        @Override
        public void onSubscribe(Disposable s) {
            DisposableHelper.setOnce(this, s);
        }

        @Override
        public void onNext(R t) {
            if (index == parent.unique) {
                queue.offer(t);
                parent.drain();
            }
        }

        @Override
        public void onError(Throwable t) {
            parent.innerError(this, t);
        }

        @Override
        public void onComplete() {
            if (index == parent.unique) {
                done = true;
                parent.drain();
            }
        }

        public void cancel() {
            DisposableHelper.dispose(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy