Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapIterableObservable.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
19 import io.reactivex.rxjava3.annotations.Nullable;
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
25 import io.reactivex.rxjava3.internal.observers.BasicQueueDisposable;
26
27 /**
28 * Maps a success value into an Iterable and streams it back as a Flowable.
29 *
30 * @param <T> the source value type
31 * @param <R> the element type of the Iterable
32 */
33 public final class MaybeFlatMapIterableObservable<T, R> extends Observable<R> {
34
35 final MaybeSource<T> source;
36
37 final Function<? super T, ? extends Iterable<? extends R>> mapper;
38
39 public MaybeFlatMapIterableObservable(MaybeSource<T> source,
40 Function<? super T, ? extends Iterable<? extends R>> mapper) {
41 this.source = source;
42 this.mapper = mapper;
43 }
44
45 @Override
46 protected void subscribeActual(Observer<? super R> observer) {
47 source.subscribe(new FlatMapIterableObserver<>(observer, mapper));
48 }
49
50 static final class FlatMapIterableObserver<T, R>
51 extends BasicQueueDisposable<R>
52 implements MaybeObserver<T> {
53
54 final Observer<? super R> downstream;
55
56 final Function<? super T, ? extends Iterable<? extends R>> mapper;
57
58 Disposable upstream;
59
60 volatile Iterator<? extends R> it;
61
62 volatile boolean cancelled;
63
64 boolean outputFused;
65
66 FlatMapIterableObserver(Observer<? super R> actual,
67 Function<? super T, ? extends Iterable<? extends R>> mapper) {
68 this.downstream = actual;
69 this.mapper = mapper;
70 }
71
72 @Override
73 public void onSubscribe(Disposable d) {
74 if (DisposableHelper.validate(this.upstream, d)) {
75 this.upstream = d;
76
77 downstream.onSubscribe(this);
78 }
79 }
80
81 @Override
82 public void onSuccess(T value) {
83 Observer<? super R> a = downstream;
84
85 Iterator<? extends R> iterator;
86 boolean has;
87 try {
88 iterator = mapper.apply(value).iterator();
89
90 has = iterator.hasNext();
91 } catch (Throwable ex) {
92 Exceptions.throwIfFatal(ex);
93 a.onError(ex);
94 return;
95 }
96
97 if (!has) {
98 a.onComplete();
99 return;
100 }
101
102 this.it = iterator;
103
104 if (outputFused) {
105 a.onNext(null);
106 a.onComplete();
107 return;
108 }
109
110 for (;;) {
111 if (cancelled) {
112 return;
113 }
114
115 R v;
116
117 try {
118 v = iterator.next();
119 } catch (Throwable ex) {
120 Exceptions.throwIfFatal(ex);
121 a.onError(ex);
122 return;
123 }
124
125 a.onNext(v);
126
127 if (cancelled) {
128 return;
129 }
130
131 boolean b;
132
133 try {
134 b = iterator.hasNext();
135 } catch (Throwable ex) {
136 Exceptions.throwIfFatal(ex);
137 a.onError(ex);
138 return;
139 }
140
141 if (!b) {
142 a.onComplete();
143 return;
144 }
145 }
146 }
147
148 @Override
149 public void onError(Throwable e) {
150 upstream = DisposableHelper.DISPOSED;
151 downstream.onError(e);
152 }
153
154 @Override
155 public void onComplete() {
156 downstream.onComplete();
157 }
158
159 @Override
160 public void dispose() {
161 cancelled = true;
162 upstream.dispose();
163 upstream = DisposableHelper.DISPOSED;
164 }
165
166 @Override
167 public boolean isDisposed() {
168 return cancelled;
169 }
170
171 @Override
172 public int requestFusion(int mode) {
173 if ((mode & ASYNC) != 0) {
174 outputFused = true;
175 return ASYNC;
176 }
177 return NONE;
178 }
179
180 @Override
181 public void clear() {
182 it = null;
183 }
184
185 @Override
186 public boolean isEmpty() {
187 return it == null;
188 }
189
190 @Nullable
191 @Override
192 public R poll() {
193 Iterator<? extends R> iterator = it;
194
195 if (iterator != null) {
196 R v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
197 if (!iterator.hasNext()) {
198 it = null;
199 }
200 return v;
201 }
202 return null;
203 }
204
205 }
206 }