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

rx.joins.operators.OperatorJoinPatterns Maven / Gradle / Ivy

The 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.joins.operators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Observer;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.joins.ActivePlan0;
import rx.joins.JoinObserver;
import rx.joins.Pattern1;
import rx.joins.Pattern2;
import rx.joins.Plan0;
import rx.subscriptions.CompositeSubscription;

/**
 * Join patterns: And, Then, When.
 */
public final class OperatorJoinPatterns {
	public OperatorJoinPatterns() { throw new IllegalStateException("No instances!"); }
    /**
     * Creates a pattern that matches when both observable sequences have an available element.
     */
    public static  Pattern2 and(/* this */Observable left, Observable right) {
        if (left == null) {
            throw new NullPointerException("left");
        }
        if (right == null) {
            throw new NullPointerException("right");
        }
        return new Pattern2(left, right);
    }

    /**
     * Matches when the observable sequence has an available element and projects the element by invoking the selector function.
     */
    public static  Plan0 then(/* this */Observable source, Func1 selector) {
        if (source == null) {
            throw new NullPointerException("source");
        }
        if (selector == null) {
            throw new NullPointerException("selector");
        }
        return new Pattern1(source).then(selector);
    }

    /**
     * Joins together the results from several patterns.
     */
    public static  OnSubscribe when(Plan0... plans) {
        if (plans == null) {
            throw new NullPointerException("plans");
        }
        return when(Arrays.asList(plans));
    }

    /**
     * Joins together the results from several patterns.
     */
    public static  OnSubscribe when(final Iterable> plans) {
        if (plans == null) {
            throw new NullPointerException("plans");
        }
        return new OnSubscribe() {
            @Override
            public void call(final Subscriber t1) {
                final Map externalSubscriptions = new HashMap();
                final Object gate = new Object();
                final List activePlans = new ArrayList();

                final Observer out = new Observer() {
                    @Override
                    public void onNext(R args) {
                        t1.onNext(args);
                    }

                    @Override
                    public void onError(Throwable e) {
                        for (JoinObserver po : externalSubscriptions.values()) {
                            po.unsubscribe();
                        }
                        t1.onError(e);
                    }

                    @Override
                    public void onCompleted() {
                        t1.onCompleted();
                    }
                };

                try {
                    for (Plan0 plan : plans) {
                        activePlans.add(plan.activate(externalSubscriptions, out, new Action1() {
                            @Override
                            public void call(ActivePlan0 activePlan) {
                                activePlans.remove(activePlan);
                                if (activePlans.isEmpty()) {
                                    out.onCompleted();
                                }
                            }
                        }));
                    }
                } catch (Throwable t) {
                    Observable. error(t).unsafeSubscribe(t1);
                    return;
                }
                CompositeSubscription group = new CompositeSubscription();
                t1.add(group);
                for (JoinObserver jo : externalSubscriptions.values()) {
                    jo.subscribe(gate);
                    group.add(jo);
                }
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy