
rx.util.async.operators.OperatorFromFunctionals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-async-util Show documentation
Show all versions of rxjava-async-util Show documentation
rxjava-async-util developed by Netflix
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.operators;
import java.util.concurrent.Callable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Actions;
import rx.functions.Func0;
/**
* Operators that invoke a function or action if
* an observer subscribes.
* Asynchrony can be achieved by using subscribeOn or observeOn.
*/
public final class OperatorFromFunctionals {
/** Utility class. */
private OperatorFromFunctionals() { throw new IllegalStateException("No instances!"); }
/** Subscriber function that invokes an action and returns the given result. */
public static OnSubscribe fromAction(Action0 action, R result) {
return new InvokeAsync(Actions.toFunc(action, result));
}
/**
* Subscriber function that invokes the callable and returns its value or
* propagates its checked exception.
*/
public static OnSubscribe fromCallable(Callable extends R> callable) {
return new InvokeAsync(callable);
}
/** Subscriber function that invokes a runnable and returns the given result. */
public static OnSubscribe fromRunnable(final Runnable run, final R result) {
return new InvokeAsync(new Func0() {
@Override
public R call() {
run.run();
return result;
}
});
}
/**
* Invokes a java.util.concurrent.Callable when an observer subscribes.
* @param the return type
*/
static final class InvokeAsync implements OnSubscribe {
final Callable extends R> callable;
public InvokeAsync(Callable extends R> callable) {
if (callable == null) {
throw new NullPointerException("function");
}
this.callable = callable;
}
@Override
public void call(Subscriber super R> t1) {
try {
t1.onNext(callable.call());
} catch (Throwable t) {
t1.onError(t);
return;
}
t1.onCompleted();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy