1

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.

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

In Java, you don't have to use the returned value of an expression, i.e., x.getNextStep().equals(Integer.zero); would be just fine as statement. In lambda, you omit the ; so the compiler can't say you intended to ignore the returned value, thus pick Publisher or meant to use the returned value, thus pick Predicate. boolean cast would not make sense as part of the statement hence the use disambiguates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.