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

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

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

import io.vavr.Tuple2;
import io.vavr.collection.List;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import vertx.effect.RetryPolicy;
import vertx.effect.Val;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

/**
 Represents a supplier of a vertx future which result is a json object. It has the same
 recursive structure as a json object. Each key has a future associated that it's
 executed asynchronously. When all the futures are completed, all the results are combined into
 a json object.
 */
final class ParallelMapExp extends MapExp {

    @SuppressWarnings({"rawtypes"})
    public static final ParallelMapExp EMPTY = new ParallelMapExp<>();


    ParallelMapExp() {
    }

    ParallelMapExp(final io.vavr.collection.Map> bindings) {
        this.bindings = requireNonNull(bindings);
    }


    /**
     returns a new object future inserting the given future at the given key

     @param key the given key
     @param exp the given future
     @return a new JsObjFuture
     */
    @Override
    public ParallelMapExp set(final String key,
                                 final Val exp
                                ) {
        return new ParallelMapExp<>(bindings.put(requireNonNull(key),
                                                 requireNonNull(exp)
                                                ));
    }

    @Override
    public Val> retry(final int attempts) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));
        return new ParallelMapExp<>(bindings.mapValues(it -> it.retry(attempts)));
    }


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

        return new ParallelMapExp<>(bindings.mapValues(it -> it.retry(attempts,
                                                                      retryPolicy
                                                                     )
                                                      ));
    }

    @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 ParallelMapExp<>(bindings.mapValues(it -> it.retry(predicate,
                                                                      attempts
                                                                     ))
        );

    }


    @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 (predicate == null)
            return Cons.failure(new NullPointerException("predicate is null"));
        if (retryPolicy == null)
            return Cons.failure(new NullPointerException("retryPolicy is null"));

        return new ParallelMapExp<>(bindings.mapValues(it -> it.retry(predicate,
                                                                      attempts,
                                                                      retryPolicy
                                                                     )));
    }


    @Override
    public Future> get() {

        List keys = bindings.keysIterator()
                                    .toList();
        @SuppressWarnings({"rawtypes"})
        io.vavr.collection.Map futures = bindings.map((k, v) -> new Tuple2<>(k,
                                                                                             v.get()
                                                                      )
                                                                     );
        return CompositeFuture.all(futures.values()
                                          .toJavaList()
                                  )
                              .map(r -> {
                                  Map result = new LinkedHashMap<>();
                                  java.util.List list = r.result()
                                                            .list();
                                  for (int i = 0; i < list.size(); i++) {
                                      result.put(keys.get(i),
                                                 list.get(i)
                                                );

                                  }

                                  return result;
                              });
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy