Class PredicateUtils

java.lang.Object
org.apache.juneau.commons.utils.PredicateUtils

public final class PredicateUtils extends Object
Utility methods for composing Predicate instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    consumeIf(Predicate<T> predicate, Consumer<T> consumer, T value)
    Consumes the specified value if the predicate is null or matches the specified value.
    static <T> Predicate<T>
    distinctByKey(Function<? super T,?> keyExtractor)
     
    static <T> Function<T,T>
    Returns a function that prints the input value to stderr and returns it unchanged.
    static <T> Function<T,T>
    peek(String message, Function<T,?> formatter)
    Returns a function that prints the input value to stderr using a custom formatter and returns it unchanged.
    static <T> boolean
    test(Predicate<T> predicate, T value)
    Returns true if the specified predicate is null or matches the specified value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • consumeIf

      public static <T> void consumeIf(Predicate<T> predicate, Consumer<T> consumer, T value)
      Consumes the specified value if the predicate is null or matches the specified value.
      Type Parameters:
      T - The type being consumed.
      Parameters:
      predicate - The predicate.
      consumer - The consumer.
      value - The value.
    • peek

      public static <T> Function<T,T> peek()
      Returns a function that prints the input value to stderr and returns it unchanged.

      Useful for debugging streams by inserting into a stream pipeline with .map(peek()).

      Example:

      list.stream() .map(peek()) .filter(x -> x != null) .collect(Collectors.toList());

      Type Parameters:
      T - The type of value.
      Returns:
      A function that prints and returns the value.
    • peek

      public static <T> Function<T,T> peek(String message, Function<T,?> formatter)
      Returns a function that prints the input value to stderr using a custom formatter and returns it unchanged.

      Useful for debugging streams by inserting into a stream pipeline with .map(peek(...)).

      Example:

      list.stream() .map(peek("Processing: {0}", x -> x.getName())) .filter(x -> x != null) .collect(Collectors.toList());

      Type Parameters:
      T - The type of value.
      Parameters:
      message - A format string using {0} as placeholder for the formatted value.
      formatter - A function to extract/format the value for display.
      Returns:
      A function that prints and returns the value.
    • test

      public static <T> boolean test(Predicate<T> predicate, T value)
      Returns true if the specified predicate is null or matches the specified value.
      Type Parameters:
      T - The type being tested.
      Parameters:
      predicate - The predicate.
      value - The value to test.
      Returns:
      true if the specified predicate is null or matches the specified value.
    • distinctByKey

      public static <T> Predicate<T> distinctByKey(Function<? super T,?> keyExtractor)