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