Debian Codegraph @

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