Package org.apache.juneau.commons.utils
Class PredicateUtils
java.lang.Object
org.apache.juneau.commons.utils.PredicateUtils
Utility methods for composing
Predicate instances.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidConsumes the specified value if the predicate isnull or matches the specified value.static <T> Predicate<T>distinctByKey(Function<? super T, ?> keyExtractor) Returns a predicate that filters out duplicate elements based on a key extractor function.static <T> Function<T,T> peek()Returns a function that prints the input value to stderr and returns it unchanged.static <T> Function<T,T> Returns a function that prints the input value to stderr using a custom formatter and returns it unchanged.static <T> booleanReturnstrue if the specified predicate isnull or matches the specified value.
-
Method Details
-
consumeIf
Consumes the specified value if the predicate isnull or matches the specified value.- Type Parameters:
T- The type being consumed.- Parameters:
predicate- The predicate. Can benull (treated as an always-match).consumer- The consumer. Must not benull when the predicate matches.value- The value. Can benull .
-
peek
Returns a function that prints the input value to stderr and returns it unchanged.- Type Parameters:
T- The type of value.- Returns:
- A function that prints and returns the value.
-
peek
Returns a function that prints the input value to stderr using a custom formatter and returns it unchanged.- Type Parameters:
T- The type of value.- Parameters:
message- A format string using{0}as placeholder for the formatted value. Must not benull .formatter- A function to extract/format the value for display. Must not benull .- Returns:
- A function that prints and returns the value.
-
test
Returnstrue if the specified predicate isnull or matches the specified value.- Type Parameters:
T- The type being tested.- Parameters:
predicate- The predicate. Can benull (returnstrue ).value- The value to test. Can benull .- Returns:
true if the specified predicate isnull or matches the specified value.
-
distinctByKey
Returns a predicate that filters out duplicate elements based on a key extractor function.This is useful for filtering streams to keep only the first occurrence of each unique key.
Example:
// Filter a stream to keep only methods with unique signatures Stream<Method>methods = ...; Stream<Method>unique =methods .filter(PredicateUtils.distinctByKey (Method::getName ));- Type Parameters:
T- The element type.- Parameters:
keyExtractor- A function that extracts the key from each element. Must not benull .- Returns:
- A predicate that returns
true for the first occurrence of each unique key, andfalse for subsequent occurrences with the same key.
-