/** * Converts a {@link Operation} into a {@link Completable}. * <p> * The returned Completable emits nothing if it succeeds, and emits the error (i.e. via {@code onError()}) if it fails. */ public static Completable complete(Operation operation) { return Completable.create(subscriber -> operation.onError(subscriber::onError).then(subscriber::onComplete) ); }
default <T> Promise<T> flatMap(Promise<T> promise) { return promise().flatMap(n -> promise); }
default Operation next(Operation operation) { return new DefaultOperation(flatMap(operation::promise)); }
@Override public HttpClientSpec responseIntercept(Operation operation) { responseInterceptor = responseInterceptor.append(response -> operation.then()); return this; }
/** * Executes the given action with the promise value, on a blocking thread. * <p> * Similar to {@link #blockingMap(Function)}, but does not provide a new value. * This can be used to do something with the value, without terminating the promise. * * @param action the action to to perform with the value, on a blocking thread * @return a promise for the same value given to the action */ default Promise<T> blockingOp(Action<? super T> action) { return flatMap(t -> Blocking.op(action.curry(t)).map(() -> t)); }
/** * Executes the given block as an operation, on a blocking thread. * * @param operation a block of code to be executed, on a blocking thread * @return an operation * @since 1.4 */ default Operation blockingNext(Block operation) { return next(Blocking.op(operation)); }
public static void exec(Block block) { op(block).then(); }
return ParallelBatch.of(resultPromises) .forEach((i, result) -> results.put(result.right, result.left)) .map(() -> new HealthCheckResults(ImmutableSortedMap.copyOf(results))); });
/** * Pauses this execution for the given duration. * <p> * Unlike {@link Thread#sleep(long)}, this method does not block the thread. * The thread will be relinquished for use by other executions. * <p> * The given block will be invoked after the duration has passed. * The duration must be non-negative. * * @param duration the duration this execution should sleep for * @param onWake the code to resume with upon awaking * @since 1.5 */ static void sleep(Duration duration, Block onWake) { sleep(duration).then(onWake); }
default <T> Promise<T> flatMap(Factory<? extends Promise<T>> factory) { return promise().flatMap(n -> factory.create()); }
/** * Converts this promise to an operation, which is the return of {@code function}. * * @param function a function that returns an operation for the promised value * @return effectively the return of {@code function} * @since 1.6 */ default Operation flatOp(Function<? super T, ? extends Operation> function) { return operation(t -> function.apply(t).then()); }
default <T> Promise<T> map(Factory<? extends T> factory) { return promise().map(n -> factory.create()); }
@Override public Promise<List<T>> yield() { List<Promise<T>> promises = Lists.newArrayList(this.promises); if (promises.isEmpty()) { return Promise.value(Collections.emptyList()); } List<T> results = Types.cast(promises); return Promise.async(d -> forEach(results::set).onError(d::error).then(() -> d.success(results))); }
/** * Starts the execution, and executes the given operation. * * @param operation the operation to execute * @since 1.4 */ @NonBlocking default void start(Operation operation) { start(e -> operation.then()); }
/** * Create an operation that delegates to another operation. * * @param factory a factory for the operation * @return an operation * @since 1.5 */ static Operation flatten(Factory<Operation> factory) { return new DefaultOperation(Promise.flatten(() -> factory.create().promise())); }