| 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.subscribers; |
| 15 |
|
| 16 |
import org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 19 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 20 |
|
| 21 |
/** |
| 22 |
* A subscriber, extending a DeferredScalarSubscription, |
| 23 |
* that is unbounded-in and can generate 0 or 1 resulting value. |
| 24 |
* @param <T> the input value type |
| 25 |
* @param <R> the output value type |
| 26 |
*/ |
| 27 |
public abstract class DeferredScalarSubscriber<T, R> extends DeferredScalarSubscription<R> |
| 28 |
implements FlowableSubscriber<T> { |
| 29 |
|
| 30 |
private static final long serialVersionUID = 2984505488220891551L; |
| 31 |
|
| 32 |
/** The upstream subscription. */ |
| 33 |
protected Subscription upstream; |
| 34 |
|
| 35 |
/** Can indicate if there was at least on onNext call. */ |
| 36 |
protected boolean hasValue; |
| 37 |
|
| 38 |
/** |
| 39 |
* Creates a DeferredScalarSubscriber instance and wraps a downstream Subscriber. |
| 40 |
* @param downstream the downstream subscriber, not null (not verified) |
| 41 |
*/ |
| 42 |
public DeferredScalarSubscriber(Subscriber<? super R> downstream) { |
| 43 |
super(downstream); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public void onSubscribe(Subscription s) { |
| 48 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 49 |
this.upstream = s; |
| 50 |
|
| 51 |
downstream.onSubscribe(this); |
| 52 |
|
| 53 |
s.request(Long.MAX_VALUE); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onError(Throwable t) { |
| 59 |
value = null; |
| 60 |
downstream.onError(t); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onComplete() { |
| 65 |
if (hasValue) { |
| 66 |
complete(value); |
| 67 |
} else { |
| 68 |
downstream.onComplete(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void cancel() { |
| 74 |
super.cancel(); |
| 75 |
upstream.cancel(); |
| 76 |
} |
| 77 |
} |