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

rx.util.async.Async 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.util.async;

import rx.*;
import rx.Scheduler.Worker;
import rx.functions.*;
import rx.schedulers.Schedulers;
import rx.subjects.AsyncSubject;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;
import rx.subscriptions.SerialSubscription;
import rx.util.async.operators.*;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Utility methods to convert functions and actions into asynchronous operations through the Observable/Observer
 * pattern.
 */
public final class Async {
    
    private Async() {
        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. *

* * * @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(Func0 func) { return Async.toAsync(func).call(); } /** * 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. *

* *

* @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(Func0 func, Scheduler scheduler) { return Async.toAsync(func, scheduler).call(); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

* @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 Func0> toAsync(Action0 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func0> toAsync(Func0 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func1> toAsync(Action1 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func1> toAsync(Func1 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @param the first parameter type * @param the second 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 Func2> toAsync(Action2 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Func2> toAsync(Func2 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func3> toAsync(Action3 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func3> toAsync(Func3 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func4> toAsync(Action4 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func4> toAsync(Func4 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func5> toAsync(Action5 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func5> toAsync(Func5 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func6> toAsync(Action6 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func6> toAsync(Func6 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func7> toAsync(Action7 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func7> toAsync(Func7 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func8> toAsync(Action8 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func8> toAsync(Func8 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func9> toAsync(Action9 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func9> toAsync(Func9 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 FuncN> toAsync(ActionN action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 FuncN> toAsync(FuncN func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func0> toAsync(final Action0 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func0> toAsync(final Func0 func, final Scheduler scheduler) { return new Func0>() { @Override public Observable call() { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @param the first 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 Func1> toAsync(final Action1 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @param the first 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 Func1> toAsync(final Func1 func, final Scheduler scheduler) { return new Func1>() { @Override public Observable call(final T1 t1) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 an Observable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Func2> toAsync(final Action2 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 an Observable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Func2> toAsync(final Func2 func, final Scheduler scheduler) { return new Func2>() { @Override public Observable call(final T1 t1, final T2 t2) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func3> toAsync(final Action3 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func3> toAsync(final Func3 func, final Scheduler scheduler) { return new Func3>() { @Override public Observable call(final T1 t1, final T2 t2, final T3 t3) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func4> toAsync(final Action4 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func4> toAsync(final Func4 func, final Scheduler scheduler) { return new Func4>() { @Override public Observable call(final T1 t1, final T2 t2, final T3 t3, final T4 t4) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func5> toAsync(final Action5 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func5> toAsync(final Func5 func, final Scheduler scheduler) { return new Func5>() { @Override public Observable call(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4, t5); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func6> toAsync(final Action6 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func6> toAsync(final Func6 func, final Scheduler scheduler) { return new Func6>() { @Override public Observable call(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4, t5, t6); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func7> toAsync(final Action7 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func7> toAsync(final Func7 func, final Scheduler scheduler) { return new Func7>() { @Override public Observable call(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4, t5, t6, t7); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func8> toAsync(final Action8 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func8> toAsync(final Func8 func, final Scheduler scheduler) { return new Func8>() { @Override public Observable call(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 AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4, t5, t6, t7, t8); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 Func9> toAsync(final Action9 action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 Func9> toAsync(final Func9 func, final Scheduler scheduler) { return new Func9>() { @Override public Observable call(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) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(t1, t2, t3, t4, t5, t6, t7, t8, t9); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* * * @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 FuncN> toAsync(final ActionN action, final Scheduler scheduler) { return toAsync(Actions.toFunc(action), scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* * * @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 FuncN> toAsync(final FuncN func, final Scheduler scheduler) { return new FuncN>() { @Override public Observable call(final Object... args) { final AsyncSubject subject = AsyncSubject.create(); final Worker inner = scheduler.createWorker(); inner.schedule(new Action0() { @Override public void call() { R result; try { result = func.call(args); } catch (Throwable t) { subject.onError(t); return; } finally { inner.unsubscribe(); } subject.onNext(result); subject.onCompleted(); } }); return subject; } }; } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

* Alias for toAsync(ActionN) intended for dynamic languages. * * @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: asyncAction() */ public static FuncN> asyncAction(final ActionN action) { return toAsync(action); } /** * Convert a synchronous action call into an asynchronous function call through an Observable. *

* *

* Alias for toAsync(ActionN, Scheduler) intended for dynamic languages. * * @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: asyncAction() */ public static FuncN> asyncAction(final ActionN action, final Scheduler scheduler) { return toAsync(action, scheduler); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

* Alias for toAsync(FuncN) intended for dynamic languages. * * @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: asyncFunc() */ public static FuncN> asyncFunc(final FuncN func) { return toAsync(func); } /** * Convert a synchronous function call into an asynchronous function call through an Observable. *

* *

* Alias for {@code toAsync(FuncN, Scheduler)} intended for dynamic languages. * * @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: asyncFunc() */ public static FuncN> asyncFunc(final FuncN func, final Scheduler scheduler) { return toAsync(func, scheduler); } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable. *

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

* * * @param the result type * @param functionAsync the asynchronous function to run * @return an Observable that surfaces the result of the future * @see #startFuture(rx.functions.Func0, rx.Scheduler) * @see RxJava Wiki: startFuture() */ public static Observable startFuture(Func0> functionAsync) { return OperatorStartFuture.startFuture(functionAsync); } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable and waits on * the specified Scheduler. *

* * * @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(Func0> functionAsync, Scheduler scheduler) { return OperatorStartFuture.startFuture(functionAsync, scheduler); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new observer * subscribes. *

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

* * * @param the result type * @param observableFactoryAsync the asynchronous function to start for each observer * @return the Observable emitting items produced by the asynchronous observer produced by the factory * @see #deferFuture(rx.functions.Func0, rx.Scheduler) * @see RxJava Wiki: deferFuture() */ public static Observable deferFuture(Func0>> observableFactoryAsync) { return OperatorDeferFuture.deferFuture(observableFactoryAsync); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new observer * subscribes. *

* * * @param the result type * @param observableFactoryAsync 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( Func0>> observableFactoryAsync, Scheduler scheduler) { return OperatorDeferFuture.deferFuture(observableFactoryAsync, 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. *

* * * @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @return the Future representing the entire for-each operation * @see #forEachFuture(rx.Observable, rx.functions.Action1, rx.Scheduler) * @see RxJava Wiki: forEachFuture() */ public static FutureTask forEachFuture( Observable source, Action1 onNext) { return OperatorForEachFuture.forEachFuture(source, onNext); } /** * 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. *

* * * @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 * @return the Future representing the entire for-each operation * @see #forEachFuture(rx.Observable, rx.functions.Action1, rx.functions.Action1, rx.Scheduler) * @see RxJava Wiki: forEachFuture() */ public static FutureTask forEachFuture( Observable source, Action1 onNext, Action1 onError) { return OperatorForEachFuture.forEachFuture(source, onNext, onError); } /** * 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. *

* * * @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 onCompleted the action to call when the source completes * @return the Future representing the entire for-each operation * @see #forEachFuture(rx.Observable, rx.functions.Action1, rx.functions.Action1, rx.functions.Action0, rx.Scheduler) * @see RxJava Wiki: forEachFuture() */ public static FutureTask forEachFuture( Observable source, Action1 onNext, Action1 onError, Action0 onCompleted) { return OperatorForEachFuture.forEachFuture(source, onNext, onError, onCompleted); } /** * 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. *

* * * @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 FutureTask forEachFuture( Observable source, Action1 onNext, Scheduler scheduler) { FutureTask task = OperatorForEachFuture.forEachFuture(source, onNext); final Worker inner = scheduler.createWorker(); inner.schedule(Functionals.fromRunnable(task, inner)); return task; } /** * 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. *

* * * @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 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 FutureTask forEachFuture( Observable source, Action1 onNext, Action1 onError, Scheduler scheduler) { FutureTask task = OperatorForEachFuture.forEachFuture(source, onNext, onError); final Worker inner = scheduler.createWorker(); inner.schedule(Functionals.fromRunnable(task, inner)); return task; } /** * 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. *

* * * @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 onCompleted 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 FutureTask forEachFuture( Observable source, Action1 onNext, Action1 onError, Action0 onCompleted, Scheduler scheduler) { FutureTask task = OperatorForEachFuture.forEachFuture(source, onNext, onError, onCompleted); final Worker inner = scheduler.createWorker(); inner.schedule(Functionals.fromRunnable(task, inner)); return task; } /** * Return an Observable that calls the given action and emits the given result when an Observer subscribes. *

* *

* The action is run on the default thread pool for computation. * * @param the return type * @param action the action to invoke on each subscription * @param result the result to emit to observers * @return an Observable that calls the given action and emits the given result when an Observer subscribes * @see RxJava Wiki: fromAction() */ public static Observable fromAction(Action0 action, R result) { return fromAction(action, result, Schedulers.computation()); } /** * Return an Observable that calls the given Callable and emits its result or Exception when an Observer * subscribes. *

* *

* The Callable is called on the default thread pool for computation. * * @param the return type * @param callable the callable to call on each subscription * @return an Observable that calls the given Callable and emits its result or Exception when an Observer * subscribes * @see #start(rx.functions.Func0) * @see RxJava Wiki: fromCallable() */ public static Observable fromCallable(Callable callable) { return fromCallable(callable, Schedulers.computation()); } /** * Return an Observable that calls the given Runnable and emits the given result when an Observer * subscribes. *

* *

* The Runnable is called on the default thread pool for computation. * * @param the return type * @param run the runnable to invoke on each subscription * @param result the result to emit to observers * @return an Observable that calls the given Runnable and emits the given result when an Observer * subscribes * @see RxJava Wiki: fromRunnable() */ public static Observable fromRunnable(final Runnable run, final R result) { return fromRunnable(run, result, Schedulers.computation()); } /** * Return an Observable that calls the given action and emits the given result when an Observer subscribes. *

* * * @param the return type * @param action the action to invoke on each subscription * @param scheduler the Scheduler where the function is called and the result is emitted * @param result the result to emit to observers * @return an Observable that calls the given action and emits the given result when an Observer subscribes * @see RxJava Wiki: fromAction() */ public static Observable fromAction(Action0 action, R result, Scheduler scheduler) { return Observable.create(OperatorFromFunctionals.fromAction(action, result)).subscribeOn(scheduler); } /** * Return an Observable that calls the given Callable and emits its result or Exception when an Observer * subscribes. *

* * * @param the return type * @param callable the callable to call on each subscription * @param scheduler the Scheduler where the function is called and the result is emitted * @return an Observable that calls the given Callable and emits its result or Exception when an Observer * subscribes * @see #start(rx.functions.Func0) * @see RxJava Wiki: fromCallable() */ public static Observable fromCallable(Callable callable, Scheduler scheduler) { return Observable.create(OperatorFromFunctionals.fromCallable(callable)).subscribeOn(scheduler); } /** * Return an Observable that calls the given Runnable and emits the given result when an Observer * subscribes. *

* * * @param the return type * @param run the runnable to invoke on each subscription * @param scheduler the Scheduler where the function is called and the result is emitted * @param result the result to emit to observers * @return an Observable that calls the given Runnable and emits the given result when an Observer * subscribes * @see RxJava Wiki: fromRunnable() */ public static Observable fromRunnable(final Runnable run, final R result, Scheduler scheduler) { return Observable.create(OperatorFromFunctionals.fromRunnable(run, result)).subscribeOn(scheduler); } /** * Runs the provided action on the given scheduler and allows propagation of multiple events to the * observers of the returned StoppableObservable. The action is immediately executed and unobserved values * will be lost. * * @param the output value type * @param scheduler the Scheduler where the action is executed * @param action the action to execute, receives an Observer where the events can be pumped and a * Subscription which lets it check for cancellation condition * @return an Observable that provides a Subscription interface to cancel the action * @see RxJava Wiki: runAsync() */ public static StoppableObservable runAsync(Scheduler scheduler, final Action2, ? super Subscription> 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 StoppableObservable. The action is immediately executed and unobserved values * might be lost, depending on the Subject type used. * * @param the output value of the action * @param the output type of the observable sequence * @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 an Observer where the events can be pumped and a * Subscription which lets it check for cancellation condition * @return an Observable that provides a Subscription interface to cancel the action * @see RxJava Wiki: runAsync() */ public static StoppableObservable runAsync(Scheduler scheduler, final Subject subject, final Action2, ? super Subscription> action) { final SerialSubscription csub = new SerialSubscription(); StoppableObservable co = new StoppableObservable(new Observable.OnSubscribe() { @Override public void call(Subscriber t1) { subject.subscribe(t1); } }, csub); final Worker inner = scheduler.createWorker(); csub.set(inner); inner.schedule(new Action0() { @Override public void call() { if (!csub.isUnsubscribed()) { action.call(subject, csub); } } }); return co; } }