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

io.github.sinri.keel.facade.async.FutureForEach Maven / Gradle / Ivy

Go to download

A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.

The newest version!
package io.github.sinri.keel.facade.async;

import io.vertx.core.Future;

import javax.annotation.Nonnull;
import java.util.Iterator;
import java.util.function.Function;

/**
 * 给定一个可迭代对象,迭代其元素,依次针对每个元素运行异步代码。
 *
 * @param  Type of elements in the source collection
 * @since 1.13
 */
public class FutureForEach {
    private final Function> asyncItemProcessFunction;

    private FutureForEach(@Nonnull Function> itemProcessor) {
        this.asyncItemProcessFunction = itemProcessor;
    }

    static  Future call(@Nonnull Iterable collection, @Nonnull Function> itemProcessor) {
        return new FutureForEach<>(itemProcessor).process(collection);
    }

    private Future process(@Nonnull Iterable collection) {
        Iterator iterator = collection.iterator();
        return KeelAsyncKit.repeatedlyCall(routineResult -> {
            if (iterator.hasNext()) {
                T next = iterator.next();
                return asyncItemProcessFunction.apply(next);
            } else {
                routineResult.stop();
                return Future.succeededFuture();
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy