Debian Codegraph @

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