0

I want to filter grid content according to two filtering criterias, therefore I have the following

ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid.getDataProvider();
SerializablePredicate<Person> filter = new SerializablePredicate<Person>() {
  @Override
  public boolean test(Person Person) {
    return false;
  }
};

filter.and(Person -> Person.getAge() == 30);
filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);

}

However, the grid does not show anything.

LucasNesk
  • 183
  • 8
FarFarAway
  • 1,017
  • 3
  • 14
  • 35

2 Answers2

2

Your conjunction always evaluates to false because it is modeled like:

false && Person.getAge() == 30 && Person.getName().equalsIgnoreCase("bernd")

Either you're using return true in the first filter, or you ommit it and start with Person.getAge() == 30.

Second problem is that you're ignoring the results of your conjunction. In addition to this, the method and is defined in Predicate and returns a non-serializable Predicate. Solution would be to use a simple &&.

SerializablePredicate<Person> filter = p -> p.getAge() == 30 &&
                                            p.getName().equalsIgnoreCase("bernd");
Flown
  • 11,480
  • 3
  • 45
  • 62
  • I have just used return true. the result is that the filters does not get evaluated – FarFarAway Jan 23 '18 at 12:45
  • 1
    As I said you're ignoring the return-values of your conjunction. See my edit and try to use the new `SerializablePredicate`. – Flown Jan 23 '18 at 12:52
  • Btw I wouldn't recommend naming the lambda parameter exactly the same as your class. This might be confusing because one could think that you access the class. – Steffen Harbich Jan 23 '18 at 19:17
0

There are two different problems. First one:

The function

default Predicate<T> and(Predicate<? super T> other)

returns a new predicate.

ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) 

grid.getDataProvider();
SerializablePredicate<Person> filter = new SerializablePredicate<Person>() {
  @Override
  public boolean test(Person Person) {
    return true;
  }
};

filter = filter.and(Person -> Person.getAge() == 30);
filter = filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);

The other problem is, that ListDataProvider expected a SerializablePredicate.

You can extend the SerializiablePredicate

public interface ExtendedSerializablePredicate<T> extends SerializablePredicate<T> {

    default ExtendedSerializablePredicate<T> and(ExtendedSerializablePredicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
}

now you can use this:

ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) 

grid.getDataProvider();
ExtendedSerializablePredicate<Person> filter = new ExtendedSerializablePredicate<Person>() {
  @Override
  public boolean test(Person Person) {
    return true;
  }
};

filter = filter.and(Person -> Person.getAge() == 30);
filter = filter.and(Person -> Person.getName().equalsIgnoreCase("bernd"));
dataProvider.setFilter(filter);
mrboo
  • 76
  • 5