Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableGenerate.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.concurrent.atomic.AtomicLong;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.*;
23 import io.reactivex.rxjava3.internal.subscriptions.*;
24 import io.reactivex.rxjava3.internal.util.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 public final class FlowableGenerate<T, S> extends Flowable<T> {
28 final Supplier<S> stateSupplier;
29 final BiFunction<S, Emitter<T>, S> generator;
30 final Consumer<? super S> disposeState;
31
32 public FlowableGenerate(Supplier<S> stateSupplier, BiFunction<S, Emitter<T>, S> generator,
33 Consumer<? super S> disposeState) {
34 this.stateSupplier = stateSupplier;
35 this.generator = generator;
36 this.disposeState = disposeState;
37 }
38
39 @Override
40 public void subscribeActual(Subscriber<? super T> s) {
41 S state;
42
43 try {
44 state = stateSupplier.get();
45 } catch (Throwable e) {
46 Exceptions.throwIfFatal(e);
47 EmptySubscription.error(e, s);
48 return;
49 }
50
51 s.onSubscribe(new GeneratorSubscription<>(s, generator, disposeState, state));
52 }
53
54 static final class GeneratorSubscription<T, S>
55 extends AtomicLong
56 implements Emitter<T>, Subscription {
57
58 private static final long serialVersionUID = 7565982551505011832L;
59
60 final Subscriber<? super T> downstream;
61 final BiFunction<S, ? super Emitter<T>, S> generator;
62 final Consumer<? super S> disposeState;
63
64 S state;
65
66 volatile boolean cancelled;
67
68 boolean terminate;
69
70 boolean hasNext;
71
72 GeneratorSubscription(Subscriber<? super T> actual,
73 BiFunction<S, ? super Emitter<T>, S> generator,
74 Consumer<? super S> disposeState, S initialState) {
75 this.downstream = actual;
76 this.generator = generator;
77 this.disposeState = disposeState;
78 this.state = initialState;
79 }
80
81 @Override
82 public void request(long n) {
83 if (!SubscriptionHelper.validate(n)) {
84 return;
85 }
86 if (BackpressureHelper.add(this, n) != 0L) {
87 return;
88 }
89
90 long e = 0L;
91
92 S s = state;
93
94 final BiFunction<S, ? super Emitter<T>, S> f = generator;
95
96 for (;;) {
97 while (e != n) {
98
99 if (cancelled) {
100 state = null;
101 dispose(s);
102 return;
103 }
104
105 hasNext = false;
106
107 try {
108 s = f.apply(s, this);
109 } catch (Throwable ex) {
110 Exceptions.throwIfFatal(ex);
111 cancelled = true;
112 state = null;
113 onError(ex);
114 dispose(s);
115 return;
116 }
117
118 if (terminate) {
119 cancelled = true;
120 state = null;
121 dispose(s);
122 return;
123 }
124
125 e++;
126 }
127
128 n = get();
129 if (e == n) {
130 state = s;
131 n = addAndGet(-e);
132 if (n == 0L) {
133 break;
134 }
135 e = 0L;
136 }
137 }
138 }
139
140 private void dispose(S s) {
141 try {
142 disposeState.accept(s);
143 } catch (Throwable ex) {
144 Exceptions.throwIfFatal(ex);
145 RxJavaPlugins.onError(ex);
146 }
147 }
148
149 @Override
150 public void cancel() {
151 if (!cancelled) {
152 cancelled = true;
153
154 // if there are no running requests, just dispose the state
155 if (BackpressureHelper.add(this, 1) == 0) {
156 S s = state;
157 state = null;
158 dispose(s);
159 }
160 }
161 }
162
163 @Override
164 public void onNext(T t) {
165 if (!terminate) {
166 if (hasNext) {
167 onError(new IllegalStateException("onNext already called in this generate turn"));
168 } else {
169 if (t == null) {
170 onError(ExceptionHelper.createNullPointerException("onNext called with a null value."));
171 } else {
172 hasNext = true;
173 downstream.onNext(t);
174 }
175 }
176 }
177 }
178
179 @Override
180 public void onError(Throwable t) {
181 if (terminate) {
182 RxJavaPlugins.onError(t);
183 } else {
184 if (t == null) {
185 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
186 }
187 terminate = true;
188 downstream.onError(t);
189 }
190 }
191
192 @Override
193 public void onComplete() {
194 if (!terminate) {
195 terminate = true;
196 downstream.onComplete();
197 }
198 }
199 }
200 }