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

io.reactivex.rxjava3.internal.operators.observable.ObservableWithLatestFromMany Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show 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.rxjava3.internal.operators.observable;

import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.*;

import io.reactivex.rxjava3.annotations.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.disposables.*;
import io.reactivex.rxjava3.internal.util.*;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;

/**
 * Combines a main sequence of values with the latest from multiple other sequences via
 * a selector function.
 *
 * @param  the main sequence's type
 * @param  the output type
 */
public final class ObservableWithLatestFromMany extends AbstractObservableWithUpstream {

    @Nullable
    final ObservableSource[] otherArray;

    @Nullable
    final Iterable> otherIterable;

    @NonNull
    final Function combiner;

    public ObservableWithLatestFromMany(@NonNull ObservableSource source, @NonNull ObservableSource[] otherArray, @NonNull Function combiner) {
        super(source);
        this.otherArray = otherArray;
        this.otherIterable = null;
        this.combiner = combiner;
    }

    public ObservableWithLatestFromMany(@NonNull ObservableSource source, @NonNull Iterable> otherIterable, @NonNull Function combiner) {
        super(source);
        this.otherArray = null;
        this.otherIterable = otherIterable;
        this.combiner = combiner;
    }

    @Override
    protected void subscribeActual(Observer observer) {
        ObservableSource[] others = otherArray;
        int n = 0;
        if (others == null) {
            others = new ObservableSource[8];

            try {
                for (ObservableSource p : otherIterable) {
                    if (n == others.length) {
                        others = Arrays.copyOf(others, n + (n >> 1));
                    }
                    others[n++] = p;
                }
            } catch (Throwable ex) {
                Exceptions.throwIfFatal(ex);
                EmptyDisposable.error(ex, observer);
                return;
            }

        } else {
            n = others.length;
        }

        if (n == 0) {
            new ObservableMap<>(source, new SingletonArrayFunc()).subscribeActual(observer);
            return;
        }

        WithLatestFromObserver parent = new WithLatestFromObserver<>(observer, combiner, n);
        observer.onSubscribe(parent);
        parent.subscribe(others, n);

        source.subscribe(parent);
    }

    static final class WithLatestFromObserver
    extends AtomicInteger
    implements Observer, Disposable {

        private static final long serialVersionUID = 1577321883966341961L;

        final Observer downstream;

        final Function combiner;

        final WithLatestInnerObserver[] observers;

        final AtomicReferenceArray values;

        final AtomicReference upstream;

        final AtomicThrowable error;

        volatile boolean done;

        WithLatestFromObserver(Observer actual, Function combiner, int n) {
            this.downstream = actual;
            this.combiner = combiner;
            WithLatestInnerObserver[] s = new WithLatestInnerObserver[n];
            for (int i = 0; i < n; i++) {
                s[i] = new WithLatestInnerObserver(this, i);
            }
            this.observers = s;
            this.values = new AtomicReferenceArray<>(n);
            this.upstream = new AtomicReference<>();
            this.error = new AtomicThrowable();
        }

        void subscribe(ObservableSource[] others, int n) {
            WithLatestInnerObserver[] observers = this.observers;
            AtomicReference upstream = this.upstream;
            for (int i = 0; i < n; i++) {
                if (DisposableHelper.isDisposed(upstream.get()) || done) {
                    return;
                }
                others[i].subscribe(observers[i]);
            }
        }

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

        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            AtomicReferenceArray ara = values;
            int n = ara.length();
            Object[] objects = new Object[n + 1];
            objects[0] = t;

            for (int i = 0; i < n; i++) {
                Object o = ara.get(i);
                if (o == null) {
                    // no latest, skip this value
                    return;
                }
                objects[i + 1] = o;
            }

            R v;

            try {
                v = Objects.requireNonNull(combiner.apply(objects), "combiner returned a null value");
            } catch (Throwable ex) {
                Exceptions.throwIfFatal(ex);
                dispose();
                onError(ex);
                return;
            }

            HalfSerializer.onNext(downstream, v, this, error);
        }

        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaPlugins.onError(t);
                return;
            }
            done = true;
            cancelAllBut(-1);
            HalfSerializer.onError(downstream, t, this, error);
        }

        @Override
        public void onComplete() {
            if (!done) {
                done = true;
                cancelAllBut(-1);
                HalfSerializer.onComplete(downstream, this, error);
            }
        }

        @Override
        public boolean isDisposed() {
            return DisposableHelper.isDisposed(upstream.get());
        }

        @Override
        public void dispose() {
            DisposableHelper.dispose(upstream);
            for (WithLatestInnerObserver observer : observers) {
                observer.dispose();
            }
        }

        void innerNext(int index, Object o) {
            values.set(index, o);
        }

        void innerError(int index, Throwable t) {
            done = true;
            DisposableHelper.dispose(upstream);
            cancelAllBut(index);
            HalfSerializer.onError(downstream, t, this, error);
        }

        void innerComplete(int index, boolean nonEmpty) {
            if (!nonEmpty) {
                done = true;
                cancelAllBut(index);
                HalfSerializer.onComplete(downstream, this, error);
            }
        }

        void cancelAllBut(int index) {
            WithLatestInnerObserver[] observers = this.observers;
            for (int i = 0; i < observers.length; i++) {
                if (i != index) {
                    observers[i].dispose();
                }
            }
        }
    }

    static final class WithLatestInnerObserver
    extends AtomicReference
    implements Observer {

        private static final long serialVersionUID = 3256684027868224024L;

        final WithLatestFromObserver parent;

        final int index;

        boolean hasValue;

        WithLatestInnerObserver(WithLatestFromObserver parent, int index) {
            this.parent = parent;
            this.index = index;
        }

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

        @Override
        public void onNext(Object t) {
            if (!hasValue) {
                hasValue = true;
            }
            parent.innerNext(index, t);
        }

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

        @Override
        public void onComplete() {
            parent.innerComplete(index, hasValue);
        }

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

    final class SingletonArrayFunc implements Function {
        @Override
        public R apply(T t) throws Throwable {
            return Objects.requireNonNull(combiner.apply(new Object[] { t }), "The combiner returned a null value");
        }
    }
}