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

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

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

import io.vavr.Tuple2;
import io.vertx.core.Future;
import vertx.effect.RetryPolicy;
import vertx.effect.Val;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

final class SequentialPair extends Pair {

    private final Val _1;
    private final Val _2;
    private static final String ATTEMPTS_LOWER_THAN_ONE_ERROR = "attempts < 1";

    SequentialPair(final Val _1,
                   final Val _2) {
        this._1 = requireNonNull(_1);
        this._2 = requireNonNull(_2);
    }


    @Override
    public Val> retry(final int attempts) {
        if (attempts < 1)
            return Cons.failure(new IllegalArgumentException(ATTEMPTS_LOWER_THAN_ONE_ERROR));
        return new SequentialPair<>(_1.retry(attempts),
                                    _2.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));
        if (retryPolicy == null)
            return Cons.failure(new NullPointerException("retryPolicy is null"));
        return new SequentialPair<>(_1.retry(attempts,
                                             retryPolicy
                                            ),
                                    _2.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 SequentialPair<>(_1.retry(predicate,
                                             attempts
                                            ),
                                    _2.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 SequentialPair<>(_1.retry(predicate,
                                             attempts
                                            ),
                                    _2.retry(predicate,
                                             attempts
                                            )
        );
    }


    @Override
    public Future> get() {
        return _1.get()
                 .flatMap(first -> _2.get()
                                     .map(sec -> new Tuple2<>(first,
                                                              sec
                                          )
                                         )
                         );

    }


    @Override
    public Val _1() {
        return _1;
    }

    @Override
    public Val _2() {
        return _2;
    }
}