| 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 java.util.NoSuchElementException; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
import io.reactivex.rxjava3.internal.fuseable.HasUpstreamMaybeSource; |
| 22 |
|
| 23 |
/** |
| 24 |
* Wraps a MaybeSource and exposes its onSuccess and onError signals and signals |
| 25 |
* NoSuchElementException for onComplete if {@code defaultValue} is null. |
| 26 |
* |
| 27 |
* @param <T> the value type |
| 28 |
*/ |
| 29 |
public final class MaybeToSingle<T> extends Single<T> implements HasUpstreamMaybeSource<T> { |
| 30 |
|
| 31 |
final MaybeSource<T> source; |
| 32 |
final T defaultValue; |
| 33 |
|
| 34 |
public MaybeToSingle(MaybeSource<T> source, T defaultValue) { |
| 35 |
this.source = source; |
| 36 |
this.defaultValue = defaultValue; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public MaybeSource<T> source() { |
| 41 |
return source; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void subscribeActual(SingleObserver<? super T> observer) { |
| 46 |
source.subscribe(new ToSingleMaybeSubscriber<>(observer, defaultValue)); |
| 47 |
} |
| 48 |
|
| 49 |
static final class ToSingleMaybeSubscriber<T> implements MaybeObserver<T>, Disposable { |
| 50 |
final SingleObserver<? super T> downstream; |
| 51 |
final T defaultValue; |
| 52 |
|
| 53 |
Disposable upstream; |
| 54 |
|
| 55 |
ToSingleMaybeSubscriber(SingleObserver<? super T> actual, T defaultValue) { |
| 56 |
this.downstream = actual; |
| 57 |
this.defaultValue = defaultValue; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void dispose() { |
| 62 |
upstream.dispose(); |
| 63 |
upstream = DisposableHelper.DISPOSED; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public boolean isDisposed() { |
| 68 |
return upstream.isDisposed(); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onSubscribe(Disposable d) { |
| 73 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 74 |
this.upstream = d; |
| 75 |
|
| 76 |
downstream.onSubscribe(this); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onSuccess(T value) { |
| 82 |
upstream = DisposableHelper.DISPOSED; |
| 83 |
downstream.onSuccess(value); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void onError(Throwable e) { |
| 88 |
upstream = DisposableHelper.DISPOSED; |
| 89 |
downstream.onError(e); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onComplete() { |
| 94 |
upstream = DisposableHelper.DISPOSED; |
| 95 |
if (defaultValue != null) { |
| 96 |
downstream.onSuccess(defaultValue); |
| 97 |
} else { |
| 98 |
downstream.onError(new NoSuchElementException("The MaybeSource is empty")); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |