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

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

Go to download

RxJava 3.x extra sources, operators and components and ports of many 1.x companion libraries.

There is a newer version: 3.1.1
Show newest version
/*
 * Copyright 2016-2019 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.rxjava3.joins;

import java.util.*;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.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