| 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.jdk8; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.function.*; |
| 18 |
import java.util.stream.Collector; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 25 |
import io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
/** |
| 29 |
* Collect items into a container defined by a Stream {@link Collector} callback set. |
| 30 |
* |
| 31 |
* @param <T> the upstream value type |
| 32 |
* @param <A> the intermediate accumulator type |
| 33 |
* @param <R> the result type |
| 34 |
* @since 3.0.0 |
| 35 |
*/ |
| 36 |
public final class ObservableCollectWithCollector<T, A, R> extends Observable<R> { |
| 37 |
|
| 38 |
final Observable<T> source; |
| 39 |
|
| 40 |
final Collector<? super T, A, R> collector; |
| 41 |
|
| 42 |
public ObservableCollectWithCollector(Observable<T> source, Collector<? super T, A, R> collector) { |
| 43 |
this.source = source; |
| 44 |
this.collector = collector; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected void subscribeActual(@NonNull Observer<? super R> observer) { |
| 49 |
A container; |
| 50 |
BiConsumer<A, ? super T> accumulator; |
| 51 |
Function<A, R> finisher; |
| 52 |
|
| 53 |
try { |
| 54 |
container = collector.supplier().get(); |
| 55 |
accumulator = collector.accumulator(); |
| 56 |
finisher = collector.finisher(); |
| 57 |
} catch (Throwable ex) { |
| 58 |
Exceptions.throwIfFatal(ex); |
| 59 |
EmptyDisposable.error(ex, observer); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
source.subscribe(new CollectorObserver<>(observer, container, accumulator, finisher)); |
| 64 |
} |
| 65 |
|
| 66 |
static final class CollectorObserver<T, A, R> |
| 67 |
extends DeferredScalarDisposable<R> |
| 68 |
implements Observer<T> { |
| 69 |
|
| 70 |
private static final long serialVersionUID = -229544830565448758L; |
| 71 |
|
| 72 |
final BiConsumer<A, T> accumulator; |
| 73 |
|
| 74 |
final Function<A, R> finisher; |
| 75 |
|
| 76 |
Disposable upstream; |
| 77 |
|
| 78 |
boolean done; |
| 79 |
|
| 80 |
A container; |
| 81 |
|
| 82 |
CollectorObserver(Observer<? super R> downstream, A container, BiConsumer<A, T> accumulator, Function<A, R> finisher) { |
| 83 |
super(downstream); |
| 84 |
this.container = container; |
| 85 |
this.accumulator = accumulator; |
| 86 |
this.finisher = finisher; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onSubscribe(@NonNull Disposable d) { |
| 91 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 92 |
this.upstream = d; |
| 93 |
|
| 94 |
downstream.onSubscribe(this); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void onNext(T t) { |
| 100 |
if (done) { |
| 101 |
return; |
| 102 |
} |
| 103 |
try { |
| 104 |
accumulator.accept(container, t); |
| 105 |
} catch (Throwable ex) { |
| 106 |
Exceptions.throwIfFatal(ex); |
| 107 |
upstream.dispose(); |
| 108 |
onError(ex); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onError(Throwable t) { |
| 114 |
if (done) { |
| 115 |
RxJavaPlugins.onError(t); |
| 116 |
} else { |
| 117 |
done = true; |
| 118 |
upstream = DisposableHelper.DISPOSED; |
| 119 |
this.container = null; |
| 120 |
downstream.onError(t); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void onComplete() { |
| 126 |
if (done) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
done = true; |
| 131 |
upstream = DisposableHelper.DISPOSED; |
| 132 |
A container = this.container; |
| 133 |
this.container = null; |
| 134 |
R result; |
| 135 |
try { |
| 136 |
result = Objects.requireNonNull(finisher.apply(container), "The finisher returned a null value"); |
| 137 |
} catch (Throwable ex) { |
| 138 |
Exceptions.throwIfFatal(ex); |
| 139 |
downstream.onError(ex); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
complete(result); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void dispose() { |
| 148 |
super.dispose(); |
| 149 |
upstream.dispose(); |
| 150 |
} |
| 151 |
} |
| 152 |
} |