Static utility methods pertaining to Predicate instances. All methods returns serializable predicates as long as they're given serializable parameters. See the Guava User Guide article on the use of Predicate<
| 42 | * @since 2.0 |
| 43 | */ |
| 44 | @GwtCompatible(emulated = true) |
| 45 | public final class Predicates { |
| 46 | private Predicates() {} |
| 47 | |
| 48 | // TODO(kevinb): considering having these implement a VisitablePredicate |
| 49 | // interface which specifies an accept(PredicateVisitor) method. |
| 50 | |
| 51 | /** |
| 52 | * Returns a predicate that always evaluates to {@code true}. |
| 53 | */ |
| 54 | @GwtCompatible(serializable = true) |
| 55 | public static <T> Predicate<T> alwaysTrue() { |
| 56 | return ObjectPredicate.ALWAYS_TRUE.withNarrowedType(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns a predicate that always evaluates to {@code false}. |
| 61 | */ |
| 62 | @GwtCompatible(serializable = true) |
| 63 | public static <T> Predicate<T> alwaysFalse() { |
| 64 | return ObjectPredicate.ALWAYS_FALSE.withNarrowedType(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a predicate that evaluates to {@code true} if the object reference being tested is |
| 69 | * null. |
| 70 | */ |
| 71 | @GwtCompatible(serializable = true) |
| 72 | public static <T> Predicate<T> isNull() { |
| 73 | return ObjectPredicate.IS_NULL.withNarrowedType(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns a predicate that evaluates to {@code true} if the object reference being tested is not |
| 78 | * null. |
| 79 | */ |
| 80 | @GwtCompatible(serializable = true) |
| 81 | public static <T> Predicate<T> notNull() { |
| 82 | return ObjectPredicate.NOT_NULL.withNarrowedType(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns a predicate that evaluates to {@code true} if the given predicate evaluates to |
| 87 | * {@code false}. |
| 88 | */ |
| 89 | public static <T> Predicate<T> not(Predicate<T> predicate) { |
| 90 | return new NotPredicate<T>(predicate); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns a predicate that evaluates to {@code true} if each of its components evaluates to |
| 95 | * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited" |
| 96 | * as soon as a false predicate is found. It defensively copies the iterable passed in, so future |
| 97 | * changes to it won't alter the behavior of this predicate. If {@code |
| 98 | * components} is empty, the returned predicate will always evaluate to {@code |
| 99 | * true}. |
| 100 | */ |
| 101 | public static <T> Predicate<T> and(Iterable<? extends Predicate<? super T>> components) { |