Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableCollect.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 org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.*;
21 import io.reactivex.rxjava3.internal.subscriptions.*;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 import java.util.Objects;
25
26 public final class FlowableCollect<T, U> extends AbstractFlowableWithUpstream<T, U> {
27
28 final Supplier<? extends U> initialSupplier;
29 final BiConsumer<? super U, ? super T> collector;
30
31 public FlowableCollect(Flowable<T> source, Supplier<? extends U> initialSupplier, BiConsumer<? super U, ? super T> collector) {
32 super(source);
33 this.initialSupplier = initialSupplier;
34 this.collector = collector;
35 }
36
37 @Override
38 protected void subscribeActual(Subscriber<? super U> s) {
39 U u;
40 try {
41 u = Objects.requireNonNull(initialSupplier.get(), "The initial value supplied is null");
42 } catch (Throwable e) {
43 Exceptions.throwIfFatal(e);
44 EmptySubscription.error(e, s);
45 return;
46 }
47
48 source.subscribe(new CollectSubscriber<>(s, u, collector));
49 }
50
51 static final class CollectSubscriber<T, U> extends DeferredScalarSubscription<U> implements FlowableSubscriber<T> {
52
53 private static final long serialVersionUID = -3589550218733891694L;
54
55 final BiConsumer<? super U, ? super T> collector;
56
57 final U u;
58
59 Subscription upstream;
60
61 boolean done;
62
63 CollectSubscriber(Subscriber<? super U> actual, U u, BiConsumer<? super U, ? super T> collector) {
64 super(actual);
65 this.collector = collector;
66 this.u = u;
67 }
68
69 @Override
70 public void onSubscribe(Subscription s) {
71 if (SubscriptionHelper.validate(this.upstream, s)) {
72 this.upstream = s;
73 downstream.onSubscribe(this);
74 s.request(Long.MAX_VALUE);
75 }
76 }
77
78 @Override
79 public void onNext(T t) {
80 if (done) {
81 return;
82 }
83 try {
84 collector.accept(u, t);
85 } catch (Throwable e) {
86 Exceptions.throwIfFatal(e);
87 upstream.cancel();
88 onError(e);
89 }
90 }
91
92 @Override
93 public void onError(Throwable t) {
94 if (done) {
95 RxJavaPlugins.onError(t);
96 return;
97 }
98 done = true;
99 downstream.onError(t);
100 }
101
102 @Override
103 public void onComplete() {
104 if (done) {
105 return;
106 }
107 done = true;
108 complete(u);
109 }
110
111 @Override
112 public void cancel() {
113 super.cancel();
114 upstream.cancel();
115 }
116 }
117 }