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

hu.akarnokd.reactive4javaflow.impl.operators.FolyamConcatIterable Maven / Gradle / Ivy

/*
 * Copyright 2017 David Karnok
 *
 * 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 hu.akarnokd.reactive4javaflow.impl.operators;

import hu.akarnokd.reactive4javaflow.*;
import hu.akarnokd.reactive4javaflow.errors.CompositeThrowable;
import hu.akarnokd.reactive4javaflow.fused.ConditionalSubscriber;
import hu.akarnokd.reactive4javaflow.impl.*;

import java.lang.invoke.*;
import java.util.*;
import java.util.concurrent.Flow;

public final class FolyamConcatIterable extends Folyam {

    final Iterable> sources;

    final boolean delayError;

    public FolyamConcatIterable(Iterable> sources, boolean delayError) {
        this.sources = sources;
        this.delayError = delayError;
    }

    @Override
    protected void subscribeActual(FolyamSubscriber s) {

        Iterator> it;

        try {
            it = Objects.requireNonNull(sources.iterator(), "The iterable returned a null iterator");
        } catch (Throwable ex) {
            EmptySubscription.error(s, ex);
            return;
        }

        AbstractConcatIterable parent;

        if (s instanceof ConditionalSubscriber) {
            parent = new ConcatIterableConditionalSubscriber<>((ConditionalSubscriber)s, delayError, it);
        } else {
            parent = new ConcatIterableSubscriber<>(s, delayError, it);
        }
        s.onSubscribe(parent);
        parent.drain();
    }

    static abstract class AbstractConcatIterable extends SubscriptionArbiter {

        final boolean delayError;

        final Iterator> sources;

        long produced;

        int wip;
        static final VarHandle WIP = VH.find(MethodHandles.lookup(), AbstractConcatIterable.class, "wip", Integer.TYPE);

        Throwable error;

        AbstractConcatIterable(boolean delayError, Iterator> sources) {
            this.delayError = delayError;
            this.sources = sources;
        }

        public void onSubscribe(Flow.Subscription subscription) {
            arbiterReplace(subscription);
        }

        public void onComplete() {
            drain();
        }

        abstract void drain();
    }

    static final class ConcatIterableSubscriber extends AbstractConcatIterable implements FolyamSubscriber {

        final FolyamSubscriber actual;

        ConcatIterableSubscriber(FolyamSubscriber actual, boolean delayError, Iterator> sources) {
            super(delayError, sources);
            this.actual = actual;
        }

        @Override
        public void onNext(T item) {
            produced++;
            actual.onNext(item);
        }

        @Override
        public void onError(Throwable throwable) {
            if (delayError) {
                error = CompositeThrowable.combine(error, throwable);
                drain();
                return;
            }
            actual.onError(throwable);
        }

        void drain() {
            if ((int)WIP.getAndAdd(this, 1) == 0) {
                do {
                    if (arbiterIsCancelled()) {
                        return;
                    }

                    boolean b;

                    try {
                        b = sources.hasNext();
                    } catch (Throwable ex) {
                        ex = CompositeThrowable.combine(error, ex);
                        actual.onError(ex);
                        return;
                    }

                    if (!b) {
                        Throwable ex = error;
                        if (ex == null) {
                            actual.onComplete();
                        } else {
                            actual.onError(ex);
                        }
                        return;
                    }

                    Flow.Publisher p;

                    try {
                        p = Objects.requireNonNull(sources.next(), "The iterator returned a null Flow.Publisher");
                    } catch (Throwable ex) {
                        ex = CompositeThrowable.combine(error, ex);
                        actual.onError(ex);
                        return;
                    }

                    long c = produced;
                    if (c != 0L) {
                        produced = 0L;
                        arbiterProduced(c);
                    }

                    p.subscribe(this);
                } while  ((int)WIP.getAndAdd(this, -1) - 1 != 0);
            }
        }
    }

    static final class ConcatIterableConditionalSubscriber extends AbstractConcatIterable implements ConditionalSubscriber {

        final ConditionalSubscriber actual;

        ConcatIterableConditionalSubscriber(ConditionalSubscriber actual, boolean delayError, Iterator> sources) {
            super(delayError, sources);
            this.actual = actual;
        }

        @Override
        public void onNext(T item) {
            produced++;
            actual.onNext(item);
        }

        @Override
        public boolean tryOnNext(T item) {
            if (actual.tryOnNext(item)) {
                produced++;
                return true;
            }
            return false;
        }

        @Override
        public void onError(Throwable throwable) {
            if (delayError) {
                error = CompositeThrowable.combine(error, throwable);
                drain();
                return;
            }
            actual.onError(throwable);
        }

        void drain() {
            if ((int)WIP.getAndAdd(this, 1) == 0) {
                do {
                    if (arbiterIsCancelled()) {
                        return;
                    }

                    boolean b;

                    try {
                        b = sources.hasNext();
                    } catch (Throwable ex) {
                        ex = CompositeThrowable.combine(error, ex);
                        actual.onError(ex);
                        return;
                    }

                    if (!b) {
                        Throwable ex = error;
                        if (ex == null) {
                            actual.onComplete();
                        } else {
                            actual.onError(ex);
                        }
                        return;
                    }

                    Flow.Publisher p;

                    try {
                        p = Objects.requireNonNull(sources.next(), "The iterator returned a null Flow.Publisher");
                    } catch (Throwable ex) {
                        ex = CompositeThrowable.combine(error, ex);
                        actual.onError(ex);
                        return;
                    }

                    long c = produced;
                    if (c != 0L) {
                        produced = 0L;
                        arbiterProduced(c);
                    }

                    p.subscribe(this);
                } while  ((int)WIP.getAndAdd(this, -1) - 1 != 0);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy