Interface Predicates


  • public interface Predicates
    The utilities class for Java Predicate
    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 of Predicate instances.
    • Method Summary

      Static Methods 
      Modifier and Type Method Description
      static <T> java.util.function.Predicate<T> alwaysFalse()
      Returns a Predicate that always evaluates to false.
      static <T> java.util.function.Predicate<T> alwaysTrue()
      Returns a Predicate that always evaluates to true.
      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 of Predicate 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

        static final java.util.function.Predicate[] EMPTY_PREDICATE_ARRAY
        An empty array of Predicate 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
        static <T> java.util.function.Predicate<T>[] emptyArray()
        Returns an empty array of Predicate 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
        static <T> java.util.function.Predicate<T> alwaysTrue()
        Returns a Predicate that always evaluates to true.

        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 returns true
      • alwaysFalse

        @Nonnull
        static <T> java.util.function.Predicate<T> alwaysFalse()
        Returns a Predicate that always evaluates to false.

        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 returns false
      • 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 return true. It uses short-circuit evaluation, meaning it stops testing as soon as one predicate returns false.

        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 to true
      • 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 returns true. It uses short-circuit evaluation, meaning it stops testing as soon as one predicate returns true.

        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 to true