Debian Codegraph @

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