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

rx.operators.OperatorAmb 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.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;

/**
 * Propagates the observable sequence that reacts first.
 */
public final class OperatorAmb implements OnSubscribe{

    public static  OnSubscribe amb(Observable o1, Observable o2) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4, Observable o5) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        sources.add(o5);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4, Observable o5, Observable o6) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        sources.add(o5);
        sources.add(o6);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4, Observable o5, Observable o6, Observable o7) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        sources.add(o5);
        sources.add(o6);
        sources.add(o7);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4, Observable o5, Observable o6, Observable o7, Observable o8) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        sources.add(o5);
        sources.add(o6);
        sources.add(o7);
        sources.add(o8);
        return amb(sources);
    }

    public static  OnSubscribe amb(Observable o1, Observable o2, Observable o3, Observable o4, Observable o5, Observable o6, Observable o7, Observable o8, Observable o9) {
        List> sources = new ArrayList>();
        sources.add(o1);
        sources.add(o2);
        sources.add(o3);
        sources.add(o4);
        sources.add(o5);
        sources.add(o6);
        sources.add(o7);
        sources.add(o8);
        sources.add(o9);
        return amb(sources);
    }

    public static  OnSubscribe amb(final Iterable> sources) {
        return new OperatorAmb(sources);
    }

    private static final class AmbSubscriber extends Subscriber {

        private static final int NONE = -1;

        private final Subscriber subscriber;
        private final int index;
        private final AtomicInteger choice;

        private AmbSubscriber(Subscriber subscriber, int index, AtomicInteger choice) {
            this.subscriber = subscriber;
            this.choice = choice;
            this.index = index;
        }

        @Override
        public void onNext(T args) {
            if (!isSelected()) {
                unsubscribe();
                return;
            }
            subscriber.onNext(args);
        }

        @Override
        public void onCompleted() {
            if (!isSelected()) {
                unsubscribe();
                return;
            }
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            if (!isSelected()) {
                unsubscribe();
                return;
            }
            subscriber.onError(e);
        }

        private boolean isSelected() {
            int ch = choice.get();
            if (ch == NONE) {
                return choice.compareAndSet(NONE, index);
            }
            return ch == index;
        }
    }

    private final Iterable> sources;

    private OperatorAmb(Iterable> sources) {
        this.sources = sources;
    }

    @Override
    public void call(Subscriber subscriber) {
        AtomicInteger choice = new AtomicInteger(AmbSubscriber.NONE);
        int index = 0;
        for (Observable source : sources) {
            if (subscriber.isUnsubscribed()) {
                break;
            }
            if (choice.get() != AmbSubscriber.NONE) {
                // Already choose someone, the rest Observables can be skipped.
                break;
            }
            AmbSubscriber ambSubscriber = new AmbSubscriber(subscriber, index, choice);
            subscriber.add(ambSubscriber);
            source.subscribe(ambSubscriber);
            index++;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy