|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.operators; |
| 17 | + |
| 18 | +import rx.Observable.Operator; |
| 19 | +import rx.Subscriber; |
| 20 | +import rx.util.functions.Func2; |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns an Observable that applies a function to the first item emitted by a source Observable, |
| 24 | + * then feeds the result of that function along with the second item emitted by an Observable into |
| 25 | + * the same function, and so on until all items have been emitted by the source Observable, |
| 26 | + * emitting the result of each of these iterations. |
| 27 | + * <p> |
| 28 | + * <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/scan.png"> |
| 29 | + * <p> |
| 30 | + * This sort of function is sometimes called an accumulator. |
| 31 | + * <p> |
| 32 | + * Note that when you pass a seed to <code>scan()</code> the resulting Observable will emit that |
| 33 | + * seed as its first emitted item. |
| 34 | + */ |
| 35 | +public final class OperatorScan<R, T> implements Operator<R, T> { |
| 36 | + |
| 37 | + private final R initialValue; |
| 38 | + private final Func2<R, ? super T, R> accumulator; |
| 39 | + // sentinel if we don't receive an initial value |
| 40 | + private static final Object NO_INITIAL_VALUE = new Object(); |
| 41 | + |
| 42 | + /** |
| 43 | + * Applies an accumulator function over an observable sequence and returns each intermediate |
| 44 | + * result with the specified source and accumulator. |
| 45 | + * |
| 46 | + * @param sequence |
| 47 | + * An observable sequence of elements to project. |
| 48 | + * @param initialValue |
| 49 | + * The initial (seed) accumulator value. |
| 50 | + * @param accumulator |
| 51 | + * An accumulator function to be invoked on each element from the sequence. |
| 52 | + * |
| 53 | + * @return An observable sequence whose elements are the result of accumulating the output from |
| 54 | + * the list of Observables. |
| 55 | + * @see <a href="http://msdn.microsoft.com/en-us/library/hh212007%28v=vs.103%29.aspx">Observable.Scan(TSource, TAccumulate) Method (IObservable(TSource), TAccumulate, Func(TAccumulate, TSource, |
| 56 | + * TAccumulate))</a> |
| 57 | + */ |
| 58 | + public OperatorScan(R initialValue, Func2<R, ? super T, R> accumulator) { |
| 59 | + this.initialValue = initialValue; |
| 60 | + this.accumulator = accumulator; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Applies an accumulator function over an observable sequence and returns each intermediate |
| 65 | + * result with the specified source and accumulator. |
| 66 | + * |
| 67 | + * @param sequence |
| 68 | + * An observable sequence of elements to project. |
| 69 | + * @param accumulator |
| 70 | + * An accumulator function to be invoked on each element from the sequence. |
| 71 | + * |
| 72 | + * @return An observable sequence whose elements are the result of accumulating the output from |
| 73 | + * the list of Observables. |
| 74 | + * @see <a href="http://msdn.microsoft.com/en-us/library/hh211665(v=vs.103).aspx">Observable.Scan(TSource) Method (IObservable(TSource), Func(TSource, TSource, TSource))</a> |
| 75 | + */ |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + public OperatorScan(final Func2<R, ? super T, R> accumulator) { |
| 78 | + this((R) NO_INITIAL_VALUE, accumulator); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Subscriber<? super T> call(final Subscriber<? super R> observer) { |
| 83 | + if (initialValue != NO_INITIAL_VALUE) { |
| 84 | + observer.onNext(initialValue); |
| 85 | + } |
| 86 | + return new Subscriber<T>(observer) { |
| 87 | + private R value = initialValue; |
| 88 | + |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + @Override |
| 91 | + public void onNext(T value) { |
| 92 | + if (this.value == NO_INITIAL_VALUE) { |
| 93 | + // if there is NO_INITIAL_VALUE then we know it is type T for both so cast T to R |
| 94 | + this.value = (R) value; |
| 95 | + } else { |
| 96 | + try { |
| 97 | + this.value = accumulator.call(this.value, value); |
| 98 | + } catch (Throwable e) { |
| 99 | + observer.onError(e); |
| 100 | + observer.unsubscribe(); |
| 101 | + } |
| 102 | + } |
| 103 | + observer.onNext(this.value); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onError(Throwable e) { |
| 108 | + observer.onError(e); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onCompleted() { |
| 113 | + observer.onCompleted(); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | +} |
0 commit comments