| 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.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
/** |
| 27 |
* Reduces a sequence via a function into a single value or signals NoSuchElementException for |
| 28 |
* an empty source. |
| 29 |
* |
| 30 |
* @param <T> the value type |
| 31 |
*/ |
| 32 |
public final class FlowableReduce<T> extends AbstractFlowableWithUpstream<T, T> { |
| 33 |
|
| 34 |
final BiFunction<T, T, T> reducer; |
| 35 |
|
| 36 |
public FlowableReduce(Flowable<T> source, BiFunction<T, T, T> reducer) { |
| 37 |
super(source); |
| 38 |
this.reducer = reducer; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 43 |
source.subscribe(new ReduceSubscriber<>(s, reducer)); |
| 44 |
} |
| 45 |
|
| 46 |
static final class ReduceSubscriber<T> extends DeferredScalarSubscription<T> implements FlowableSubscriber<T> { |
| 47 |
|
| 48 |
private static final long serialVersionUID = -4663883003264602070L; |
| 49 |
|
| 50 |
final BiFunction<T, T, T> reducer; |
| 51 |
|
| 52 |
Subscription upstream; |
| 53 |
|
| 54 |
ReduceSubscriber(Subscriber<? super T> actual, BiFunction<T, T, T> reducer) { |
| 55 |
super(actual); |
| 56 |
this.reducer = reducer; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onSubscribe(Subscription s) { |
| 61 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 62 |
this.upstream = s; |
| 63 |
|
| 64 |
downstream.onSubscribe(this); |
| 65 |
|
| 66 |
s.request(Long.MAX_VALUE); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onNext(T t) { |
| 72 |
if (upstream == SubscriptionHelper.CANCELLED) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
T v = value; |
| 77 |
if (v == null) { |
| 78 |
value = t; |
| 79 |
} else { |
| 80 |
try { |
| 81 |
value = Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value"); |
| 82 |
} catch (Throwable ex) { |
| 83 |
Exceptions.throwIfFatal(ex); |
| 84 |
upstream.cancel(); |
| 85 |
onError(ex); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onError(Throwable t) { |
| 92 |
if (upstream == SubscriptionHelper.CANCELLED) { |
| 93 |
RxJavaPlugins.onError(t); |
| 94 |
return; |
| 95 |
} |
| 96 |
upstream = SubscriptionHelper.CANCELLED; |
| 97 |
downstream.onError(t); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onComplete() { |
| 102 |
if (upstream == SubscriptionHelper.CANCELLED) { |
| 103 |
return; |
| 104 |
} |
| 105 |
upstream = SubscriptionHelper.CANCELLED; |
| 106 |
|
| 107 |
T v = value; |
| 108 |
if (v != null) { |
| 109 |
complete(v); |
| 110 |
} else { |
| 111 |
downstream.onComplete(); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void cancel() { |
| 117 |
super.cancel(); |
| 118 |
upstream.cancel(); |
| 119 |
upstream = SubscriptionHelper.CANCELLED; |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
} |