Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeConcatIterable.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.maybe;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18 import java.util.concurrent.atomic.*;
19
20 import org.reactivestreams.*;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import io.reactivex.rxjava3.exceptions.Exceptions;
25 import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
26 import io.reactivex.rxjava3.internal.subscriptions.*;
27 import io.reactivex.rxjava3.internal.util.*;
28
29 /**
30 * Concatenate values of each MaybeSource provided by an Iterable.
31 *
32 * @param <T> the value type
33 */
34 public final class MaybeConcatIterable<T> extends Flowable<T> {
35
36 final Iterable<? extends MaybeSource<? extends T>> sources;
37
38 public MaybeConcatIterable(Iterable<? extends MaybeSource<? extends T>> sources) {
39 this.sources = sources;
40 }
41
42 @Override
43 protected void subscribeActual(Subscriber<? super T> s) {
44
45 Iterator<? extends MaybeSource<? extends T>> it;
46
47 try {
48 it = Objects.requireNonNull(sources.iterator(), "The sources Iterable returned a null Iterator");
49 } catch (Throwable ex) {
50 Exceptions.throwIfFatal(ex);
51 EmptySubscription.error(ex, s);
52 return;
53 }
54
55 ConcatMaybeObserver<T> parent = new ConcatMaybeObserver<>(s, it);
56 s.onSubscribe(parent);
57 parent.drain();
58 }
59
60 static final class ConcatMaybeObserver<T>
61 extends AtomicInteger
62 implements MaybeObserver<T>, Subscription {
63
64 private static final long serialVersionUID = 3520831347801429610L;
65
66 final Subscriber<? super T> downstream;
67
68 final AtomicLong requested;
69
70 final AtomicReference<Object> current;
71
72 final SequentialDisposable disposables;
73
74 final Iterator<? extends MaybeSource<? extends T>> sources;
75
76 long produced;
77
78 ConcatMaybeObserver(Subscriber<? super T> actual, Iterator<? extends MaybeSource<? extends T>> sources) {
79 this.downstream = actual;
80 this.sources = sources;
81 this.requested = new AtomicLong();
82 this.disposables = new SequentialDisposable();
83 this.current = new AtomicReference<>(NotificationLite.COMPLETE); // as if a previous completed
84 }
85
86 @Override
87 public void request(long n) {
88 if (SubscriptionHelper.validate(n)) {
89 BackpressureHelper.add(requested, n);
90 drain();
91 }
92 }
93
94 @Override
95 public void cancel() {
96 disposables.dispose();
97 }
98
99 @Override
100 public void onSubscribe(Disposable d) {
101 disposables.replace(d);
102 }
103
104 @Override
105 public void onSuccess(T value) {
106 current.lazySet(value);
107 drain();
108 }
109
110 @Override
111 public void onError(Throwable e) {
112 downstream.onError(e);
113 }
114
115 @Override
116 public void onComplete() {
117 current.lazySet(NotificationLite.COMPLETE);
118 drain();
119 }
120
121 @SuppressWarnings("unchecked")
122 void drain() {
123 if (getAndIncrement() != 0) {
124 return;
125 }
126
127 AtomicReference<Object> c = current;
128 Subscriber<? super T> a = downstream;
129 Disposable cancelled = disposables;
130
131 for (;;) {
132 if (cancelled.isDisposed()) {
133 c.lazySet(null);
134 return;
135 }
136
137 Object o = c.get();
138
139 if (o != null) {
140 boolean goNextSource;
141 if (o != NotificationLite.COMPLETE) {
142 long p = produced;
143 if (p != requested.get()) {
144 produced = p + 1;
145 c.lazySet(null);
146 goNextSource = true;
147
148 a.onNext((T)o);
149 } else {
150 goNextSource = false;
151 }
152 } else {
153 goNextSource = true;
154 c.lazySet(null);
155 }
156
157 if (goNextSource && !cancelled.isDisposed()) {
158 boolean b;
159
160 try {
161 b = sources.hasNext();
162 } catch (Throwable ex) {
163 Exceptions.throwIfFatal(ex);
164 a.onError(ex);
165 return;
166 }
167
168 if (b) {
169 MaybeSource<? extends T> source;
170
171 try {
172 source = Objects.requireNonNull(sources.next(), "The source Iterator returned a null MaybeSource");
173 } catch (Throwable ex) {
174 Exceptions.throwIfFatal(ex);
175 a.onError(ex);
176 return;
177 }
178
179 source.subscribe(this);
180 } else {
181 a.onComplete();
182 }
183 }
184 }
185
186 if (decrementAndGet() == 0) {
187 break;
188 }
189 }
190 }
191 }
192 }