| 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.internal.fuseable.FuseToObservable; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
public final class ObservableCollectSingle<T, U> extends Single<U> implements FuseToObservable<U> { |
| 27 |
|
| 28 |
final ObservableSource<T> source; |
| 29 |
|
| 30 |
final Supplier<? extends U> initialSupplier; |
| 31 |
final BiConsumer<? super U, ? super T> collector; |
| 32 |
|
| 33 |
public ObservableCollectSingle(ObservableSource<T> source, |
| 34 |
Supplier<? extends U> initialSupplier, BiConsumer<? super U, ? super T> collector) { |
| 35 |
this.source = source; |
| 36 |
this.initialSupplier = initialSupplier; |
| 37 |
this.collector = collector; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(SingleObserver<? super U> t) { |
| 42 |
U u; |
| 43 |
try { |
| 44 |
u = Objects.requireNonNull(initialSupplier.get(), "The initialSupplier returned a null value"); |
| 45 |
} catch (Throwable e) { |
| 46 |
Exceptions.throwIfFatal(e); |
| 47 |
EmptyDisposable.error(e, t); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
source.subscribe(new CollectObserver<>(t, u, collector)); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public Observable<U> fuseToObservable() { |
| 56 |
return RxJavaPlugins.onAssembly(new ObservableCollect<>(source, initialSupplier, collector)); |
| 57 |
} |
| 58 |
|
| 59 |
static final class CollectObserver<T, U> implements Observer<T>, Disposable { |
| 60 |
final SingleObserver<? super U> downstream; |
| 61 |
final BiConsumer<? super U, ? super T> collector; |
| 62 |
final U u; |
| 63 |
|
| 64 |
Disposable upstream; |
| 65 |
|
| 66 |
boolean done; |
| 67 |
|
| 68 |
CollectObserver(SingleObserver<? super U> actual, U u, BiConsumer<? super U, ? super T> collector) { |
| 69 |
this.downstream = actual; |
| 70 |
this.collector = collector; |
| 71 |
this.u = u; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onSubscribe(Disposable d) { |
| 76 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 77 |
this.upstream = d; |
| 78 |
downstream.onSubscribe(this); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void dispose() { |
| 84 |
upstream.dispose(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean isDisposed() { |
| 89 |
return upstream.isDisposed(); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onNext(T t) { |
| 94 |
if (done) { |
| 95 |
return; |
| 96 |
} |
| 97 |
try { |
| 98 |
collector.accept(u, t); |
| 99 |
} catch (Throwable e) { |
| 100 |
Exceptions.throwIfFatal(e); |
| 101 |
upstream.dispose(); |
| 102 |
onError(e); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onError(Throwable t) { |
| 108 |
if (done) { |
| 109 |
RxJavaPlugins.onError(t); |
| 110 |
return; |
| 111 |
} |
| 112 |
done = true; |
| 113 |
downstream.onError(t); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onComplete() { |
| 118 |
if (done) { |
| 119 |
return; |
| 120 |
} |
| 121 |
done = true; |
| 122 |
downstream.onSuccess(u); |
| 123 |
} |
| 124 |
} |
| 125 |
} |