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

hu.akarnokd.rxjava.interop.SingleV2ToSingleV1 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;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Convert a V2 Single into a V1 Single, composing cancellation.
 *
 * @param  the value type
 */
final class SingleV2ToSingleV1 implements rx.Single.OnSubscribe {

    final io.reactivex.SingleSource source;

    SingleV2ToSingleV1(io.reactivex.SingleSource source) {
        this.source = source;
    }

    @Override
    public void call(rx.SingleSubscriber t) {
        SourceSingleObserver parent = new SourceSingleObserver(t);
        t.add(parent);
        source.subscribe(parent);
    }

    static final class SourceSingleObserver
    extends AtomicReference
    implements io.reactivex.SingleObserver, rx.Subscription {

        private static final long serialVersionUID = 4758098209431016997L;

        final rx.SingleSubscriber actual;

        SourceSingleObserver(rx.SingleSubscriber actual) {
            this.actual = actual;
        }

        @Override
        public void unsubscribe() {
            io.reactivex.internal.disposables.DisposableHelper.dispose(this);
        }

        @Override
        public boolean isUnsubscribed() {
            return io.reactivex.internal.disposables.DisposableHelper.isDisposed(get());
        }

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

        @Override
        public void onSuccess(T value) {
            actual.onSuccess(value);
        }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy