All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.internal.operators.OperatorScan Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
/**
 * Copyright 2014 Netflix, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package rx.internal.operators;

import rx.Observable.Operator;
import rx.Subscriber;
import rx.exceptions.OnErrorThrowable;
import rx.functions.Func2;

/**
 * Returns an Observable that applies a function to the first item emitted by a source Observable, then feeds
 * the result of that function along with the second item emitted by an Observable into the same function, and
 * so on until all items have been emitted by the source Observable, emitting the result of each of these
 * iterations.
 * 

* *

* This sort of function is sometimes called an accumulator. *

* Note that when you pass a seed to scan() the resulting Observable will emit that seed as its * first emitted item. */ public final class OperatorScan implements Operator { private final R initialValue; private final Func2 accumulator; // sentinel if we don't receive an initial value private static final Object NO_INITIAL_VALUE = new Object(); /** * Applies an accumulator function over an observable sequence and returns each intermediate result with the * specified source and accumulator. * * @param initialValue * the initial (seed) accumulator value * @param accumulator * an accumulator function to be invoked on each element from the sequence * @see Observable.Scan(TSource, TAccumulate) Method (IObservable(TSource), TAccumulate, Func(TAccumulate, TSource, * TAccumulate)) */ public OperatorScan(R initialValue, Func2 accumulator) { this.initialValue = initialValue; this.accumulator = accumulator; } /** * Applies an accumulator function over an observable sequence and returns each intermediate result with the * specified source and accumulator. * * @param accumulator * an accumulator function to be invoked on each element from the sequence * @see Observable.Scan(TSource) Method (IObservable(TSource), Func(TSource, TSource, TSource)) */ @SuppressWarnings("unchecked") public OperatorScan(final Func2 accumulator) { this((R) NO_INITIAL_VALUE, accumulator); } @Override public Subscriber call(final Subscriber observer) { if (initialValue != NO_INITIAL_VALUE) { observer.onNext(initialValue); } return new Subscriber(observer) { private R value = initialValue; @SuppressWarnings("unchecked") @Override public void onNext(T value) { if (this.value == NO_INITIAL_VALUE) { // if there is NO_INITIAL_VALUE then we know it is type T for both so cast T to R this.value = (R) value; } else { try { this.value = accumulator.call(this.value, value); } catch (Throwable e) { observer.onError(OnErrorThrowable.addValueAsLastCause(e, value)); } } observer.onNext(this.value); } @Override public void onError(Throwable e) { observer.onError(e); } @Override public void onCompleted() { observer.onCompleted(); } }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy