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

rx.operators.OperationSkipUntil 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 java.util.concurrent.atomic.AtomicBoolean;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.SerialSubscription;

/**
 * Skip elements from the source Observable until the secondary
 * observable fires an element.
 * 
 * @see MSDN: Observable.SkipUntil
 */
public class OperationSkipUntil implements OnSubscribeFunc {
    protected final Observable source;
    protected final Observable other;

    public OperationSkipUntil(Observable source, Observable other) {
        this.source = source;
        this.other = other;
    }

    @Override
    public Subscription onSubscribe(Observer t1) {
        return new ResultManager(t1).init();
    }

    /** Manage the source and other observers. */
    private class ResultManager implements Subscription, Observer {
        final Observer observer;
        final CompositeSubscription cancel;
        final Object guard = new Object();
        final AtomicBoolean running = new AtomicBoolean();

        public ResultManager(Observer observer) {
            this.observer = observer;
            this.cancel = new CompositeSubscription();
        }

        public ResultManager init() {

            SerialSubscription toSource = new SerialSubscription();
            SerialSubscription toOther = new SerialSubscription();

            cancel.add(toSource);
            cancel.add(toOther);

            toSource.setSubscription(source.subscribe(this));
            toOther.setSubscription(other.subscribe(new OtherObserver(toOther)));

            return this;
        }

        @Override
        public void unsubscribe() {
            cancel.unsubscribe();
        }
        
        @Override
        public boolean isUnsubscribed() {
            return cancel.isUnsubscribed();
        }

        @Override
        public void onNext(T args) {
            if (running.get()) {
                observer.onNext(args);
            }
        }

        @Override
        public void onError(Throwable e) {
            synchronized (guard) {
                observer.onError(e);
                unsubscribe();
            }
        }

        @Override
        public void onCompleted() {
            synchronized (guard) {
                observer.onCompleted();
                unsubscribe();
            }
        }

        /** Observe the other stream. */
        private class OtherObserver implements Observer {
            final Subscription self;

            public OtherObserver(Subscription self) {
                this.self = self;
            }

            @Override
            public void onNext(U args) {
                running.set(true);
                self.unsubscribe();
            }

            @Override
            public void onError(Throwable e) {
                ResultManager.this.onError(e);
            }

            @Override
            public void onCompleted() {
                self.unsubscribe();
            }

        }

    }
}