Interface Predicates
-
public interface Predicates
The utilities class for JavaPredicate
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.function.Predicate[]
EMPTY_PREDICATE_ARRAY
An empty array ofPredicate
instances.
-
Method Summary
Static Methods Modifier and Type Method Description static <T> java.util.function.Predicate<T>
alwaysFalse()
Returns aPredicate
that always evaluates tofalse
.static <T> java.util.function.Predicate<T>
alwaysTrue()
Returns aPredicate
that always evaluates totrue
.static <T> java.util.function.Predicate<? super T>
and(java.util.function.Predicate<? super T>... predicates)
Returns a composed predicate that represents a short-circuiting logical AND of the given predicates.static <T> java.util.function.Predicate<T>[]
emptyArray()
Returns an empty array ofPredicate
instances.static <T> java.util.function.Predicate<? super T>
or(java.util.function.Predicate<? super T>... predicates)
Returns a composed predicate that represents a short-circuiting logical OR of the given predicates.
-
-
-
Field Detail
-
EMPTY_PREDICATE_ARRAY
@Immutable static final java.util.function.Predicate[] EMPTY_PREDICATE_ARRAY
An empty array ofPredicate
instances.This constant is useful when you need to return or pass a zero-length array of predicates, ensuring type safety and avoiding unnecessary allocations.
Example Usage
Predicate<String>[] emptyPredicates = Predicates.EMPTY_PREDICATE_ARRAY;
-
-
Method Detail
-
emptyArray
@Nonnull @Immutable static <T> java.util.function.Predicate<T>[] emptyArray()
Returns an empty array ofPredicate
instances.This method is useful when you need to return or pass a zero-length array of predicates, ensuring type safety and avoiding unnecessary allocations.
Example Usage
Predicate<String>[] emptyPredicates = Predicates.emptyArray();
- Type Parameters:
T
- the type of the input object to be tested by the predicates- Returns:
- a shared, empty array of
Predicate
instances
-
alwaysTrue
@Nonnull @Immutable static <T> java.util.function.Predicate<T> alwaysTrue()
Returns aPredicate
that always evaluates totrue
.This method is useful when you need a no-op predicate that accepts all inputs.
Example Usage
Predicate<String> predicate = Predicates.alwaysTrue(); boolean result = predicate.test("anyValue"); // result will always be true
- Type Parameters:
T
- the type of the input object to be tested (ignored in the implementation)- Returns:
- a
Predicate
that always returnstrue
-
alwaysFalse
@Nonnull @Immutable static <T> java.util.function.Predicate<T> alwaysFalse()
Returns aPredicate
that always evaluates tofalse
.This method is useful when you need a predicate that rejects all inputs.
Example Usage
Predicate<String> predicate = Predicates.alwaysFalse(); boolean result = predicate.test("anyValue"); // result will always be false
- Type Parameters:
T
- the type of the input object to be tested (ignored in the implementation)- Returns:
- a
Predicate
that always returnsfalse
-
and
@Nonnull static <T> java.util.function.Predicate<? super T> and(java.util.function.Predicate<? super T>... predicates)
Returns a composed predicate that represents a short-circuiting logical AND of the given predicates.This method applies the logical AND operation to the provided array of predicates. The resulting predicate will test its input against all the given predicates, returning
true
only if all predicates returntrue
. It uses short-circuit evaluation, meaning it stops testing as soon as one predicate returnsfalse
.Example Usage
Predicate<Integer> isPositive = i -> i > 0; Predicate<Integer> isEven = i -> i % 2 == 0; Predicate<Integer> isPositiveAndEven = Predicates.and(isPositive, isEven); System.out.println(isPositiveAndEven.test(4)); // Output: true System.out.println(isPositiveAndEven.test(-2)); // Output: false System.out.println(isPositiveAndEven.test(3)); // Output: false
- Type Parameters:
T
- the type of the input object to be tested- Parameters:
predicates
- an array of predicates to be logically ANDed together- Returns:
- a composed predicate that evaluates to
true
only if all given predicates evaluate totrue
-
or
@Nonnull static <T> java.util.function.Predicate<? super T> or(java.util.function.Predicate<? super T>... predicates)
Returns a composed predicate that represents a short-circuiting logical OR of the given predicates.This method applies the logical OR operation to the provided array of predicates. The resulting predicate will test its input against each predicate in sequence, returning
true
if any predicate returnstrue
. It uses short-circuit evaluation, meaning it stops testing as soon as one predicate returnstrue
.Example Usage
Predicate<Integer> isPositive = i -> i > 0; Predicate<Integer> isEven = i -> i % 2 == 0; Predicate<Integer> isPositiveOrEven = Predicates.or(isPositive, isEven); System.out.println(isPositiveOrEven.test(4)); // Output: true System.out.println(isPositiveOrEven.test(-2)); // Output: true System.out.println(isPositiveOrEven.test(3)); // Output: true System.out.println(isPositiveOrEven.test(-3)); // Output: false
- Type Parameters:
T
- the type of the input object to be tested- Parameters:
predicates
- an array of predicates to be logically ORed together- Returns:
- a composed predicate that evaluates to
true
if any of the given predicates evaluate totrue
-
-