hu.akarnokd.rxjava.interop.MaybeV2ToCompletableV1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava2-interop Show documentation
Show all versions of rxjava2-interop Show documentation
rxjava2-interop developed by David Karnok
/*
* 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;
/**
* Converts a V2 Maybe into a V1 Complete where an onSuccess value triggers onCompleted.
*
* @param the value type
*/
final class MaybeV2ToCompletableV1 implements rx.Completable.OnSubscribe {
final io.reactivex.MaybeSource source;
MaybeV2ToCompletableV1(io.reactivex.MaybeSource source) {
this.source = source;
}
@Override
public void call(rx.CompletableSubscriber t) {
MaybeV2Observer parent = new MaybeV2Observer(t);
t.onSubscribe(parent);
source.subscribe(parent);
}
static final class MaybeV2Observer
extends AtomicReference
implements io.reactivex.MaybeObserver, rx.Subscription {
private static final long serialVersionUID = 5045507662443540605L;
final rx.CompletableSubscriber actual;
MaybeV2Observer(rx.CompletableSubscriber 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.onCompleted();
}
@Override
public void onError(Throwable e) {
actual.onError(e);
}
@Override
public void onComplete() {
actual.onCompleted();
}
}
}