Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableInternalHelper.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.Objects;
17 import java.util.concurrent.TimeUnit;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.functions.*;
25
26 /**
27 * Helper utility class to support Flowable with inner classes.
28 */
29 public final class FlowableInternalHelper {
30
31 /** Utility class. */
32 private FlowableInternalHelper() {
33 throw new IllegalStateException("No instances!");
34 }
35
36 static final class SimpleGenerator<T, S> implements BiFunction<S, Emitter<T>, S> {
37 final Consumer<Emitter<T>> consumer;
38
39 SimpleGenerator(Consumer<Emitter<T>> consumer) {
40 this.consumer = consumer;
41 }
42
43 @Override
44 public S apply(S t1, Emitter<T> t2) throws Throwable {
45 consumer.accept(t2);
46 return t1;
47 }
48 }
49
50 public static <T, S> BiFunction<S, Emitter<T>, S> simpleGenerator(Consumer<Emitter<T>> consumer) {
51 return new SimpleGenerator<>(consumer);
52 }
53
54 static final class SimpleBiGenerator<T, S> implements BiFunction<S, Emitter<T>, S> {
55 final BiConsumer<S, Emitter<T>> consumer;
56
57 SimpleBiGenerator(BiConsumer<S, Emitter<T>> consumer) {
58 this.consumer = consumer;
59 }
60
61 @Override
62 public S apply(S t1, Emitter<T> t2) throws Throwable {
63 consumer.accept(t1, t2);
64 return t1;
65 }
66 }
67
68 public static <T, S> BiFunction<S, Emitter<T>, S> simpleBiGenerator(BiConsumer<S, Emitter<T>> consumer) {
69 return new SimpleBiGenerator<>(consumer);
70 }
71
72 static final class ItemDelayFunction<T, U> implements Function<T, Publisher<T>> {
73 final Function<? super T, ? extends Publisher<U>> itemDelay;
74
75 ItemDelayFunction(Function<? super T, ? extends Publisher<U>> itemDelay) {
76 this.itemDelay = itemDelay;
77 }
78
79 @Override
80 public Publisher<T> apply(final T v) throws Throwable {
81 Publisher<U> p = Objects.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null Publisher");
82 return new FlowableTakePublisher<>(p, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
83 }
84 }
85
86 public static <T, U> Function<T, Publisher<T>> itemDelay(final Function<? super T, ? extends Publisher<U>> itemDelay) {
87 return new ItemDelayFunction<>(itemDelay);
88 }
89
90 static final class SubscriberOnNext<T> implements Consumer<T> {
91 final Subscriber<T> subscriber;
92
93 SubscriberOnNext(Subscriber<T> subscriber) {
94 this.subscriber = subscriber;
95 }
96
97 @Override
98 public void accept(T v) {
99 subscriber.onNext(v);
100 }
101 }
102
103 static final class SubscriberOnError<T> implements Consumer<Throwable> {
104 final Subscriber<T> subscriber;
105
106 SubscriberOnError(Subscriber<T> subscriber) {
107 this.subscriber = subscriber;
108 }
109
110 @Override
111 public void accept(Throwable v) {
112 subscriber.onError(v);
113 }
114 }
115
116 static final class SubscriberOnComplete<T> implements Action {
117 final Subscriber<T> subscriber;
118
119 SubscriberOnComplete(Subscriber<T> subscriber) {
120 this.subscriber = subscriber;
121 }
122
123 @Override
124 public void run() {
125 subscriber.onComplete();
126 }
127 }
128
129 public static <T> Consumer<T> subscriberOnNext(Subscriber<T> subscriber) {
130 return new SubscriberOnNext<>(subscriber);
131 }
132
133 public static <T> Consumer<Throwable> subscriberOnError(Subscriber<T> subscriber) {
134 return new SubscriberOnError<>(subscriber);
135 }
136
137 public static <T> Action subscriberOnComplete(Subscriber<T> subscriber) {
138 return new SubscriberOnComplete<>(subscriber);
139 }
140
141 static final class FlatMapWithCombinerInner<U, R, T> implements Function<U, R> {
142 private final BiFunction<? super T, ? super U, ? extends R> combiner;
143 private final T t;
144
145 FlatMapWithCombinerInner(BiFunction<? super T, ? super U, ? extends R> combiner, T t) {
146 this.combiner = combiner;
147 this.t = t;
148 }
149
150 @Override
151 public R apply(U w) throws Throwable {
152 return combiner.apply(t, w);
153 }
154 }
155
156 static final class FlatMapWithCombinerOuter<T, R, U> implements Function<T, Publisher<R>> {
157 private final BiFunction<? super T, ? super U, ? extends R> combiner;
158 private final Function<? super T, ? extends Publisher<? extends U>> mapper;
159
160 FlatMapWithCombinerOuter(BiFunction<? super T, ? super U, ? extends R> combiner,
161 Function<? super T, ? extends Publisher<? extends U>> mapper) {
162 this.combiner = combiner;
163 this.mapper = mapper;
164 }
165
166 @Override
167 public Publisher<R> apply(final T t) throws Throwable {
168 @SuppressWarnings("unchecked")
169 Publisher<U> u = (Publisher<U>)Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
170 return new FlowableMapPublisher<>(u, new FlatMapWithCombinerInner<U, R, T>(combiner, t));
171 }
172 }
173
174 public static <T, U, R> Function<T, Publisher<R>> flatMapWithCombiner(
175 final Function<? super T, ? extends Publisher<? extends U>> mapper,
176 final BiFunction<? super T, ? super U, ? extends R> combiner) {
177 return new FlatMapWithCombinerOuter<>(combiner, mapper);
178 }
179
180 static final class FlatMapIntoIterable<T, U> implements Function<T, Publisher<U>> {
181 private final Function<? super T, ? extends Iterable<? extends U>> mapper;
182
183 FlatMapIntoIterable(Function<? super T, ? extends Iterable<? extends U>> mapper) {
184 this.mapper = mapper;
185 }
186
187 @Override
188 public Publisher<U> apply(T t) throws Throwable {
189 return new FlowableFromIterable<>(Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Iterable"));
190 }
191 }
192
193 public static <T, U> Function<T, Publisher<U>> flatMapIntoIterable(final Function<? super T, ? extends Iterable<? extends U>> mapper) {
194 return new FlatMapIntoIterable<>(mapper);
195 }
196
197 public static <T> Supplier<ConnectableFlowable<T>> replaySupplier(final Flowable<T> parent) {
198 return new ReplaySupplier<>(parent);
199 }
200
201 public static <T> Supplier<ConnectableFlowable<T>> replaySupplier(final Flowable<T> parent, final int bufferSize, boolean eagerTruncate) {
202 return new BufferedReplaySupplier<>(parent, bufferSize, eagerTruncate);
203 }
204
205 public static <T> Supplier<ConnectableFlowable<T>> replaySupplier(final Flowable<T> parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler, boolean eagerTruncate) {
206 return new BufferedTimedReplay<>(parent, bufferSize, time, unit, scheduler, eagerTruncate);
207 }
208
209 public static <T> Supplier<ConnectableFlowable<T>> replaySupplier(final Flowable<T> parent, final long time, final TimeUnit unit, final Scheduler scheduler, boolean eagerTruncate) {
210 return new TimedReplay<>(parent, time, unit, scheduler, eagerTruncate);
211 }
212
213 public enum RequestMax implements Consumer<Subscription> {
214 INSTANCE;
215 @Override
216 public void accept(Subscription t) {
217 t.request(Long.MAX_VALUE);
218 }
219 }
220
221 static final class ReplaySupplier<T> implements Supplier<ConnectableFlowable<T>> {
222
223 final Flowable<T> parent;
224
225 ReplaySupplier(Flowable<T> parent) {
226 this.parent = parent;
227 }
228
229 @Override
230 public ConnectableFlowable<T> get() {
231 return parent.replay();
232 }
233 }
234
235 static final class BufferedReplaySupplier<T> implements Supplier<ConnectableFlowable<T>> {
236
237 final Flowable<T> parent;
238
239 final int bufferSize;
240
241 final boolean eagerTruncate;
242
243 BufferedReplaySupplier(Flowable<T> parent, int bufferSize, boolean eagerTruncate) {
244 this.parent = parent;
245 this.bufferSize = bufferSize;
246 this.eagerTruncate = eagerTruncate;
247 }
248
249 @Override
250 public ConnectableFlowable<T> get() {
251 return parent.replay(bufferSize, eagerTruncate);
252 }
253 }
254
255 static final class BufferedTimedReplay<T> implements Supplier<ConnectableFlowable<T>> {
256 final Flowable<T> parent;
257 final int bufferSize;
258 final long time;
259 final TimeUnit unit;
260 final Scheduler scheduler;
261
262 final boolean eagerTruncate;
263
264 BufferedTimedReplay(Flowable<T> parent, int bufferSize, long time, TimeUnit unit, Scheduler scheduler, boolean eagerTruncate) {
265 this.parent = parent;
266 this.bufferSize = bufferSize;
267 this.time = time;
268 this.unit = unit;
269 this.scheduler = scheduler;
270 this.eagerTruncate = eagerTruncate;
271 }
272
273 @Override
274 public ConnectableFlowable<T> get() {
275 return parent.replay(bufferSize, time, unit, scheduler, eagerTruncate);
276 }
277 }
278
279 static final class TimedReplay<T> implements Supplier<ConnectableFlowable<T>> {
280 private final Flowable<T> parent;
281 private final long time;
282 private final TimeUnit unit;
283 private final Scheduler scheduler;
284
285 final boolean eagerTruncate;
286
287 TimedReplay(Flowable<T> parent, long time, TimeUnit unit, Scheduler scheduler, boolean eagerTruncate) {
288 this.parent = parent;
289 this.time = time;
290 this.unit = unit;
291 this.scheduler = scheduler;
292 this.eagerTruncate = eagerTruncate;
293 }
294
295 @Override
296 public ConnectableFlowable<T> get() {
297 return parent.replay(time, unit, scheduler, eagerTruncate);
298 }
299 }
300 }