| 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.completable; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.Supplier; |
| 20 |
|
| 21 |
public final class CompletableToSingle<T> extends Single<T> { |
| 22 |
final CompletableSource source; |
| 23 |
|
| 24 |
final Supplier<? extends T> completionValueSupplier; |
| 25 |
|
| 26 |
final T completionValue; |
| 27 |
|
| 28 |
public CompletableToSingle(CompletableSource source, |
| 29 |
Supplier<? extends T> completionValueSupplier, T completionValue) { |
| 30 |
this.source = source; |
| 31 |
this.completionValue = completionValue; |
| 32 |
this.completionValueSupplier = completionValueSupplier; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
protected void subscribeActual(final SingleObserver<? super T> observer) { |
| 37 |
source.subscribe(new ToSingle(observer)); |
| 38 |
} |
| 39 |
|
| 40 |
final class ToSingle implements CompletableObserver { |
| 41 |
|
| 42 |
private final SingleObserver<? super T> observer; |
| 43 |
|
| 44 |
ToSingle(SingleObserver<? super T> observer) { |
| 45 |
this.observer = observer; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void onComplete() { |
| 50 |
T v; |
| 51 |
|
| 52 |
if (completionValueSupplier != null) { |
| 53 |
try { |
| 54 |
v = completionValueSupplier.get(); |
| 55 |
} catch (Throwable e) { |
| 56 |
Exceptions.throwIfFatal(e); |
| 57 |
observer.onError(e); |
| 58 |
return; |
| 59 |
} |
| 60 |
} else { |
| 61 |
v = completionValue; |
| 62 |
} |
| 63 |
|
| 64 |
if (v == null) { |
| 65 |
observer.onError(new NullPointerException("The value supplied is null")); |
| 66 |
} else { |
| 67 |
observer.onSuccess(v); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onError(Throwable e) { |
| 73 |
observer.onError(e); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onSubscribe(Disposable d) { |
| 78 |
observer.onSubscribe(d); |
| 79 |
} |
| 80 |
|
| 81 |
} |
| 82 |
} |