| 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.observers; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.Observer; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 19 |
|
| 20 |
/** |
| 21 |
* A fuseable Observer that can generate 0 or 1 resulting value. |
| 22 |
* @param <T> the input value type |
| 23 |
* @param <R> the output value type |
| 24 |
*/ |
| 25 |
public abstract class DeferredScalarObserver<T, R> |
| 26 |
extends DeferredScalarDisposable<R> |
| 27 |
implements Observer<T> { |
| 28 |
|
| 29 |
private static final long serialVersionUID = -266195175408988651L; |
| 30 |
|
| 31 |
/** The upstream disposable. */ |
| 32 |
protected Disposable upstream; |
| 33 |
|
| 34 |
/** |
| 35 |
* Creates a DeferredScalarObserver instance and wraps a downstream Observer. |
| 36 |
* @param downstream the downstream subscriber, not null (not verified) |
| 37 |
*/ |
| 38 |
public DeferredScalarObserver(Observer<? super R> downstream) { |
| 39 |
super(downstream); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void onSubscribe(Disposable d) { |
| 44 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 45 |
this.upstream = d; |
| 46 |
|
| 47 |
downstream.onSubscribe(this); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public void onError(Throwable t) { |
| 53 |
value = null; |
| 54 |
error(t); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onComplete() { |
| 59 |
R v = value; |
| 60 |
if (v != null) { |
| 61 |
value = null; |
| 62 |
complete(v); |
| 63 |
} else { |
| 64 |
complete(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void dispose() { |
| 70 |
super.dispose(); |
| 71 |
upstream.dispose(); |
| 72 |
} |
| 73 |
} |