Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableReduceMaybe.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.BiFunction;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 import java.util.Objects;
24
25 /**
26 * Reduce a sequence of values into a single value via an aggregator function and emit the final value or complete
27 * if the source is empty.
28 *
29 * @param <T> the source and result value type
30 */
31 public final class ObservableReduceMaybe<T> extends Maybe<T> {
32
33 final ObservableSource<T> source;
34
35 final BiFunction<T, T, T> reducer;
36
37 public ObservableReduceMaybe(ObservableSource<T> source, BiFunction<T, T, T> reducer) {
38 this.source = source;
39 this.reducer = reducer;
40 }
41
42 @Override
43 protected void subscribeActual(MaybeObserver<? super T> observer) {
44 source.subscribe(new ReduceObserver<>(observer, reducer));
45 }
46
47 static final class ReduceObserver<T> implements Observer<T>, Disposable {
48
49 final MaybeObserver<? super T> downstream;
50
51 final BiFunction<T, T, T> reducer;
52
53 boolean done;
54
55 T value;
56
57 Disposable upstream;
58
59 ReduceObserver(MaybeObserver<? super T> observer, BiFunction<T, T, T> reducer) {
60 this.downstream = observer;
61 this.reducer = reducer;
62 }
63
64 @Override
65 public void onSubscribe(Disposable d) {
66 if (DisposableHelper.validate(this.upstream, d)) {
67 this.upstream = d;
68
69 downstream.onSubscribe(this);
70 }
71 }
72
73 @Override
74 public void onNext(T value) {
75 if (!done) {
76 T v = this.value;
77
78 if (v == null) {
79 this.value = value;
80 } else {
81 try {
82 this.value = Objects.requireNonNull(reducer.apply(v, value), "The reducer returned a null value");
83 } catch (Throwable ex) {
84 Exceptions.throwIfFatal(ex);
85 upstream.dispose();
86 onError(ex);
87 }
88 }
89 }
90 }
91
92 @Override
93 public void onError(Throwable e) {
94 if (done) {
95 RxJavaPlugins.onError(e);
96 return;
97 }
98 done = true;
99 value = null;
100 downstream.onError(e);
101 }
102
103 @Override
104 public void onComplete() {
105 if (done) {
106 return;
107 }
108 done = true;
109 T v = value;
110 value = null;
111 if (v != null) {
112 downstream.onSuccess(v);
113 } else {
114 downstream.onComplete();
115 }
116 }
117
118 @Override
119 public void dispose() {
120 upstream.dispose();
121 }
122
123 @Override
124 public boolean isDisposed() {
125 return upstream.isDisposed();
126 }
127 }
128 }