Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/jdk8/FlowableCollectWithCollectorSingle.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.jdk8;
15
16 import java.util.Objects;
17 import java.util.function.*;
18 import java.util.stream.Collector;
19
20 import org.reactivestreams.Subscription;
21
22 import io.reactivex.rxjava3.annotations.NonNull;
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.Exceptions;
26 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
27 import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable;
28 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30
31 /**
32 * Collect items into a container defined by a Stream {@link Collector} callback set.
33 *
34 * @param <T> the upstream value type
35 * @param <A> the intermediate accumulator type
36 * @param <R> the result type
37 * @since 3.0.0
38 */
39 public final class FlowableCollectWithCollectorSingle<T, A, R> extends Single<R> implements FuseToFlowable<R> {
40
41 final Flowable<T> source;
42
43 final Collector<? super T, A, R> collector;
44
45 public FlowableCollectWithCollectorSingle(Flowable<T> source, Collector<? super T, A, R> collector) {
46 this.source = source;
47 this.collector = collector;
48 }
49
50 @Override
51 public Flowable<R> fuseToFlowable() {
52 return new FlowableCollectWithCollector<>(source, collector);
53 }
54
55 @Override
56 protected void subscribeActual(@NonNull SingleObserver<? super R> observer) {
57 A container;
58 BiConsumer<A, ? super T> accumulator;
59 Function<A, R> finisher;
60
61 try {
62 container = collector.supplier().get();
63 accumulator = collector.accumulator();
64 finisher = collector.finisher();
65 } catch (Throwable ex) {
66 Exceptions.throwIfFatal(ex);
67 EmptyDisposable.error(ex, observer);
68 return;
69 }
70
71 source.subscribe(new CollectorSingleObserver<>(observer, container, accumulator, finisher));
72 }
73
74 static final class CollectorSingleObserver<T, A, R> implements FlowableSubscriber<T>, Disposable {
75
76 final SingleObserver<? super R> downstream;
77
78 final BiConsumer<A, T> accumulator;
79
80 final Function<A, R> finisher;
81
82 Subscription upstream;
83
84 boolean done;
85
86 A container;
87
88 CollectorSingleObserver(SingleObserver<? super R> downstream, A container, BiConsumer<A, T> accumulator, Function<A, R> finisher) {
89 this.downstream = downstream;
90 this.container = container;
91 this.accumulator = accumulator;
92 this.finisher = finisher;
93 }
94
95 @Override
96 public void onSubscribe(@NonNull Subscription s) {
97 if (SubscriptionHelper.validate(this.upstream, s)) {
98 this.upstream = s;
99
100 downstream.onSubscribe(this);
101
102 s.request(Long.MAX_VALUE);
103 }
104 }
105
106 @Override
107 public void onNext(T t) {
108 if (done) {
109 return;
110 }
111 try {
112 accumulator.accept(container, t);
113 } catch (Throwable ex) {
114 Exceptions.throwIfFatal(ex);
115 upstream.cancel();
116 onError(ex);
117 }
118 }
119
120 @Override
121 public void onError(Throwable t) {
122 if (done) {
123 RxJavaPlugins.onError(t);
124 } else {
125 done = true;
126 upstream = SubscriptionHelper.CANCELLED;
127 this.container = null;
128 downstream.onError(t);
129 }
130 }
131
132 @Override
133 public void onComplete() {
134 if (done) {
135 return;
136 }
137
138 done = true;
139 upstream = SubscriptionHelper.CANCELLED;
140 A container = this.container;
141 this.container = null;
142 R result;
143 try {
144 result = Objects.requireNonNull(finisher.apply(container), "The finisher returned a null value");
145 } catch (Throwable ex) {
146 Exceptions.throwIfFatal(ex);
147 downstream.onError(ex);
148 return;
149 }
150
151 downstream.onSuccess(result);
152 }
153
154 @Override
155 public void dispose() {
156 upstream.cancel();
157 upstream = SubscriptionHelper.CANCELLED;
158 }
159
160 @Override
161 public boolean isDisposed() {
162 return upstream == SubscriptionHelper.CANCELLED;
163 }
164 }
165 }