Guava multiple interface and generics, got error "The method XXX in the type XXX is not applicable for the arguments"

541 views Asked by At

Predicate and() method is defined like this :

 com.google.common.base.Predicates.and(Predicate<? super T>, Predicate<? super T>)

I have three interfaces with the third extending others :

public interface IFolderable {
    String getFolder();
}

public interface IId {
    int getId();
}

public interface IBoth extends IFolderable, IId {}

And an object implementing the third:

public class MyClass implements IBoth {
     String getFolder() {
          return "myFolder";
     }
     int getId() {
          return 1;
     }
}

I want to do something like this :

    List<MyClass> list = Lists.newArrayList();
    Collections2.filter(list, Predicates.and(new Predicate<IFolderable>() {
         // core predicate using getFolder();
    }, new Predicate<IId>() {
         // core predicate using getId();
    }));

But this code generate this error at compile time :

The method and(Predicate<? super T>, Predicate<? super T>) in the type Predicates is not applicable for the arguments (new Predicate<IFolderable>(){}, new Predicate<IId>(){})

How can I mix two predicates specialized on one interface over the and() guava method, in order to filter a list of MyClass ?

More generally, how can I specialized generic method in that way ?

I don't want to declare Predicate with IBoth generic type, because I have other object implementing just IFolderable or IId. (I don't want to have a new Predicate<IBoth>)

0

There are 0 answers