| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.maybe; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 19 |
import io.reactivex.rxjava3.internal.fuseable.*; |
| 20 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 21 |
|
| 22 |
/** |
| 23 |
* Signals true if the source Maybe signals onComplete, signals false if the source Maybe |
| 24 |
* signals onSuccess. |
| 25 |
* |
| 26 |
* @param <T> the value type |
| 27 |
*/ |
| 28 |
public final class MaybeIsEmptySingle<T> extends Single<Boolean> |
| 29 |
implements HasUpstreamMaybeSource<T>, FuseToMaybe<Boolean> { |
| 30 |
|
| 31 |
final MaybeSource<T> source; |
| 32 |
|
| 33 |
public MaybeIsEmptySingle(MaybeSource<T> source) { |
| 34 |
this.source = source; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public MaybeSource<T> source() { |
| 39 |
return source; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public Maybe<Boolean> fuseToMaybe() { |
| 44 |
return RxJavaPlugins.onAssembly(new MaybeIsEmpty<>(source)); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected void subscribeActual(SingleObserver<? super Boolean> observer) { |
| 49 |
source.subscribe(new IsEmptyMaybeObserver<>(observer)); |
| 50 |
} |
| 51 |
|
| 52 |
static final class IsEmptyMaybeObserver<T> |
| 53 |
implements MaybeObserver<T>, Disposable { |
| 54 |
|
| 55 |
final SingleObserver<? super Boolean> downstream; |
| 56 |
|
| 57 |
Disposable upstream; |
| 58 |
|
| 59 |
IsEmptyMaybeObserver(SingleObserver<? super Boolean> downstream) { |
| 60 |
this.downstream = downstream; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void dispose() { |
| 65 |
upstream.dispose(); |
| 66 |
upstream = DisposableHelper.DISPOSED; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public boolean isDisposed() { |
| 71 |
return upstream.isDisposed(); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onSubscribe(Disposable d) { |
| 76 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 77 |
this.upstream = d; |
| 78 |
|
| 79 |
downstream.onSubscribe(this); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onSuccess(T value) { |
| 85 |
upstream = DisposableHelper.DISPOSED; |
| 86 |
downstream.onSuccess(false); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onError(Throwable e) { |
| 91 |
upstream = DisposableHelper.DISPOSED; |
| 92 |
downstream.onError(e); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onComplete() { |
| 97 |
upstream = DisposableHelper.DISPOSED; |
| 98 |
downstream.onSuccess(true); |
| 99 |
} |
| 100 |
} |
| 101 |
} |