| 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.observable; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
import java.util.Objects; |
| 24 |
|
| 25 |
public final class ObservableScanSeed<T, R> extends AbstractObservableWithUpstream<T, R> { |
| 26 |
final BiFunction<R, ? super T, R> accumulator; |
| 27 |
final Supplier<R> seedSupplier; |
| 28 |
|
| 29 |
public ObservableScanSeed(ObservableSource<T> source, Supplier<R> seedSupplier, BiFunction<R, ? super T, R> accumulator) { |
| 30 |
super(source); |
| 31 |
this.accumulator = accumulator; |
| 32 |
this.seedSupplier = seedSupplier; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void subscribeActual(Observer<? super R> t) { |
| 37 |
R r; |
| 38 |
|
| 39 |
try { |
| 40 |
r = Objects.requireNonNull(seedSupplier.get(), "The seed supplied is null"); |
| 41 |
} catch (Throwable e) { |
| 42 |
Exceptions.throwIfFatal(e); |
| 43 |
EmptyDisposable.error(e, t); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
source.subscribe(new ScanSeedObserver<>(t, accumulator, r)); |
| 48 |
} |
| 49 |
|
| 50 |
static final class ScanSeedObserver<T, R> implements Observer<T>, Disposable { |
| 51 |
final Observer<? super R> downstream; |
| 52 |
final BiFunction<R, ? super T, R> accumulator; |
| 53 |
|
| 54 |
R value; |
| 55 |
|
| 56 |
Disposable upstream; |
| 57 |
|
| 58 |
boolean done; |
| 59 |
|
| 60 |
ScanSeedObserver(Observer<? super R> actual, BiFunction<R, ? super T, R> accumulator, R value) { |
| 61 |
this.downstream = actual; |
| 62 |
this.accumulator = accumulator; |
| 63 |
this.value = value; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onSubscribe(Disposable d) { |
| 68 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 69 |
this.upstream = d; |
| 70 |
|
| 71 |
downstream.onSubscribe(this); |
| 72 |
|
| 73 |
downstream.onNext(value); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void dispose() { |
| 79 |
upstream.dispose(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean isDisposed() { |
| 84 |
return upstream.isDisposed(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onNext(T t) { |
| 89 |
if (done) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
R v = value; |
| 94 |
|
| 95 |
R u; |
| 96 |
|
| 97 |
try { |
| 98 |
u = Objects.requireNonNull(accumulator.apply(v, t), "The accumulator returned a null value"); |
| 99 |
} catch (Throwable e) { |
| 100 |
Exceptions.throwIfFatal(e); |
| 101 |
upstream.dispose(); |
| 102 |
onError(e); |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
value = u; |
| 107 |
|
| 108 |
downstream.onNext(u); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public void onError(Throwable t) { |
| 113 |
if (done) { |
| 114 |
RxJavaPlugins.onError(t); |
| 115 |
return; |
| 116 |
} |
| 117 |
done = true; |
| 118 |
downstream.onError(t); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void onComplete() { |
| 123 |
if (done) { |
| 124 |
return; |
| 125 |
} |
| 126 |
done = true; |
| 127 |
downstream.onComplete(); |
| 128 |
} |
| 129 |
} |
| 130 |
} |