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

io.reactivex.observable.internal.operators.ObservableCombineLatest 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.Arrays;
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.disposables.EmptyDisposable;
import io.reactivex.observable.internal.queues.SpscLinkedArrayQueue;

public final class ObservableCombineLatest extends Observable {
    final ObservableSource[] sources;
    final Iterable> sourcesIterable;
    final Function combiner;
    final int bufferSize;
    final boolean delayError;

    public ObservableCombineLatest(ObservableSource[] sources,
            Iterable> sourcesIterable,
            Function combiner, int bufferSize,
            boolean delayError) {
        this.sources = sources;
        this.sourcesIterable = sourcesIterable;
        this.combiner = combiner;
        this.bufferSize = bufferSize;
        this.delayError = delayError;
    }


    @Override
    @SuppressWarnings("unchecked")
    public void subscribeActual(Observer s) {
        ObservableSource[] sources = this.sources;
        int count = 0;
        if (sources == null) {
            sources = new Observable[8];
            for (ObservableSource p : sourcesIterable) {
                if (count == sources.length) {
                    ObservableSource[] b = new ObservableSource[count + (count >> 2)];
                    System.arraycopy(sources, 0, b, 0, count);
                    sources = b;
                }
                sources[count++] = p;
            }
        } else {
            count = sources.length;
        }

        if (count == 0) {
            EmptyDisposable.complete(s);
            return;
        }

        LatestCoordinator lc = new LatestCoordinator(s, combiner, count, bufferSize, delayError);
        lc.subscribe(sources);
    }

    static final class LatestCoordinator extends AtomicInteger implements Disposable {

        private static final long serialVersionUID = 8567835998786448817L;
        final Observer actual;
        final Function combiner;
        final CombinerObserver[] observers;
        final T[] latest;
        final SpscLinkedArrayQueue queue;
        final boolean delayError;

        volatile boolean cancelled;

        volatile boolean done;

        final AtomicThrowable errors = new AtomicThrowable();

        int active;
        int complete;

        @SuppressWarnings("unchecked")
        LatestCoordinator(Observer actual,
                Function combiner,
                int count, int bufferSize, boolean delayError) {
            this.actual = actual;
            this.combiner = combiner;
            this.delayError = delayError;
            this.latest = (T[])new Object[count];
            this.observers = new CombinerObserver[count];
            this.queue = new SpscLinkedArrayQueue(bufferSize);
        }

        public void subscribe(ObservableSource[] sources) {
            Observer[] as = observers;
            int len = as.length;
            for (int i = 0; i < len; i++) {
                as[i] = new CombinerObserver(this, i);
            }
            lazySet(0); // release array contents
            actual.onSubscribe(this);
            for (int i = 0; i < len; i++) {
                if (done || cancelled) {
                    return;
                }
                sources[i].subscribe(as[i]);
            }
        }

        @Override
        public void dispose() {
            if (!cancelled) {
                cancelled = true;
                cancelSources();
                if (getAndIncrement() == 0) {
                    clear(queue);
                }
            }
        }

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

        void cancel(SpscLinkedArrayQueue q) {
            clear(q);
            cancelSources();
        }

        void cancelSources() {
            for (CombinerObserver s : observers) {
                s.dispose();
            }
        }

        void clear(SpscLinkedArrayQueue q) {
            synchronized (this) {
                Arrays.fill(latest, null);
            }
            q.clear();
        }

        void combine(T value, int index) {
            CombinerObserver cs = observers[index];

            int a;
            int c;
            int len;
            boolean empty;
            boolean f;
            synchronized (this) {
                if (cancelled) {
                    return;
                }
                len = latest.length;
                T o = latest[index];
                a = active;
                if (o == null) {
                    active = ++a;
                }
                c = complete;
                if (value == null) {
                    complete = ++c;
                } else {
                    latest[index] = value;
                }
                f = a == len;
                // see if either all sources completed
                empty = c == len
                        || (value == null && o == null); // or this source completed without any value
                if (!empty) {
                    if (value != null && f) {
                        queue.offer(cs, latest.clone());
                    } else
                    if (value == null && errors.get() != null) {
                        done = true; // if this source completed without a value
                    }
                } else {
                    done = true;
                }
            }
            if (!f && value != null) {
                return;
            }
            drain();
        }
        void drain() {
            if (getAndIncrement() != 0) {
                return;
            }

            final SpscLinkedArrayQueue q = queue;
            final Observer a = actual;
            final boolean delayError = this.delayError;

            int missed = 1;
            for (;;) {

                if (checkTerminated(done, q.isEmpty(), a, q, delayError)) {
                    return;
                }

                for (;;) {

                    boolean d = done;
                    @SuppressWarnings("unchecked")
                    CombinerObserver cs = (CombinerObserver)q.poll();
                    boolean empty = cs == null;

                    if (checkTerminated(d, empty, a, q, delayError)) {
                        return;
                    }

                    if (empty) {
                        break;
                    }

                    @SuppressWarnings("unchecked")
                    T[] array = (T[])q.poll();

                    R v;
                    try {
                        v = ObjectHelper.requireNonNull(combiner.apply(array), "The combiner returned a null");
                    } catch (Throwable ex) {
                        Exceptions.throwIfFatal(ex);
                        cancelled = true;
                        cancel(q);
                        a.onError(ex);
                        return;
                    }

                    a.onNext(v);
                }

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


        boolean checkTerminated(boolean d, boolean empty, Observer a, SpscLinkedArrayQueue q, boolean delayError) {
            if (cancelled) {
                cancel(q);
                return true;
            }
            if (d) {
                if (delayError) {
                    if (empty) {
                        cancel(q);
                        Throwable e = errors.terminate();
                        if (e != null) {
                            a.onError(e);
                        } else {
                            a.onComplete();
                        }
                        return true;
                    }
                } else {
                    Throwable e = errors.get();
                    if (e != null) {
                        cancel(q);
                        a.onError(errors.terminate());
                        return true;
                    } else
                    if (empty) {
                        clear(queue);
                        a.onComplete();
                        return true;
                    }
                }
            }
            return false;
        }

        void onError(Throwable e) {
            if (!errors.addThrowable(e)) {
                RxJavaCommonPlugins.onError(e);
            }
        }
    }

    static final class CombinerObserver implements Observer {
        final LatestCoordinator parent;
        final int index;

        final AtomicReference s = new AtomicReference();

        CombinerObserver(LatestCoordinator parent, int index) {
            this.parent = parent;
            this.index = index;
        }

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

        @Override
        public void onNext(T t) {
            parent.combine(t, index);
        }

        @Override
        public void onError(Throwable t) {
            parent.onError(t);
            parent.combine(null, index);
        }

        @Override
        public void onComplete() {
            parent.combine(null, index);
        }

        public void dispose() {
            DisposableHelper.dispose(s);
        }
    }
}