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

jio.CondExpSeq Maven / Gradle / Ivy

The newest version!
package jio;

import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import jio.Result.Failure;

final class CondExpSeq extends CondExp {

  final List> tests;
  final List>> consequences;
  final Supplier> otherwise;

  public CondExpSeq(List> tests,
                    List>> consequences,
                    Supplier> otherwise,
                    Function> debugger
                   ) {
    super(debugger);
    this.tests = tests;
    this.consequences = consequences;
    this.otherwise = otherwise;
  }

  private static  Result get(List> tests,
                                   List>> consequences,
                                   Supplier> otherwise,
                                   int condTestedSoFar
                                  ) {

    try {
      if (condTestedSoFar == tests.size()) {
        return otherwise.get()
                        .call();
      }
      return tests.get(condTestedSoFar)
                  .call()
                  .getOutputOrThrow() ? consequences.get(condTestedSoFar)
                                                    .get()
                                                    .call() : get(tests,
                                                      consequences,
                                                      otherwise,
                                                      condTestedSoFar + 1);
    } catch (Exception e) {
      return new Failure<>(e);
    }

  }

  @Override
  Result reduceExp() {
    return get(tests,
               consequences,
               otherwise,
               0
              );
  }

  @Override
  public CondExp retryEach(final Predicate predicate,
                                   final RetryPolicy policy
                                  ) {
    requireNonNull(predicate);
    requireNonNull(policy);
    return new CondExpSeq<>(tests.stream()
                                 .map(it -> it.retry(predicate,
                                                     policy
                                                    ))
                                 .collect(Collectors.toList()),
                            consequences.stream()
                                        .map(Fun.mapSupplier(it -> it.retry(predicate,
                                                                            policy)))
                                        .toList(),
                            otherwise,
                            jfrPublisher
    );
  }

  @Override
  public CondExp debugEach(final EventBuilder eventBuilder) {
    Objects.requireNonNull(eventBuilder);
    return new CondExpSeq<>(DebuggerHelper.debugConditions(tests,
                                                           EventBuilder.of("%s-test".formatted(eventBuilder.exp),
                                                                           eventBuilder.context)
                                                          ),
                            DebuggerHelper.debugSuppliers(consequences,
                                                          "%s-consequence".formatted(eventBuilder.exp),
                                                          eventBuilder.context
                                                         ),
                            DebuggerHelper.debugSupplier(otherwise,
                                                         "%s-otherwise".formatted(eventBuilder.exp),
                                                         eventBuilder.context
                                                        ),
                            getJFRPublisher(eventBuilder)
    );
  }

  @Override
  public CondExp debugEach(final String context) {
    return debugEach(
        EventBuilder.of(this.getClass()
                            .getSimpleName(),
                        context));

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy