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

vertx.effect.exp.ParallelAny Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package vertx.effect.exp;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import vertx.effect.RetryPolicy;
import vertx.effect.Val;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static java.util.Objects.requireNonNull;

final class ParallelAny extends Any {

    private static final String ATTEMPTS_LOWER_THAN_ONE_ERROR = "attempts < 1";

    ParallelAny(final List> exps) {
        this.exps = requireNonNull(exps);
    }

    final List> exps;


    @Override
    public Val retry(final int attempts) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));

        return new ParallelAny(exps.stream()
                                   .map(it -> it.retry(attempts))
                                   .collect(Collectors.toList()));
    }


    @Override
    public Val retry(final int attempts,
                              final BiFunction> retryPolicy) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));
        if (retryPolicy == null)
            return Cons.failure(new NullPointerException("retryPolicy is null"));
        return new ParallelAny(exps.stream()
                                   .map(it -> it.retry(attempts,
                                              retryPolicy
                                             )
                              )
                                   .collect(Collectors.toList()));
    }

    @Override
    public Val retry(final Predicate predicate,
                              final int attempts) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));
        if(predicate==null)
            return Cons.failure(new NullPointerException("predicate is null"));
        return new ParallelAny(exps.stream()
                                   .map(it -> it.retry(predicate,
                                                       attempts
                                                      ))
                                   .collect(Collectors.toList()));
    }


    @Override
    public Val retry(final Predicate predicate,
                              final int attempts,
                              final RetryPolicy retryPolicy) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));
        if (retryPolicy == null)
            return Cons.failure(new NullPointerException("retryPolicy is null"));
        if(predicate==null)
            return Cons.failure(new NullPointerException("predicate is null"));
        return new ParallelAny(exps.stream()
                                   .map(it -> it.retry(predicate,
                                                       attempts,
                                                       retryPolicy
                                                      ))
                                   .collect(Collectors.toList()));
    }


    @Override
    public Future get() {
        return CompositeFuture.all(exps.stream()
                                       .map(Supplier::get)
                                       .collect(Collectors.toList())
                                  )
                              .map(l -> l.result()
                                         .list()
                                         .stream()
                                         .anyMatch(it -> it instanceof Boolean && ((Boolean) it))
                                  );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy