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

hu.akarnokd.rxjava2.expr.FlowableWhileDoWhile Maven / Gradle / Ivy

There is a newer version: 0.20.10
Show newest version
/*
 * Copyright 2016-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.rxjava2.expr;

import java.util.concurrent.atomic.AtomicInteger;

import org.reactivestreams.*;

import io.reactivex.Flowable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.internal.subscriptions.*;

final class FlowableWhileDoWhile extends Flowable {

    final Publisher source;

    final BooleanSupplier preCondition;

    final BooleanSupplier postCondition;

    FlowableWhileDoWhile(Publisher source, BooleanSupplier preCondition,
            BooleanSupplier postCondition) {
        this.source = source;
        this.preCondition = preCondition;
        this.postCondition = postCondition;
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        boolean b;

        try {
            b = preCondition.getAsBoolean();
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            EmptySubscription.error(ex, s);
            return;
        }

        if (b) {
            WhileDoWhileObserver parent = new WhileDoWhileObserver(s, postCondition, source);
            s.onSubscribe(parent);
            parent.subscribeNext();
        } else {
            EmptySubscription.complete(s);
        }
    }

    static final class WhileDoWhileObserver
    extends SubscriptionArbiter
    implements Subscriber, Subscription {

        private static final long serialVersionUID = -5255585317630843019L;

        final Subscriber actual;

        final AtomicInteger wip;

        final BooleanSupplier postCondition;

        final Publisher source;

        volatile boolean active;

        long produced;

        WhileDoWhileObserver(Subscriber actual, BooleanSupplier postCondition, Publisher source) {
            this.actual = actual;
            this.wip = new AtomicInteger();
            this.postCondition = postCondition;
            this.source = source;
        }

        @Override
        public void onSubscribe(Subscription d) {
            super.setSubscription(d);
        }

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

        @Override
        public void onError(Throwable e) {
            actual.onError(e);
        }

        @Override
        public void onComplete() {

            boolean b;

            try {
                b = postCondition.getAsBoolean();
            } catch (Throwable ex) {
                Exceptions.throwIfFatal(ex);
                actual.onError(ex);
                return;
            }

            if (b) {
                long p = produced;
                if (p != 0L) {
                    produced(p);
                }

                active = false;
                subscribeNext();
            } else {
                actual.onComplete();
            }
        }

        void subscribeNext() {
            if (wip.getAndIncrement() != 0) {
                return;
            }

            for (;;) {

                if (isCancelled()) {
                    return;
                }

                if (!active) {
                    active = true;
                    source.subscribe(this);
                }

                if (wip.decrementAndGet() == 0) {
                    break;
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy