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

hu.akarnokd.rxjava.interop.ObservableV1ToObservableV2 Maven / Gradle / Ivy

/*
 * Copyright 2016-2018 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.rxjava.interop;

/**
 * Convert a V1 Observable into a V2 Observable, composing cancellation.
 *
 * @param  the value type
 */
final class ObservableV1ToObservableV2 extends io.reactivex.Observable {

    final rx.Observable source;

    ObservableV1ToObservableV2(rx.Observable source) {
        this.source = source;
    }

    @Override
    protected void subscribeActual(io.reactivex.Observer s) {
        ObservableSubscriber parent = new ObservableSubscriber(s);
        s.onSubscribe(parent);

        source.unsafeSubscribe(parent);
    }

    static final class ObservableSubscriber extends rx.Subscriber
    implements io.reactivex.disposables.Disposable {

        final io.reactivex.Observer actual;

        boolean done;

        ObservableSubscriber(io.reactivex.Observer actual) {
            this.actual = actual;
        }

        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            if (t == null) {
                unsubscribe();
                onError(new NullPointerException(
                    "The upstream 1.x Observable signalled a null value which is not supported in 2.x"));
            } else {
                actual.onNext(t);
            }
        }

        @Override
        public void onError(Throwable e) {
            if (done) {
                io.reactivex.plugins.RxJavaPlugins.onError(e);
                return;
            }
            done = true;
            actual.onError(e);
            unsubscribe(); // v1 expects an unsubscribe call when terminated
        }

        @Override
        public void onCompleted() {
            if (done) {
                return;
            }
            done = true;
            actual.onComplete();
            unsubscribe(); // v1 expects an unsubscribe call when terminated
        }

        @Override
        public void dispose() {
            unsubscribe();
        }

        @Override
        public boolean isDisposed() {
            return isUnsubscribed();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy