Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableReduceWithSingle.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.Publisher;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.*;
21 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
22 import io.reactivex.rxjava3.internal.operators.flowable.FlowableReduceSeedSingle.ReduceSeedObserver;
23
24 import java.util.Objects;
25
26 /**
27 * Reduce a sequence of values, starting from a generated seed value and by using
28 * an accumulator function and return the last accumulated value.
29 *
30 * @param <T> the source value type
31 * @param <R> the accumulated result type
32 */
33 public final class FlowableReduceWithSingle<T, R> extends Single<R> {
34
35 final Publisher<T> source;
36
37 final Supplier<R> seedSupplier;
38
39 final BiFunction<R, ? super T, R> reducer;
40
41 public FlowableReduceWithSingle(Publisher<T> source, Supplier<R> seedSupplier, BiFunction<R, ? super T, R> reducer) {
42 this.source = source;
43 this.seedSupplier = seedSupplier;
44 this.reducer = reducer;
45 }
46
47 @Override
48 protected void subscribeActual(SingleObserver<? super R> observer) {
49 R seed;
50
51 try {
52 seed = Objects.requireNonNull(seedSupplier.get(), "The seedSupplier returned a null value");
53 } catch (Throwable ex) {
54 Exceptions.throwIfFatal(ex);
55 EmptyDisposable.error(ex, observer);
56 return;
57 }
58 source.subscribe(new ReduceSeedObserver<>(observer, reducer, seed));
59 }
60 }