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

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

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

/**
 * Wrap a V1 Subject and expose it as a V2 Subject.
 * @param  the input/output value type
 * @since 0.9.0
 */
final class SubjectV1ToSubjectV2 extends io.reactivex.subjects.Subject {

    final rx.subjects.Subject source;

    volatile boolean terminated;
    Throwable error;

    SubjectV1ToSubjectV2(rx.subjects.Subject source) {
        this.source = source;
    }

    @Override
    public void onSubscribe(io.reactivex.disposables.Disposable d) {
        if (terminated) {
            d.dispose();
        }
    }

    @Override
    public void onNext(T t) {
        if (!terminated) {
            if (t == null) {
                onError(new NullPointerException());
            } else {
                source.onNext(t);
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        if (!terminated) {
            if (e == null) {
                e = new NullPointerException("Throwable was null");
            }
            error = e;
            terminated = true;
            source.onError(e);
        } else {
            io.reactivex.plugins.RxJavaPlugins.onError(e);
        }
    }

    @Override
    public void onComplete() {
        if (!terminated) {
            terminated = true;
            source.onCompleted();
        }
    }

    @Override
    protected void subscribeActual(io.reactivex.Observer observer) {
        hu.akarnokd.rxjava.interop.ObservableV1ToObservableV2.ObservableSubscriber parent =
                new hu.akarnokd.rxjava.interop.ObservableV1ToObservableV2.ObservableSubscriber(observer);
        observer.onSubscribe(parent);

        source.unsafeSubscribe(parent);
    }

    @Override
    public boolean hasObservers() {
        return source.hasObservers();
    }

    @Override
    public boolean hasComplete() {
        return terminated && error == null;
    }

    @Override
    public boolean hasThrowable() {
        return terminated && error != null;
    }

    @Override
    public Throwable getThrowable() {
        return terminated ? error : null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy