| 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 io.reactivex.rxjava3.annotations.NonNull; |
| 17 |
import io.reactivex.rxjava3.core.Flowable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 20 |
import io.reactivex.rxjava3.functions.Supplier; |
| 21 |
import org.reactivestreams.Subscriber; |
| 22 |
|
| 23 |
import java.util.Objects; |
| 24 |
|
| 25 |
public final class FlowableOnBackpressureReduceWith<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 26 |
|
| 27 |
final BiFunction<R, ? super T, R> reducer; |
| 28 |
final Supplier<R> supplier; |
| 29 |
|
| 30 |
public FlowableOnBackpressureReduceWith(@NonNull Flowable<T> source, |
| 31 |
@NonNull Supplier<R> supplier, |
| 32 |
@NonNull BiFunction<R, ? super T, R> reducer) { |
| 33 |
super(source); |
| 34 |
this.reducer = reducer; |
| 35 |
this.supplier = supplier; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void subscribeActual(@NonNull Subscriber<? super R> s) { |
| 40 |
source.subscribe(new BackpressureReduceWithSubscriber<>(s, supplier, reducer)); |
| 41 |
} |
| 42 |
|
| 43 |
static final class BackpressureReduceWithSubscriber<T, R> extends AbstractBackpressureThrottlingSubscriber<T, R> { |
| 44 |
|
| 45 |
private static final long serialVersionUID = 8255923705960622424L; |
| 46 |
|
| 47 |
final BiFunction<R, ? super T, R> reducer; |
| 48 |
final Supplier<R> supplier; |
| 49 |
|
| 50 |
BackpressureReduceWithSubscriber(@NonNull Subscriber<? super R> downstream, |
| 51 |
@NonNull Supplier<R> supplier, |
| 52 |
@NonNull BiFunction<R, ? super T, R> reducer) { |
| 53 |
super(downstream); |
| 54 |
this.reducer = reducer; |
| 55 |
this.supplier = supplier; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void onNext(T t) { |
| 60 |
R v = current.get(); |
| 61 |
if (v != null) { |
| 62 |
v = current.getAndSet(null); |
| 63 |
} |
| 64 |
try { |
| 65 |
if (v == null) { |
| 66 |
current.lazySet(Objects.requireNonNull( |
| 67 |
reducer.apply(Objects.requireNonNull(supplier.get(), "The supplier returned a null value"), t), |
| 68 |
"The reducer returned a null value" |
| 69 |
)); |
| 70 |
} else { |
| 71 |
current.lazySet(Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value")); |
| 72 |
} |
| 73 |
} catch (Throwable ex) { |
| 74 |
Exceptions.throwIfFatal(ex); |
| 75 |
upstream.cancel(); |
| 76 |
onError(ex); |
| 77 |
return; |
| 78 |
} |
| 79 |
drain(); |
| 80 |
} |
| 81 |
} |
| 82 |
} |