Debian Codegraph @

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