| 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.flowable; |
| 15 |
|
| 16 |
import org.reactivestreams.Subscription; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class FlowableSingleMaybe<T> extends Maybe<T> implements FuseToFlowable<T> { |
| 25 |
|
| 26 |
final Flowable<T> source; |
| 27 |
|
| 28 |
public FlowableSingleMaybe(Flowable<T> source) { |
| 29 |
this.source = source; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 34 |
source.subscribe(new SingleElementSubscriber<>(observer)); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public Flowable<T> fuseToFlowable() { |
| 39 |
return RxJavaPlugins.onAssembly(new FlowableSingle<>(source, null, false)); |
| 40 |
} |
| 41 |
|
| 42 |
static final class SingleElementSubscriber<T> |
| 43 |
implements FlowableSubscriber<T>, Disposable { |
| 44 |
|
| 45 |
final MaybeObserver<? super T> downstream; |
| 46 |
|
| 47 |
Subscription upstream; |
| 48 |
|
| 49 |
boolean done; |
| 50 |
|
| 51 |
T value; |
| 52 |
|
| 53 |
SingleElementSubscriber(MaybeObserver<? super T> downstream) { |
| 54 |
this.downstream = downstream; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onSubscribe(Subscription s) { |
| 59 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 60 |
this.upstream = s; |
| 61 |
downstream.onSubscribe(this); |
| 62 |
s.request(Long.MAX_VALUE); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onNext(T t) { |
| 68 |
if (done) { |
| 69 |
return; |
| 70 |
} |
| 71 |
if (value != null) { |
| 72 |
done = true; |
| 73 |
upstream.cancel(); |
| 74 |
upstream = SubscriptionHelper.CANCELLED; |
| 75 |
downstream.onError(new IllegalArgumentException("Sequence contains more than one element!")); |
| 76 |
return; |
| 77 |
} |
| 78 |
value = t; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onError(Throwable t) { |
| 83 |
if (done) { |
| 84 |
RxJavaPlugins.onError(t); |
| 85 |
return; |
| 86 |
} |
| 87 |
done = true; |
| 88 |
upstream = SubscriptionHelper.CANCELLED; |
| 89 |
downstream.onError(t); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onComplete() { |
| 94 |
if (done) { |
| 95 |
return; |
| 96 |
} |
| 97 |
done = true; |
| 98 |
upstream = SubscriptionHelper.CANCELLED; |
| 99 |
T v = value; |
| 100 |
value = null; |
| 101 |
if (v == null) { |
| 102 |
downstream.onComplete(); |
| 103 |
} else { |
| 104 |
downstream.onSuccess(v); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void dispose() { |
| 110 |
upstream.cancel(); |
| 111 |
upstream = SubscriptionHelper.CANCELLED; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public boolean isDisposed() { |
| 116 |
return upstream == SubscriptionHelper.CANCELLED; |
| 117 |
} |
| 118 |
} |
| 119 |
} |