Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stream reduction incompatible types

I'm trying to create a finder which takes several predicates and reduces them:

public static <T extends BusinessInterface> Collection<T> findOr(
    Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) {
  Predicate<? super T> lReducedPredicate =
      Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false);
  return find(pContext, pClass, lReducedPredicate);
}

Unfortunately I get following compiler error:

Predicate lReducedPredicate = Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false); incompatible types: Predicate cannot be converted to Predicate where T is a type-variable: T extends BusinessInterface declared in method findOr(Context,Class,Predicate...) where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends Object super: T from capture of ? super T CAP#2 extends Object super: T from capture of ? super T

I get not errors in Eclipse and I have no idea what is going wrong.

Any help is really appreciated :).

like image 394
user1567896 Avatar asked Dec 11 '22 10:12

user1567896


1 Answers

One way to solve this is using

public static <T extends BusinessInterface> Collection<T> findOr(
    Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) {
  Predicate<? super T> lReducedPredicate = Arrays.asList(pPredicates).stream()
    .reduce((a,b) -> t -> a.test(t) || b.test(t)).orElse(r -> false);
  return find(pContext, pClass, lReducedPredicate);
}

While you can’t invoke the or method on a Predicate<? super T> instance with another Predicate<? super T> as argument, you can create the same function, or would return, yourself, as passing a T instance to either test method is valid.

like image 183
Holger Avatar answered Dec 21 '22 05:12

Holger