@GwtCompatible(emulated=true) public final class Predicates extends Object
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.
| Modifier and Type | Method and Description |
|---|---|
static <T> Predicate |
alwaysFalse()
Returns a predicate that always evaluates to
false.
|
static <T> Predicate |
alwaysTrue()
Returns a predicate that always evaluates to
true.
|
static <T> Predicate |
and(Iterable
Returns a predicate that evaluates to
true if each of its components evaluates to
true.
|
static <T> Predicate |
and(Predicate
Returns a predicate that evaluates to
true if each of its components evaluates to
true.
|
static <T> Predicate |
and(Predicate
Returns a predicate that evaluates to
true if both of its components evaluate to
true.
|
static Predicate |
assignableFrom(Class
Returns a predicate that evaluates to
true if the class being tested is assignable from the given class.
|
static <A |
compose(Predicate
Returns the composition of a function and a predicate.
|
static Predicate |
contains(Pattern
Returns a predicate that evaluates to
true if the
CharSequence being tested contains any match for the given regular expression pattern.
|
static Predicate |
containsPattern(String
Returns a predicate that evaluates to
true if the
CharSequence being tested contains any match for the given regular expression pattern.
|
static <T> Predicate |
equalTo(T target)
Returns a predicate that evaluates to
true if the object being tested
equals() the given target or both are null.
|
static <T> Predicate |
in(Collection
Returns a predicate that evaluates to
true if the object reference being tested is a member of the given collection.
|
static Predicate |
instanceOf(Class
Returns a predicate that evaluates to
true if the object being tested is an instance of the given class.
|
static <T> Predicate |
isNull()
Returns a predicate that evaluates to
true if the object reference being tested is null.
|
static <T> Predicate |
not(Predicate
Returns a predicate that evaluates to
true if the given predicate evaluates to
false.
|
static <T> Predicate |
notNull()
Returns a predicate that evaluates to
true if the object reference being tested is not null.
|
static <T> Predicate |
or(Iterable
Returns a predicate that evaluates to
true if any one of its components evaluates to
true.
|
static <T> Predicate |
or(Predicate
Returns a predicate that evaluates to
true if any one of its components evaluates to
true.
|
static <T> Predicate |
or(Predicate
Returns a predicate that evaluates to
true if either of its components evaluates to
true.
|
@GwtCompatible(serializable=true) public static <T> Predicate<T> alwaysTrue()
true.
@GwtCompatible(serializable=true) public static <T> Predicate<T> alwaysFalse()
false.
@GwtCompatible(serializable=true) public static <T> Predicate<T> isNull()
true if the object reference being tested is null.
@GwtCompatible(serializable=true) public static <T> Predicate<T> notNull()
true if the object reference being tested is not null.
public static <T> Predicate<T> not(Predicate <T> predicate)
true if the given predicate evaluates to
false.
public static <T> Predicate<T> and(Iterable <? extends Predicate <? super T>> components)
true if each of its components evaluates to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a false predicate is found. It defensively copies the iterable passed in, so future changes to it won't alter the behavior of this predicate. If
components is empty, the returned predicate will always evaluate to
true.
public static <T> Predicate<T> and(Predicate <? super T>... components)
true if each of its components evaluates to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a false predicate is found. It defensively copies the array passed in, so future changes to it won't alter the behavior of this predicate. If
components is empty, the returned predicate will always evaluate to
true.
public static <T> Predicate<T> and(Predicate <? super T> first, Predicate <? super T> second)
true if both of its components evaluate to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a false predicate is found.
public static <T> Predicate<T> or(Iterable <? extends Predicate <? super T>> components)
true if any one of its components evaluates to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a true predicate is found. It defensively copies the iterable passed in, so future changes to it won't alter the behavior of this predicate. If
components is empty, the returned predicate will always evaluate to
false.
public static <T> Predicate<T> or(Predicate <? super T>... components)
true if any one of its components evaluates to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a true predicate is found. It defensively copies the array passed in, so future changes to it won't alter the behavior of this predicate. If
components is empty, the returned predicate will always evaluate to
false.
public static <T> Predicate<T> or(Predicate <? super T> first, Predicate <? super T> second)
true if either of its components evaluates to
true. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a true predicate is found.
public static <T> Predicate<T> equalTo(T target)
true if the object being tested
equals() the given target or both are null.
@GwtIncompatible(value="Class.isInstance") public static Predicate<Object > instanceOf(Class <?> clazz)
true if the object being tested is an instance of the given class. If the object being tested is
null this predicate evaluates to
false.
If you want to filter an Iterable to narrow its type, consider using Iterables in preference.
Warning: contrary to the typical assumptions about predicates (as documented at Predicate), the returned predicate may not be consistent with equals. For example, instanceOf(ArrayList.class) will yield different results for the two equal instances Lists.newArrayList(1) and Arrays.asList(1).
@GwtIncompatible(value="Class.isAssignableFrom") @Beta public static Predicate<Class <?>> assignableFrom(Class <?> clazz)
true if the class being tested is assignable from the given class. The returned predicate does not allow null inputs.
public static <T> Predicate<T> in(Collection <? extends T> target)
true if the object reference being tested is a member of the given collection. It does not defensively copy the collection passed in, so future changes to it will alter the behavior of the predicate.
This method can technically accept any Collection<?>, but using a typed collection helps prevent bugs. This approach doesn't block any potential users since it is always possible to use Predicates.<Object>in().
target - the collection that may contain the function input
public static <A,B> Predicate <A> compose(Predicate <B> predicate, Function <A ,? extends B> function)
x, the generated predicate returns
predicate(function(x)).
@GwtIncompatible(value="java.util.regex.Pattern") public static Predicate<CharSequence > containsPattern(String pattern)
true if the
CharSequence being tested contains any match for the given regular expression pattern. The test used is equivalent to
Pattern.compile(pattern).matcher(arg).find()
PatternSyntaxException - if the pattern is invalid
@GwtIncompatible(value="java.util.regex.Pattern") public static Predicate<CharSequence > contains(Pattern pattern)
true if the
CharSequence being tested contains any match for the given regular expression pattern. The test used is equivalent to
pattern.matcher(arg).find()