Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableReduceSeedSingle.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, starting from a seed value and by using
27 * an accumulator function and return the last accumulated value.
28 *
29 * @param <T> the source value type
30 * @param <R> the accumulated result type
31 */
32 public final class ObservableReduceSeedSingle<T, R> extends Single<R> {
33
34 final ObservableSource<T> source;
35
36 final R seed;
37
38 final BiFunction<R, ? super T, R> reducer;
39
40 public ObservableReduceSeedSingle(ObservableSource<T> source, R seed, BiFunction<R, ? super T, R> reducer) {
41 this.source = source;
42 this.seed = seed;
43 this.reducer = reducer;
44 }
45
46 @Override
47 protected void subscribeActual(SingleObserver<? super R> observer) {
48 source.subscribe(new ReduceSeedObserver<>(observer, reducer, seed));
49 }
50
51 static final class ReduceSeedObserver<T, R> implements Observer<T>, Disposable {
52
53 final SingleObserver<? super R> downstream;
54
55 final BiFunction<R, ? super T, R> reducer;
56
57 R value;
58
59 Disposable upstream;
60
61 ReduceSeedObserver(SingleObserver<? super R> actual, BiFunction<R, ? super T, R> reducer, R value) {
62 this.downstream = actual;
63 this.value = value;
64 this.reducer = reducer;
65 }
66
67 @Override
68 public void onSubscribe(Disposable d) {
69 if (DisposableHelper.validate(this.upstream, d)) {
70 this.upstream = d;
71
72 downstream.onSubscribe(this);
73 }
74 }
75
76 @Override
77 public void onNext(T value) {
78 R v = this.value;
79 if (v != null) {
80 try {
81 this.value = Objects.requireNonNull(reducer.apply(v, value), "The reducer returned a null value");
82 } catch (Throwable ex) {
83 Exceptions.throwIfFatal(ex);
84 upstream.dispose();
85 onError(ex);
86 }
87 }
88 }
89
90 @Override
91 public void onError(Throwable e) {
92 R v = value;
93 if (v != null) {
94 value = null;
95 downstream.onError(e);
96 } else {
97 RxJavaPlugins.onError(e);
98 }
99 }
100
101 @Override
102 public void onComplete() {
103 R v = value;
104 if (v != null) {
105 value = null;
106 downstream.onSuccess(v);
107 }
108 }
109
110 @Override
111 public void dispose() {
112 upstream.dispose();
113 }
114
115 @Override
116 public boolean isDisposed() {
117 return upstream.isDisposed();
118 }
119 }
120 }