Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSingleSingle.java

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