| 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.fuseable.*; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 25 |
|
| 26 |
import java.util.Objects; |
| 27 |
|
| 28 |
/** |
| 29 |
* Reduce a Flowable into a single value exposed as Single or signal NoSuchElementException. |
| 30 |
* |
| 31 |
* @param <T> the value type |
| 32 |
*/ |
| 33 |
public final class FlowableReduceMaybe<T> |
| 34 |
extends Maybe<T> |
| 35 |
implements HasUpstreamPublisher<T>, FuseToFlowable<T> { |
| 36 |
|
| 37 |
final Flowable<T> source; |
| 38 |
|
| 39 |
final BiFunction<T, T, T> reducer; |
| 40 |
|
| 41 |
public FlowableReduceMaybe(Flowable<T> source, BiFunction<T, T, T> reducer) { |
| 42 |
this.source = source; |
| 43 |
this.reducer = reducer; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public Publisher<T> source() { |
| 48 |
return source; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public Flowable<T> fuseToFlowable() { |
| 53 |
return RxJavaPlugins.onAssembly(new FlowableReduce<>(source, reducer)); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 58 |
source.subscribe(new ReduceSubscriber<>(observer, reducer)); |
| 59 |
} |
| 60 |
|
| 61 |
static final class ReduceSubscriber<T> implements FlowableSubscriber<T>, Disposable { |
| 62 |
final MaybeObserver<? super T> downstream; |
| 63 |
|
| 64 |
final BiFunction<T, T, T> reducer; |
| 65 |
|
| 66 |
T value; |
| 67 |
|
| 68 |
Subscription upstream; |
| 69 |
|
| 70 |
boolean done; |
| 71 |
|
| 72 |
ReduceSubscriber(MaybeObserver<? super T> actual, BiFunction<T, T, T> reducer) { |
| 73 |
this.downstream = actual; |
| 74 |
this.reducer = reducer; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void dispose() { |
| 79 |
upstream.cancel(); |
| 80 |
done = true; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public boolean isDisposed() { |
| 85 |
return done; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onSubscribe(Subscription s) { |
| 90 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 91 |
this.upstream = s; |
| 92 |
|
| 93 |
downstream.onSubscribe(this); |
| 94 |
|
| 95 |
s.request(Long.MAX_VALUE); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void onNext(T t) { |
| 101 |
if (done) { |
| 102 |
return; |
| 103 |
} |
| 104 |
T v = value; |
| 105 |
if (v == null) { |
| 106 |
value = t; |
| 107 |
} else { |
| 108 |
try { |
| 109 |
value = Objects.requireNonNull(reducer.apply(v, t), "The reducer returned a null value"); |
| 110 |
} catch (Throwable ex) { |
| 111 |
Exceptions.throwIfFatal(ex); |
| 112 |
upstream.cancel(); |
| 113 |
onError(ex); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onError(Throwable t) { |
| 120 |
if (done) { |
| 121 |
RxJavaPlugins.onError(t); |
| 122 |
return; |
| 123 |
} |
| 124 |
done = true; |
| 125 |
downstream.onError(t); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onComplete() { |
| 130 |
if (done) { |
| 131 |
return; |
| 132 |
} |
| 133 |
done = true; |
| 134 |
T v = value; |
| 135 |
if (v != null) { |
| 136 |
// value = null; |
| 137 |
downstream.onSuccess(v); |
| 138 |
} else { |
| 139 |
downstream.onComplete(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |