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

io.reactivex.internal.operators.flowable.FlowableZip Maven / Gradle / Ivy

There is a newer version: 2.2.21
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.internal.operators.flowable;

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

import org.reactivestreams.*;

import io.reactivex.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.fuseable.*;
import io.reactivex.internal.queue.SpscArrayQueue;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;

public final class FlowableZip extends Flowable {

    final Publisher[] sources;
    final Iterable> sourcesIterable;
    final Function zipper;
    final int bufferSize;
    final boolean delayError;

    public FlowableZip(Publisher[] sources,
            Iterable> sourcesIterable,
                    Function zipper,
                    int bufferSize,
                    boolean delayError) {
        this.sources = sources;
        this.sourcesIterable = sourcesIterable;
        this.zipper = zipper;
        this.bufferSize = bufferSize;
        this.delayError = delayError;
    }

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

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

        ZipCoordinator coordinator = new ZipCoordinator(s, zipper, count, bufferSize, delayError);

        s.onSubscribe(coordinator);

        coordinator.subscribe(sources, count);
    }

    static final class ZipCoordinator
    extends AtomicInteger
    implements Subscription {

        private static final long serialVersionUID = -2434867452883857743L;

        final Subscriber downstream;

        final ZipSubscriber[] subscribers;

        final Function zipper;

        final AtomicLong requested;

        final AtomicThrowable errors;

        final boolean delayErrors;

        volatile boolean cancelled;

        final Object[] current;

        ZipCoordinator(Subscriber actual,
                Function zipper, int n, int prefetch, boolean delayErrors) {
            this.downstream = actual;
            this.zipper = zipper;
            this.delayErrors = delayErrors;
            @SuppressWarnings("unchecked")
            ZipSubscriber[] a = new ZipSubscriber[n];
            for (int i = 0; i < n; i++) {
                a[i] = new ZipSubscriber(this, prefetch);
            }
            this.current = new Object[n];
            this.subscribers = a;
            this.requested = new AtomicLong();
            this.errors = new AtomicThrowable();
        }

        void subscribe(Publisher[] sources, int n) {
            ZipSubscriber[] a = subscribers;
            for (int i = 0; i < n; i++) {
                if (cancelled || (!delayErrors && errors.get() != null)) {
                    return;
                }
                sources[i].subscribe(a[i]);
            }
        }

        @Override
        public void request(long n) {
            if (SubscriptionHelper.validate(n)) {
                BackpressureHelper.add(requested, n);
                drain();
            }
        }

        @Override
        public void cancel() {
            if (!cancelled) {
                cancelled = true;

                cancelAll();
            }
        }

        void error(ZipSubscriber inner, Throwable e) {
            if (errors.addThrowable(e)) {
                inner.done = true;
                drain();
            } else {
                RxJavaPlugins.onError(e);
            }
        }

        void cancelAll() {
            for (ZipSubscriber s : subscribers) {
                s.cancel();
            }
        }

        void drain() {

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

            final Subscriber a = downstream;
            final ZipSubscriber[] qs = subscribers;
            final int n = qs.length;
            Object[] values = current;

            int missed = 1;

            for (;;) {

                long r = requested.get();
                long e = 0L;

                while (r != e) {

                    if (cancelled) {
                        return;
                    }

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

                    boolean empty = false;

                    for (int j = 0; j < n; j++) {
                        ZipSubscriber inner = qs[j];
                        if (values[j] == null) {
                            try {
                                boolean d = inner.done;
                                SimpleQueue q = inner.queue;

                                T v = q != null ? q.poll() : null;

                                boolean sourceEmpty = v == null;
                                if (d && sourceEmpty) {
                                    cancelAll();
                                    Throwable ex = errors.get();
                                    if (ex != null) {
                                        a.onError(errors.terminate());
                                    } else {
                                        a.onComplete();
                                    }
                                    return;
                                }
                                if (!sourceEmpty) {
                                    values[j] = v;
                                } else {
                                    empty = true;
                                }
                            } catch (Throwable ex) {
                                Exceptions.throwIfFatal(ex);

                                errors.addThrowable(ex);
                                if (!delayErrors) {
                                    cancelAll();
                                    a.onError(errors.terminate());
                                    return;
                                }
                                empty = true;
                            }
                        }
                    }

                    if (empty) {
                        break;
                    }

                    R v;

                    try {
                        v = ObjectHelper.requireNonNull(zipper.apply(values.clone()), "The zipper returned a null value");
                    } catch (Throwable ex) {
                        Exceptions.throwIfFatal(ex);
                        cancelAll();
                        errors.addThrowable(ex);
                        a.onError(errors.terminate());
                        return;
                    }

                    a.onNext(v);

                    e++;

                    Arrays.fill(values, null);
                }

                if (r == e) {
                    if (cancelled) {
                        return;
                    }

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

                    for (int j = 0; j < n; j++) {
                        ZipSubscriber inner = qs[j];
                        if (values[j] == null) {
                            try {
                                boolean d = inner.done;
                                SimpleQueue q = inner.queue;
                                T v = q != null ? q.poll() : null;

                                boolean empty = v == null;
                                if (d && empty) {
                                    cancelAll();
                                    Throwable ex = errors.get();
                                    if (ex != null) {
                                        a.onError(errors.terminate());
                                    } else {
                                        a.onComplete();
                                    }
                                    return;
                                }
                                if (!empty) {
                                    values[j] = v;
                                }
                            } catch (Throwable ex) {
                                Exceptions.throwIfFatal(ex);
                                errors.addThrowable(ex);
                                if (!delayErrors) {
                                    cancelAll();
                                    a.onError(errors.terminate());
                                    return;
                                }
                            }
                        }
                    }

                }

                if (e != 0L) {

                    for (ZipSubscriber inner : qs) {
                        inner.request(e);
                    }

                    if (r != Long.MAX_VALUE) {
                        requested.addAndGet(-e);
                    }
                }

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

    static final class ZipSubscriber extends AtomicReference implements FlowableSubscriber, Subscription {

        private static final long serialVersionUID = -4627193790118206028L;

        final ZipCoordinator parent;

        final int prefetch;

        final int limit;

        SimpleQueue queue;

        long produced;

        volatile boolean done;

        int sourceMode;

        ZipSubscriber(ZipCoordinator parent, int prefetch) {
            this.parent = parent;
            this.prefetch = prefetch;
            this.limit = prefetch - (prefetch >> 2);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.setOnce(this, s)) {
                if (s instanceof QueueSubscription) {
                    QueueSubscription f = (QueueSubscription) s;

                    int m = f.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY);

                    if (m == QueueSubscription.SYNC) {
                        sourceMode = m;
                        queue = f;
                        done = true;
                        parent.drain();
                        return;
                    }
                    if (m == QueueSubscription.ASYNC) {
                        sourceMode = m;
                        queue = f;
                        s.request(prefetch);
                        return;
                    }
                }

                queue = new SpscArrayQueue(prefetch);

                s.request(prefetch);
            }
        }

        @Override
        public void onNext(T t) {
            if (sourceMode != QueueSubscription.ASYNC) {
                queue.offer(t);
            }
            parent.drain();
        }

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

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

        @Override
        public void cancel() {
            SubscriptionHelper.cancel(this);
        }

        @Override
        public void request(long n) {
            if (sourceMode != QueueSubscription.SYNC) {
                long p = produced + n;
                if (p >= limit) {
                    produced = 0L;
                    get().request(p);
                } else {
                    produced = p;
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy