hu.akarnokd.rxjava2.expr.StatementObservable Maven / Gradle / Ivy
Show all versions of rxjava2-extensions Show documentation
/*
* 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.expr;
import java.util.Map;
import java.util.concurrent.Callable;
import hu.akarnokd.rxjava2.util.AlwaysTrueBooleanSupplier;
import io.reactivex.*;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.internal.functions.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Imperative statements expressed as Observable operators.
*/
public final class StatementObservable {
/** Factory class. */
private StatementObservable() { throw new IllegalStateException("No instances!"); }
/**
* Return a particular one of several possible Observables based on a case
* selector.
*
*
*
* @param
* the case key type
* @param
* the result value type
* @param caseSelector
* the function that produces a case key when an
* Observer subscribes
* @param mapOfCases
* a map that maps a case key to an Observable
* @return a particular Observable chosen by key from the map of
* Observables, or an empty Observable if no Observable matches the
* key
*/
public static Observable switchCase(Callable extends K> caseSelector,
Map super K, ? extends ObservableSource extends R>> mapOfCases) {
return switchCase(caseSelector, mapOfCases, Observable. empty());
}
/**
* Return a particular one of several possible Observables based on a case
* selector and run it on the designated scheduler.
*
*
*
* @param
* the case key type
* @param
* the result value type
* @param caseSelector
* the function that produces a case key when an
* Observer subscribes
* @param mapOfCases
* a map that maps a case key to an Observable
* @param scheduler
* the scheduler where the empty observable is observed
* @return a particular Observable chosen by key from the map of
* Observables, or an empty Observable if no Observable matches the
* key, but one that runs on the designated scheduler in either case
*/
public static Observable switchCase(Callable extends K> caseSelector,
Map super K, ? extends ObservableSource extends R>> mapOfCases, Scheduler scheduler) {
return switchCase(caseSelector, mapOfCases, Observable. empty().subscribeOn(scheduler));
}
/**
* Return a particular one of several possible Observables based on a case
* selector, or a default Observable if the case selector does not map to
* a particular one.
*
*
*
* @param
* the case key type
* @param
* the result value type
* @param caseSelector
* the function that produces a case key when an
* Observer subscribes
* @param mapOfCases
* a map that maps a case key to an Observable
* @param defaultCase
* the default Observable if the {@code mapOfCases} doesn't contain a value for the key returned by the {@code caseSelector}
* @return a particular Observable chosen by key from the map of
* Observables, or the default case if no Observable matches the key
*/
public static Observable switchCase(Callable extends K> caseSelector,
Map super K, ? extends ObservableSource extends R>> mapOfCases,
ObservableSource extends R> defaultCase) {
ObjectHelper.requireNonNull(caseSelector, "caseSelector is null");
ObjectHelper.requireNonNull(mapOfCases, "mapOfCases is null");
ObjectHelper.requireNonNull(defaultCase, "defaultCase is null");
return RxJavaPlugins.onAssembly(new ObservableSwitchCase(caseSelector, mapOfCases, defaultCase));
}
/**
* Return an Observable that re-emits the emissions from the source
* Observable, and then re-subscribes to the source long as a condition is
* true.
*
*
*
* @param the value type
* @param source the source Observable to work with
* @param postCondition
* the post condition to test after the source
* Observable completes
* @return an Observable that replays the emissions from the source
* Observable, and then continues to replay them so long as the post
* condition is true
*/
public static Observable doWhile(ObservableSource extends T> source, BooleanSupplier postCondition) {
ObjectHelper.requireNonNull(source, "source is null");
ObjectHelper.requireNonNull(postCondition, "postCondition is null");
return RxJavaPlugins.onAssembly(new ObservableWhileDoWhile(source, AlwaysTrueBooleanSupplier.INSTANCE, postCondition));
}
/**
* Return an Observable that re-emits the emissions from the source
* Observable as long as the condition is true before the first or subsequent subscribe() calls.
*
*
*
* @param the value type
* @param source the source Observable to work with
* @param preCondition
* the condition to evaluate before subscribing to or
* replaying the source Observable
* @return an Observable that replays the emissions from the source
* Observable so long as preCondition
is true
*/
public static Observable whileDo(ObservableSource extends T> source, BooleanSupplier preCondition) {
ObjectHelper.requireNonNull(source, "source is null");
ObjectHelper.requireNonNull(preCondition, "preCondition is null");
return RxJavaPlugins.onAssembly(new ObservableWhileDoWhile(source, preCondition, preCondition));
}
/**
* Return an Observable that emits the emissions from a specified Observable
* if a condition evaluates to true, otherwise return an empty Observable.
*
*
*
* @param
* the result value type
* @param condition
* the condition that decides whether to emit the emissions
* from the then
Observable
* @param then
* the Observable sequence to emit to if {@code condition} is {@code true}
* @return an Observable that mimics the {@code then} Observable if the {@code condition} function evaluates to true, or an empty
* Observable otherwise
*/
public static Observable ifThen(BooleanSupplier condition, ObservableSource extends R> then) {
return ifThen(condition, then, Observable. empty());
}
/**
* Return an Observable that emits the emissions from a specified Observable
* if a condition evaluates to true, otherwise return an empty Observable
* that runs on a specified Scheduler.
*
*
*
* @param
* the result value type
* @param condition
* the condition that decides whether to emit the emissions
* from the then
Observable
* @param then
* the Observable sequence to emit to if {@code condition} is {@code true}
* @param scheduler
* the Scheduler on which the empty Observable runs if the
* in case the condition returns false
* @return an Observable that mimics the {@code then} Observable if the {@code condition} function evaluates to true, or an empty
* Observable running on the specified Scheduler otherwise
*/
public static Observable ifThen(BooleanSupplier condition, ObservableSource extends R> then, Scheduler scheduler) {
return ifThen(condition, then, Observable. empty().subscribeOn(scheduler));
}
/**
* Return an Observable that emits the emissions from one specified
* Observable if a condition evaluates to true, or from another specified
* Observable otherwise.
*
*
*
* @param
* the result value type
* @param condition
* the condition that decides which Observable to emit the
* emissions from
* @param then
* the Observable sequence to emit to if {@code condition} is {@code true}
* @param orElse
* the Observable sequence to emit to if {@code condition} is {@code false}
* @return an Observable that mimics either the {@code then} or {@code orElse} Observables depending on a condition function
*/
public static Observable ifThen(BooleanSupplier condition, ObservableSource extends R> then,
Observable extends R> orElse) {
ObjectHelper.requireNonNull(condition, "condition is null");
ObjectHelper.requireNonNull(then, "then is null");
ObjectHelper.requireNonNull(orElse, "orElse is null");
return RxJavaPlugins.onAssembly(new ObservableIfThen(condition, then, orElse));
}
}