Class Predicates
Predicate
instances.
All methods return serializable predicates as long as they're given serializable parameters.
See the Guava User Guide article on the use of Predicate
.
- Since:
- 2.0
- Author:
- Kevin Bourrillion
-
Method Summary
Modifier and TypeMethodDescriptionReturns a predicate that always evaluates tofalse
.Returns a predicate that always evaluates totrue
.Returns a predicate that evaluates totrue
if each of its components evaluates totrue
.Returns a predicate that evaluates totrue
if both of its components evaluate totrue
.Returns a predicate that evaluates totrue
if each of its components evaluates totrue
.Returns the composition of a function and a predicate.static Predicate
<CharSequence> Returns a predicate that evaluates totrue
if theCharSequence
being tested contains any match for the given regular expression pattern.static Predicate
<CharSequence> containsPattern
(String pattern) Returns a predicate that evaluates totrue
if theCharSequence
being tested contains any match for the given regular expression pattern.equalTo
(T target) Returns a predicate that evaluates totrue
if the object being testedequals()
the given target or both are null.in
(Collection<? extends T> target) Returns a predicate that evaluates totrue
if the object reference being tested is a member of the given collection.instanceOf
(Class<?> clazz) Returns a predicate that evaluates totrue
if the object being tested is an instance of the given class.isNull()
Returns a predicate that evaluates totrue
if the object reference being tested is null.Returns a predicate that evaluates totrue
if the given predicate evaluates tofalse
.notNull()
Returns a predicate that evaluates totrue
if the object reference being tested is not null.Returns a predicate that evaluates totrue
if any one of its components evaluates totrue
.Returns a predicate that evaluates totrue
if either of its components evaluates totrue
.Returns a predicate that evaluates totrue
if any one of its components evaluates totrue
.Returns a predicate that evaluates totrue
if the class being tested is assignable to (is a subtype of)clazz
.
-
Method Details
-
alwaysTrue
@GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> alwaysTrue()Returns a predicate that always evaluates totrue
. -
alwaysFalse
@GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> alwaysFalse()Returns a predicate that always evaluates tofalse
. -
isNull
Returns a predicate that evaluates totrue
if the object reference being tested is null. -
notNull
Returns a predicate that evaluates totrue
if the object reference being tested is not null. -
not
-
and
public static <T extends @Nullable Object> Predicate<T> and(Iterable<? extends Predicate<? super T>> components) Returns a predicate that evaluates totrue
if each of its components evaluates totrue
. 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. Ifcomponents
is empty, the returned predicate will always evaluate totrue
. -
and
@SafeVarargs public static <T extends @Nullable Object> Predicate<T> and(Predicate<? super T>... components) Returns a predicate that evaluates totrue
if each of its components evaluates totrue
. 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. Ifcomponents
is empty, the returned predicate will always evaluate totrue
. -
and
public static <T extends @Nullable Object> Predicate<T> and(Predicate<? super T> first, Predicate<? super T> second) Returns a predicate that evaluates totrue
if both of its components evaluate totrue
. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a false predicate is found. -
or
public static <T extends @Nullable Object> Predicate<T> or(Iterable<? extends Predicate<? super T>> components) Returns a predicate that evaluates totrue
if any one of its components evaluates totrue
. 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. Ifcomponents
is empty, the returned predicate will always evaluate tofalse
. -
or
@SafeVarargs public static <T extends @Nullable Object> Predicate<T> or(Predicate<? super T>... components) Returns a predicate that evaluates totrue
if any one of its components evaluates totrue
. 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. Ifcomponents
is empty, the returned predicate will always evaluate tofalse
. -
or
public static <T extends @Nullable Object> Predicate<T> or(Predicate<? super T> first, Predicate<? super T> second) Returns a predicate that evaluates totrue
if either of its components evaluates totrue
. The components are evaluated in order, and evaluation will be "short-circuited" as soon as a true predicate is found. -
equalTo
-
instanceOf
Returns a predicate that evaluates totrue
if the object being tested is an instance of the given class. If the object being tested isnull
this predicate evaluates tofalse
.If you want to filter an
Iterable
to narrow its type, consider usingIterables.filter(Iterable, Class)
in preference.Warning: contrary to the typical assumptions about predicates (as documented at
Predicate.apply(T)
), the returned predicate may not be consistent with equals. For example,instanceOf(ArrayList.class)
will yield different results for the two equal instancesLists.newArrayList(1)
andArrays.asList(1)
. -
subtypeOf
Returns a predicate that evaluates totrue
if the class being tested is assignable to (is a subtype of)clazz
. Example:
The code above returns an iterable containingList<Class<?>> classes = Arrays.asList( Object.class, String.class, Number.class, Long.class); return Iterables.filter(classes, subtypeOf(Number.class));
Number.class
andLong.class
.- Since:
- 20.0 (since 10.0 under the incorrect name
assignableFrom
)
-
in
Returns a predicate that evaluates totrue
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 usePredicates.<Object>in()
.- Parameters:
target
- the collection that may contain the function input
-
compose
public static <A extends @Nullable Object,B extends @Nullable Object> Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function) Returns the composition of a function and a predicate. For everyx
, the generated predicate returnspredicate(function(x))
.- Returns:
- the composition of the provided function and predicate
-
containsPattern
Returns a predicate that evaluates totrue
if theCharSequence
being tested contains any match for the given regular expression pattern. The test used is equivalent toPattern.compile(pattern).matcher(arg).find()
- Throws:
IllegalArgumentException
- if the pattern is invalid- Since:
- 3.0
-
contains
@GwtIncompatible("java.util.regex.Pattern") public static Predicate<CharSequence> contains(Pattern pattern) Returns a predicate that evaluates totrue
if theCharSequence
being tested contains any match for the given regular expression pattern. The test used is equivalent topattern.matcher(arg).find()
- Since:
- 3.0
-