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

hu.akarnokd.rxjava2.joins.JoinPatterns Maven / Gradle / Ivy

There is a newer version: 0.20.10
Show newest version
/*
 * Copyright 2016-2017 David Karnok
 *
 * 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 hu.akarnokd.rxjava2.joins;

import java.util.*;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.*;
import io.reactivex.functions.*;
import io.reactivex.observers.SafeObserver;

/**
 * Join patterns  And, Then and When.
 */
final class JoinPatterns {

    private JoinPatterns() { throw new IllegalStateException("No instances!"); }
    /**
     * Creates a pattern that matches when both observable sequences have an available element.
     * @param left the left source
     * @param right the right source
     * @param  the left value type
     * @param  the right value type
     * @return the pattern with two sources 'and'-ed
     */
    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.
     * @param source the source Observable
     * @param selector the selector to map the source values into result values
     * @param  the source value type
     * @param  the result value type
     * @return the new Plan0 representing the mapping
     */
    public static  Plan then(/* this */Observable source, Function 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.
     * @param plans the array of plans with the common result type
     * @param  the result type
     * @return the Observable coining the plans
     */
    public static  Observable when(Plan... plans) {
        if (plans == null) {
            throw new NullPointerException("plans");
        }
        return when(Arrays.asList(plans));
    }

    /**
     * Joins together the results from several patterns.
     * @param plans the iterable sequence of plans
     * @param  the common result type
     * @return the Observable joining the plans
     */
    public static  Observable when(final Iterable> plans) {
        if (plans == null) {
            throw new NullPointerException("plans");
        }
        return new Observable() {
            @Override
            protected void subscribeActual(final Observer t1) {
                final Map externalSubscriptions = new HashMap();
                final Object gate = new Object();
                final List activePlans = new ArrayList();

                final Observer out = new SafeObserver(new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(R args) {
                        t1.onNext(args);
                    }

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

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

                out.onSubscribe(Disposables.empty());

                try {
                    for (Plan plan : plans) {
                        activePlans.add(plan.activate(externalSubscriptions, out, new Consumer() {
                            @Override
                            public void accept(ActivePlan0 activePlan) {
                                activePlans.remove(activePlan);
                                if (activePlans.isEmpty()) {
                                    out.onComplete();
                                }
                            }
                        }));
                    }
                } catch (Throwable t) {
                    Observable. error(t).subscribe(t1);
                    return;
                }
                CompositeDisposable group = new CompositeDisposable();
                t1.onSubscribe(group);
                for (JoinObserver jo : externalSubscriptions.values()) {
                    jo.subscribe(gate);
                    group.add(jo);
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy