Class Predicates

java.lang.Object
com.google.common.base.Predicates

@GwtCompatible(emulated=true) public final class Predicates extends Object
Static utility methods pertaining to 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 Details

    • alwaysTrue

      @GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> alwaysTrue()
      Returns a predicate that always evaluates to true.

      Discouraged: Prefer using x -> true, but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

    • alwaysFalse

      @GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> alwaysFalse()
      Returns a predicate that always evaluates to false.

      Discouraged: Prefer using x -> false, but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

    • isNull

      @GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> isNull()
      Returns a predicate that evaluates to true if the object reference being tested is null.

      Discouraged: Prefer using either x -> x == null or Objects::isNull, but note that lambdas and method references do not have human-readable Object.toString() representations and are not serializable.

    • notNull

      @GwtCompatible(serializable=true) public static <T extends @Nullable Object> Predicate<T> notNull()
      Returns a predicate that evaluates to true if the object reference being tested is not null.

      Discouraged: Prefer using either x -> x != null or Objects::nonNull, but note that lambdas and method references do not have human-readable Object.toString() representations and are not serializable.

    • not

      public static <T extends @Nullable Object> Predicate<T> not(Predicate<T> predicate)
      Returns a predicate that evaluates to true if the given predicate evaluates to false.

      Discouraged: Prefer using predicate.negate().

    • and

      public static <T extends @Nullable Object> Predicate<T> and(Iterable<? extends Predicate<? super T>> components)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.and(second).and(third).and(...).

    • and

      @SafeVarargs public static <T extends @Nullable Object> Predicate<T> and(Predicate<? super T>... components)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.and(second).and(third).and(...).

    • and

      public static <T extends @Nullable Object> Predicate<T> and(Predicate<? super T> first, Predicate<? super T> second)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.and(second).

    • or

      public static <T extends @Nullable Object> Predicate<T> or(Iterable<? extends Predicate<? super T>> components)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.or(second).or(third).or(...).

    • or

      @SafeVarargs public static <T extends @Nullable Object> Predicate<T> or(Predicate<? super T>... components)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.or(second).or(third).or(...).

    • or

      public static <T extends @Nullable Object> Predicate<T> or(Predicate<? super T> first, Predicate<? super T> second)
      Returns a predicate that evaluates to 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.

      Discouraged: Prefer using first.or(second).

    • equalTo

      public static <T extends @Nullable Object> Predicate<T> equalTo(T target)
      Returns a predicate that evaluates to true if the object being tested equals() the given target or both are null.

      Discouraged: Prefer using x -> Objects.equals(x, target), but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

    • instanceOf

      @GwtIncompatible public static <T extends @Nullable Object> Predicate<T> instanceOf(Class<?> clazz)
      Returns a predicate that evaluates to 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.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 instances Lists.newArrayList(1) and Arrays.asList(1).

      Discouraged: Prefer using clazz::isInstance or x -> x instanceof Clazz, but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

    • subtypeOf

      @GwtIncompatible public static Predicate<Class<?>> subtypeOf(Class<?> clazz)
      Returns a predicate that evaluates to true if the class being tested is assignable to (is a subtype of) clazz. Example:
      List<Class<?>> classes = Arrays.asList(
          Object.class, String.class, Number.class, Long.class);
      return Iterables.filter(classes, subtypeOf(Number.class));
      
      The code above returns an iterable containing Number.class and Long.class.

      Discouraged: Prefer using clazz::isAssignableFrom or x -> clazz.isAssignableFrom(x), but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

      Since:
      20.0 (since 10.0 under the incorrect name assignableFrom)
    • in

      public static <T extends @Nullable Object> Predicate<T> in(Collection<? extends T> target)
      Returns a predicate that evaluates to 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().

      You may prefer to use a method reference (e.g., target::contains) instead of this method. However, there are some subtle considerations:

      • The Predicate returned by this method is Serializable.
      • The Predicate returned by this method catches ClassCastException and NullPointerException.
      • Code that chains multiple predicates together (especially negations) may be more readable using this method. For example, not(in(target)) is generally more readable than not(target::contains).
      • This method's name conflicts with Kotlin's in operator.

      Discouraged: Prefer using either target::contains or x -> target.contains(x), but note that lambdas do not have human-readable Object.toString() representations and are not serializable.

      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 every x, the generated predicate returns predicate(function(x)).
      Returns:
      the composition of the provided function and predicate
    • containsPattern

      @GwtIncompatible public static Predicate<CharSequence> containsPattern(String pattern)
      Returns a predicate that evaluates to 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()
      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 to 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()
      Since:
      3.0