Common Predicates


class And implements Predicate {
  And(Predicate lhs, Predicate rhs) { ... }

  boolean test(Object o) {
    return lhs_.test(o) && rhs_.test(o);
  }
}


class Or implements Predicate {
  Or(Predicate lhs, Predicate rhs) { ... }

  boolean test(Object o) {
    return lhs_.test(o) || rhs_.test(o);
  }
}
And and Or - your normal everyday logical expressions. Not is trivial too, but there wasn't quite space to fit it on.

Classes like this, together with "domain appropriate" classes can wildly simplify complex selection/filtering/searching, particularly when the criteria are runtime configurable or when there are several different collections to iterate over.

Here's a real example.