Home > other >  ambiguous error using generics in rxjava to loop condition
ambiguous error using generics in rxjava to loop condition

Time:07-20

I can't use takeUntil in rxjava because I receive this error:

takeUntil(Predicate<? super T>) is ambiguous for the type (Flowable<T>)

my code is like this:

public class Step {

  private Integer actualStep;
  private Integer nextStep;
  //getters and setters
}

public Interface<T> BusinessStep {

  Single<T> execute(T data);
}

public ComposedStep<T extends Step> implements BusinessStep<T> {
  private Map<Integer,BusinessStep<T>> steps = new HashMap<>();
  
  public void addStep(final Integer priority,
      final BusinessStep<T> businessStepValidator) {
    if (Objects.isNull(priority) || Objects.isNull(businessStepValidator)) {
      return;
    }
    validations.put(priority, businessStepValidator);
  }
   
   @override
   public Single<T> stepValidator(final T data) {

    return Single.just(data)
                 .flatMap(x -> {
                  Optional<WrapperBusinessStepValidator<T>> oBusinessStep = 
                                                        Optional.ofNullable(validations
                                                       .get(x.getNextStep()));
                  if(oBusinessStep.isPresent()) {
                    return oBusinessStep.get().stepValidator(x);
                  } else {
                    return Single.just(x);
                  }
                 })
                 .repeat()
                 .takeUntil(x -> x.getNextStep().equals(Integer.zero))
                 .lastElement()
                 .toSingle()
                 ;
                 

  }
}

in steps there are a map with implementations of BusinessStep to receive a child of Step and change nextStep. I need to create a loop in order to execute several BusinessStep while nextStep is different to a condition.

The problem is that code isn't compile because the error that I mentioned in first part of text.

CodePudding user response:

The compiler doesn't know if you want takeUntil(Predicate<? super T>) or takeUntil(Publisher<U>), because technically your lambda function matches both. Just explicitly cast it tell the compiler which you want:

      .takeUntil((Predicate<? super T>) x -> x.getNextStep().equals(Integer.zero))

Even this will remove the ambiguity (though I'm not 100% sure why, given that equals is already returning a boolean):

      .takeUntil(x -> (boolean) x.getNextStep().equals(Integer.zero))
  • Related