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

rx.operators.OperationTakeUntil 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.operators;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;

/**
 * Returns an Observable that emits the items from the source Observable until another Observable
 * emits an item.
 * 

* */ public class OperationTakeUntil { /** * Returns the values from the source observable sequence until the other observable sequence produces a value. * * @param source * the source sequence to propagate elements for. * @param other * the observable sequence that terminates propagation of elements of the source sequence. * @param * the type of source. * @param * the other type. * @return An observable sequence containing the elements of the source sequence up to the point the other sequence interrupted further propagation. */ public static Observable takeUntil(final Observable source, final Observable other) { Observable> s = Observable.create(new SourceObservable(source)); Observable> o = Observable.create(new OtherObservable(other)); Observable> result = Observable.merge(s, o); return result.takeWhile(new Func1, Boolean>() { @Override public Boolean call(Notification notification) { return !notification.halt; } }).map(new Func1, T>() { @Override public T call(Notification notification) { return notification.value; } }); } private static class Notification { private final boolean halt; private final T value; public static Notification value(T value) { return new Notification(false, value); } public static Notification halt() { return new Notification(true, null); } private Notification(boolean halt, T value) { this.halt = halt; this.value = value; } } private static class SourceObservable implements OnSubscribeFunc> { private final Observable sequence; private SourceObservable(Observable sequence) { this.sequence = sequence; } @Override public Subscription onSubscribe(final Observer> notificationObserver) { return sequence.subscribe(new Observer() { @Override public void onCompleted() { notificationObserver.onNext(Notification. halt()); } @Override public void onError(Throwable e) { notificationObserver.onError(e); } @Override public void onNext(T args) { notificationObserver.onNext(Notification.value(args)); } }); } } private static class OtherObservable implements OnSubscribeFunc> { private final Observable sequence; private OtherObservable(Observable sequence) { this.sequence = sequence; } @Override public Subscription onSubscribe(final Observer> notificationObserver) { return sequence.subscribe(new Observer() { @Override public void onCompleted() { notificationObserver.onNext(Notification. halt()); } @Override public void onError(Throwable e) { notificationObserver.onError(e); } @Override public void onNext(E args) { notificationObserver.onNext(Notification. halt()); } }); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy