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

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

There is a newer version: 3.1.9
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.concurrent.atomic.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
import io.reactivex.rxjava3.internal.util.*;

public final class ObservableTakeUntil extends AbstractObservableWithUpstream {

    final ObservableSource other;

    public ObservableTakeUntil(ObservableSource source, ObservableSource other) {
        super(source);
        this.other = other;
    }

    @Override
    public void subscribeActual(Observer child) {
        TakeUntilMainObserver parent = new TakeUntilMainObserver<>(child);
        child.onSubscribe(parent);

        other.subscribe(parent.otherObserver);
        source.subscribe(parent);
    }

    static final class TakeUntilMainObserver extends AtomicInteger
    implements Observer, Disposable {

        private static final long serialVersionUID = 1418547743690811973L;

        final Observer downstream;

        final AtomicReference upstream;

        final OtherObserver otherObserver;

        final AtomicThrowable error;

        TakeUntilMainObserver(Observer downstream) {
            this.downstream = downstream;
            this.upstream = new AtomicReference<>();
            this.otherObserver = new OtherObserver();
            this.error = new AtomicThrowable();
        }

        @Override
        public void dispose() {
            DisposableHelper.dispose(upstream);
            DisposableHelper.dispose(otherObserver);
        }

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

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

        @Override
        public void onNext(T t) {
            HalfSerializer.onNext(downstream, t, this, error);
        }

        @Override
        public void onError(Throwable e) {
            DisposableHelper.dispose(otherObserver);
            HalfSerializer.onError(downstream, e, this, error);
        }

        @Override
        public void onComplete() {
            DisposableHelper.dispose(otherObserver);
            HalfSerializer.onComplete(downstream, this, error);
        }

        void otherError(Throwable e) {
            DisposableHelper.dispose(upstream);
            HalfSerializer.onError(downstream, e, this, error);
        }

        void otherComplete() {
            DisposableHelper.dispose(upstream);
            HalfSerializer.onComplete(downstream, this, error);
        }

        final class OtherObserver extends AtomicReference
        implements Observer {

            private static final long serialVersionUID = -8693423678067375039L;

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

            @Override
            public void onNext(U t) {
                DisposableHelper.dispose(this);
                otherComplete();
            }

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

            @Override
            public void onComplete() {
                otherComplete();
            }

        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy