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

io.github.sinri.keel.facade.async.FutureForEachBreakable 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.BiFunction;

/**
 * @since 3.2.3
 */
public class FutureForEachBreakable {

    @Nonnull
    private final Iterable iterable;
    @Nonnull
    private final BiFunction> itemProcessor;
    private FutureForEachBreakable(@Nonnull Iterable iterable, @Nonnull BiFunction> itemProcessor) {
        this.iterable = iterable;
        this.itemProcessor = itemProcessor;
    }

    public static  Future call(@Nonnull Iterable iterable, @Nonnull BiFunction> itemProcessor) {
        return new FutureForEachBreakable<>(iterable, itemProcessor).start();
    }

    private Future start() {
        Iterator iterator = iterable.iterator();
        return KeelAsyncKit.repeatedlyCall(routineResult -> {
            if (iterator.hasNext()) {
                T next = iterator.next();
                return this.itemProcessor.apply(next, routineResult);
            } else {
                routineResult.stop();
                return Future.succeededFuture();
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy