|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 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.internal.operators.flowable; |
| 15 | + |
| 16 | +import org.reactivestreams.*; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.disposables.Disposable; |
| 20 | +import io.reactivex.exceptions.Exceptions; |
| 21 | +import io.reactivex.functions.BiFunction; |
| 22 | +import io.reactivex.internal.functions.ObjectHelper; |
| 23 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 24 | +import io.reactivex.plugins.RxJavaPlugins; |
| 25 | + |
| 26 | +/** |
| 27 | + * Reduce a sequence of values, starting from a seed value and by using |
| 28 | + * an accumulator function and return the last accumulated value. |
| 29 | + * |
| 30 | + * @param <T> the source value type |
| 31 | + * @param <R> the accumulated result type |
| 32 | + */ |
| 33 | +public final class FlowableReduceSeedSingle<T, R> extends Single<R> { |
| 34 | + |
| 35 | + final Publisher<T> source; |
| 36 | + |
| 37 | + final R seed; |
| 38 | + |
| 39 | + final BiFunction<R, ? super T, R> reducer; |
| 40 | + |
| 41 | + public FlowableReduceSeedSingle(Publisher<T> source, R seed, BiFunction<R, ? super T, R> reducer) { |
| 42 | + this.source = source; |
| 43 | + this.seed = seed; |
| 44 | + this.reducer = reducer; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void subscribeActual(SingleObserver<? super R> observer) { |
| 49 | + source.subscribe(new ReduceSeedObserver<T, R>(observer, reducer, seed)); |
| 50 | + } |
| 51 | + |
| 52 | + static final class ReduceSeedObserver<T, R> implements Subscriber<T>, Disposable { |
| 53 | + |
| 54 | + final SingleObserver<? super R> actual; |
| 55 | + |
| 56 | + final BiFunction<R, ? super T, R> reducer; |
| 57 | + |
| 58 | + R value; |
| 59 | + |
| 60 | + Subscription s; |
| 61 | + |
| 62 | + public ReduceSeedObserver(SingleObserver<? super R> actual, BiFunction<R, ? super T, R> reducer, R value) { |
| 63 | + this.actual = actual; |
| 64 | + this.value = value; |
| 65 | + this.reducer = reducer; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onSubscribe(Subscription s) { |
| 70 | + if (SubscriptionHelper.validate(this.s, s)) { |
| 71 | + this.s = s; |
| 72 | + |
| 73 | + actual.onSubscribe(this); |
| 74 | + |
| 75 | + s.request(Long.MAX_VALUE); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onNext(T value) { |
| 81 | + R v = this.value; |
| 82 | + if (v != null) { |
| 83 | + try { |
| 84 | + this.value = ObjectHelper.requireNonNull(reducer.apply(v, value), "The reducer returned a null value"); |
| 85 | + } catch (Throwable ex) { |
| 86 | + Exceptions.throwIfFatal(ex); |
| 87 | + s.cancel(); |
| 88 | + onError(ex); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onError(Throwable e) { |
| 95 | + R v = value; |
| 96 | + value = null; |
| 97 | + if (v != null) { |
| 98 | + s = SubscriptionHelper.CANCELLED; |
| 99 | + actual.onError(e); |
| 100 | + } else { |
| 101 | + RxJavaPlugins.onError(e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onComplete() { |
| 107 | + R v = value; |
| 108 | + value = null; |
| 109 | + if (v != null) { |
| 110 | + s = SubscriptionHelper.CANCELLED; |
| 111 | + actual.onSuccess(v); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void dispose() { |
| 117 | + s.cancel(); |
| 118 | + s = SubscriptionHelper.CANCELLED; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean isDisposed() { |
| 123 | + return s == SubscriptionHelper.CANCELLED; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments