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

hu.akarnokd.rxjava2.async.AsyncObservable 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.async;

import java.util.concurrent.*;

import hu.akarnokd.rxjava2.functions.*;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
import io.reactivex.internal.disposables.SequentialDisposable;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.observers.LambdaObserver;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.*;

/**
 * Utility methods to convert functions and actions into asynchronous operations through the Observable/Observer
 * pattern.
 */
public final class AsyncObservable {

    /** Utility class. */
    private AsyncObservable() {
        throw new IllegalStateException("No instances!");
    }


    /**
     * Invokes the specified function asynchronously and returns an Observable that emits the result.
     * 

* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting * Observable. Multiple subscriptions to this Observable observe the same return value. *

* *

*
Scheduler:
*
{@code start} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result value type * @param func function to run asynchronously * @return an Observable that emits the function's result value, or notifies observers of an exception * @see RxJava Wiki: start() * @see MSDN: Observable.Start */ public static Observable start(Callable func) { return start(func, Schedulers.computation()); } /** * Invokes the specified function asynchronously on the specified Scheduler and returns an Observable that * emits the result. *

* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting * Observable. Multiple subscriptions to this Observable observe the same return value. *

* *

*
Scheduler:
*
You specify the {@link Scheduler} {@code start} works on.
*
*

* @param the result value type * @param func function to run asynchronously * @param scheduler Scheduler to run the function on * @return an Observable that emits the function's result value, or notifies observers of an exception * @see RxJava Wiki: start() * @see MSDN: Observable.Start */ public static Observable start(Callable func, Scheduler scheduler) { return Observable.fromCallable(func).subscribeOn(scheduler).subscribeWith(AsyncSubject.create()); } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(Action action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result value type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(Callable func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param first parameter type of the action * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(Consumer action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param first parameter type of the action * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(Function func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param action the action to convert * @return a function that returns a Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(BiConsumer action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the result type * @param func the function to convert * @return a function that returns a Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(BiFunction func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(Consumer3 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(Function3 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(Consumer4 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(Function4 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(Consumer5 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(Function5 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(Consumer6 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(Function6 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync(Consumer7 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync(Function7 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync(Consumer8 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync(Function8 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync(Consumer9 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync(Function9 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsyncArray} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param action the action to convert * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(Consumer action) { return toAsyncArray(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

*
Scheduler:
*
{@code toAsyncArray} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result type * @param func the function to convert * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(Function func) { return toAsyncArray(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits an Object * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(final Action action, final Scheduler scheduler) { return new Supplier>() { @Override public Observable call() { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.run(); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the result value type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(final Callable func, final Scheduler scheduler) { return new Supplier>() { @Override public Observable call() { return Observable.fromCallable(func).subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param first parameter type of the action * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(final Consumer action, final Scheduler scheduler) { return new PlainFunction>() { @Override public Observable apply(final T1 t1) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param first parameter type of the action * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(final Function func, final Scheduler scheduler) { return new PlainFunction>() { @Override public Observable apply(final T1 t1) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through a Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns a Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(final BiConsumer action, final Scheduler scheduler) { return new PlainBiFunction>() { @Override public Observable apply(final T1 t1, final T2 t2) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns a Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(final BiFunction func, final Scheduler scheduler) { return new PlainBiFunction>() { @Override public Observable apply(final T1 t1, final T2 t2) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(final Consumer3 action, final Scheduler scheduler) { return new PlainFunction3>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(final Function3 func, final Scheduler scheduler) { return new PlainFunction3>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(final Consumer4 action, final Scheduler scheduler) { return new PlainFunction4>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(final Function4 func, final Scheduler scheduler) { return new PlainFunction4>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(final Consumer5 action, final Scheduler scheduler) { return new PlainFunction5>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(final Function5 func, final Scheduler scheduler) { return new PlainFunction5>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(final Consumer6 action, final Scheduler scheduler) { return new PlainFunction6>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(final Function6 func, final Scheduler scheduler) { return new PlainFunction6>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync( final Consumer7 action, final Scheduler scheduler) { return new PlainFunction7>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync( final Function7 func, final Scheduler scheduler) { return new PlainFunction7>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync( final Consumer8 action, final Scheduler scheduler) { return new PlainFunction8>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7, t8); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync( final Function8 func, final Scheduler scheduler) { return new PlainFunction8>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7, t8); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync( final Consumer9 action, final Scheduler scheduler) { return new PlainFunction9>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8, final T9 t9) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7, t8, t9); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync( final Function9 func, final Scheduler scheduler) { return new PlainFunction9>() { @Override public Observable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8, final T9 t9) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7, t8, t9); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(final Consumer action, final Scheduler scheduler) { return new PlainFunction>() { @Override public Observable apply(final Object[] t) { return Observable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(final Function func, final Scheduler scheduler) { return new PlainFunction>() { @Override public Observable apply(final Object[] t) { return Observable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t); } }) .subscribeOn(scheduler); } }; } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable. *

* Important note subscribing to the resulting Observable blocks until the future completes. *

* * *

*
Scheduler:
*
{@code startFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the result type * @param functionAsync the asynchronous function to run * @return an Observable that surfaces the result of the future * @see #startFuture(Callable, Scheduler) * @see RxJava Wiki: startFuture() */ public static Observable startFuture(final Callable> functionAsync) { return RxJavaPlugins.onAssembly(new ObservableFromCallableNull(new Callable() { @Override public T call() throws Exception { return functionAsync.call().get(); } })); } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable and waits on * the specified Scheduler. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code startFuture} works on.
*
* @param the result type * @param functionAsync the asynchronous function to run * @param scheduler the Scheduler where the completion of the Future is awaited * @return an Observable that surfaces the result of the future * @see RxJava Wiki: startFuture() */ public static Observable startFuture(Callable> functionAsync, Scheduler scheduler) { return startFuture(functionAsync).subscribeOn(scheduler); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new subscriber * subscribes. *

* Important note subscribing to the resulting Observable blocks until the future completes. *

* * *

*
Scheduler:
*
{@code deferFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the result type * @param publisherFactoryAsync the asynchronous function to start for each observer * @return the Observable emitting items produced by the asynchronous observer produced by the factory * @see #deferFuture(Callable, Scheduler) * @see RxJava Wiki: deferFuture() */ public static Observable deferFuture(Callable>> publisherFactoryAsync) { return deferFuture(publisherFactoryAsync, Schedulers.computation()); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new subscriber * subscribes. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code deferFuture} works on.
*
* @param the result type * @param publisherFactoryAsync the asynchronous function to start for each observer * @param scheduler the Scheduler where the completion of the Future is awaited * @return the Observable emitting items produced by the asynchronous observer produced by the factory * @see RxJava Wiki: deferFuture() */ public static Observable deferFuture( final Callable>> publisherFactoryAsync, Scheduler scheduler) { return Observable.defer(new Callable>() { @Override public ObservableSource call() throws Exception { return publisherFactoryAsync.call().get(); } }).subscribeOn(scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source ObservableSource * @param onNext the action to call with each emitted element * @return the Future representing the entire for-each operation * @see #forEachFuture(ObservableSource, Consumer, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext) { return forEachFuture(source, onNext, Functions.emptyConsumer(), Functions.EMPTY_ACTION, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source ObservableSource * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @return the Future representing the entire for-each operation * @see #forEachFuture(ObservableSource, Consumer, Consumer, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext, final Consumer onError) { return forEachFuture(source, onNext, onError, Functions.EMPTY_ACTION, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source ObservableSource * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param onComplete the action to call when the source completes * @return the Future representing the entire for-each operation * @see #forEachFuture(ObservableSource, Consumer, Consumer, Action, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext, Consumer onError, Action onComplete) { return forEachFuture(source, onNext, onError, onComplete, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given scheduler. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext, Scheduler scheduler) { return forEachFuture(source, onNext, Functions.emptyConsumer(), Functions.EMPTY_ACTION, scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given Scheduler. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source ObservableSource * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext, Consumer onError, Scheduler scheduler) { return forEachFuture(source, onNext, onError, Functions.EMPTY_ACTION, scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given Scheduler. *

* * *

*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param onComplete the action to call when the source completes * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( ObservableSource source, Consumer onNext, final Consumer onError, final Action onComplete, Scheduler scheduler) { SequentialDisposable d = new SequentialDisposable(); final FutureCompletable f = new FutureCompletable(d); LambdaObserver ls = new LambdaObserver(onNext, new Consumer() { @Override public void accept(Throwable e) throws Exception { try { onError.accept(e); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); f.completeExceptionally(new CompositeException(e, ex)); return; } f.completeExceptionally(e); } }, new Action() { @Override public void run() throws Exception { try { onComplete.run(); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); f.completeExceptionally(ex); return; } f.complete(null); } }, new Consumer() { @Override public void accept(Disposable s) throws Exception { } }); d.lazySet(ls); // FIXME next RxJava 2.x release will allow using ObservableSubscribeOn with ObservableSource Observable.wrap(source).subscribeOn(scheduler).subscribe(ls); return f; } /** * Runs the provided action on the given scheduler and allows propagation of multiple events to the * observers of the returned DisposableObservable. The action is immediately executed and unobserved values * will be lost. * *
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the output value type * @param scheduler the Scheduler where the action is executed * @param action the action to execute, receives a Subscriber where the events can be pumped and a * Disposable which lets it check for cancellation condition * @return an DisposableObservable that provides a Disposable interface to cancel the action * @see RxJava Wiki: runAsync() */ public static DisposableObservable runAsync(Scheduler scheduler, final BiConsumer, ? super Disposable> action) { return runAsync(scheduler, PublishSubject.create(), action); } /** * Runs the provided action on the given scheduler and allows propagation of multiple events to the * observers of the returned DisposableObservable. The action is immediately executed and unobserved values * might be lost, depending on the Subject type used. * *
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the output value of the action * @param scheduler the Scheduler where the action is executed * @param subject the subject to use to distribute values emitted by the action * @param action the action to execute, receives a Subscriber where the events can be pumped and a * Disposable which lets it check for cancellation condition * @return an DisposableObservable that provides a Disposable interface to cancel the action * @see RxJava Wiki: runAsync() */ public static DisposableObservable runAsync(Scheduler scheduler, final Subject subject, final BiConsumer, ? super Disposable> action) { final SequentialDisposable d = new SequentialDisposable(); d.replace(scheduler.scheduleDirect(new Runnable() { @Override public void run() { try { action.accept(subject, d); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); subject.onError(ex); } } })); return new DisposableObservable() { @Override protected void subscribeActual(Observer s) { subject.subscribe(s); } @Override public boolean isDisposed() { return d.isDisposed(); } @Override public void dispose() { d.dispose(); } }; } }