Class PredicateToolkit


  • public class PredicateToolkit
    extends Object
    Toolkit used to create instances of Predicate matching various criteria.
    • Constructor Detail

      • PredicateToolkit

        public PredicateToolkit()
    • Method Detail

      • truePredicate

        public static <T> Predicate<T> truePredicate()
        Returns:
        a predicate that always will test to true
      • falsePredicate

        public static <T> Predicate<T> falsePredicate()
        Returns:
        a predicate that always will test to false
      • isTrueGuaranteed

        public static boolean isTrueGuaranteed​(Predicate<?> p)
        Test if a predicate is guaranteed to always test to true. Note that if this method returns false, then it only means that it is unknown what the predicate will return.
        Parameters:
        p - a predicate to test
        Returns:
        true if the predicate is guaranteed to test to true
      • isFalseGuaranteed

        public static boolean isFalseGuaranteed​(Predicate<?> p)
        Test if a predicate is guaranteed to always test to false. Note that if this method returns false, then it only means that it is unknown what the predicate will return.
        Parameters:
        p - a predicate to test
        Returns:
        true if the predicate is guaranteed to test to false
      • and

        public static <T> Predicate<T> and​(Collection<Predicate<T>> predicates)
        Combine a collection of predicates using an AND operation.
        Parameters:
        predicates - input predicates
        Returns:
        a predicate that tests to true if all input predicates test to true
      • or

        public static <T> Predicate<T> or​(Collection<Predicate<T>> predicates)
        Combine a collection of predicates using an OR operation.
        Parameters:
        predicates - input predicates
        Returns:
        a predicate that tests to true if at least one of the input predicates test to true
      • not

        public static <T> Predicate<T> not​(Predicate<T> predicate)
        Invert a predicate.
        Parameters:
        predicate - predicate to invert
        Returns:
        a predicate that tests to true if the input predicate tests to false and vice versa
      • less

        public static <T,​M> Predicate<T> less​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                    Comparable<? super M> limit,
                                                    boolean orEqual)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        orEqual - if true, test values that are equal to the limit to true
        Returns:
        a predicate that tests to true if the value to check is less than, or optionally equal to, the limit value
      • less

        public static <T,​M> Predicate<T> less​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                    Comparable<? super M> limit)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        Returns:
        a predicate that tests to true if the value to check is strictly less than the limit value
      • lessOrEqual

        public static <T,​M> Predicate<T> lessOrEqual​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                           Comparable<? super M> limit)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        Returns:
        a predicate that tests to true if the value to check is less than or equal to the limit value
      • more

        public static <T,​M> Predicate<T> more​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                    Comparable<? super M> limit,
                                                    boolean orEqual)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        orEqual - if true, test values that are equal to the limit to true
        Returns:
        a predicate that tests to true if the value to check is greater than, or optionally equal to, the limit value
      • more

        public static <T,​M> Predicate<T> more​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                    Comparable<? super M> limit)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        Returns:
        a predicate that tests to true if the value to check is strictly greater than the limit value
      • moreOrEqual

        public static <T,​M> Predicate<T> moreOrEqual​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                           Comparable<? super M> limit)
        Create a predicate that compares values to a limit.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        limit - value to compare against
        Returns:
        a predicate that tests to true if the value to check is greater than or equal to the limit value
      • rangeIntersects

        public static <T,​M extends Comparable<? super M>> Predicate<T> rangeIntersects​(IMemberAccessor<? extends IRange<M>,​T> rangeAccessor,
                                                                                             IRange<M> limit)
        Return a predicate based on limit according to RangeMatchPolicy.CLOSED_INTERSECTS_WITH_CLOSED.

        The predicate takes an input object as argument but the range that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the range value that is compared
        Parameters:
        rangeAccessor - accessor used to get the range value to check from the input type
        limit - range value to compare against
        Returns:
        a predicate that tests to true if the range value to check intersects with the limit range
      • rangeContained

        public static <T,​M extends Comparable<? super M>> Predicate<T> rangeContained​(IMemberAccessor<? extends IRange<M>,​T> rangeAccessor,
                                                                                            IRange<M> limit)
        Return a predicate based on limit according to RangeMatchPolicy.CONTAINED_IN_CLOSED.

        The predicate takes an input object as argument but the range that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the range value that is compared
        Parameters:
        rangeAccessor - accessor used to get the range value to check from the input type
        limit - range value to compare against
        Returns:
        a predicate that tests to true if the range value to check is contained in the limit range
      • centerContained

        public static <T,​M extends Comparable<? super M>> Predicate<T> centerContained​(IMemberAccessor<? extends IRange<M>,​T> rangeAccessor,
                                                                                             IRange<M> limit)
        Return a predicate based on limit according to RangeMatchPolicy.CENTER_CONTAINED_IN_RIGHT_OPEN.

        The predicate takes an input object as argument but the range that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the range value that is compared
        Parameters:
        rangeAccessor - accessor used to get the range value to check from the input type
        limit - range value to compare against
        Returns:
        a predicate that tests to true if the center point of the range value to check is contained in the limit range
      • equals

        public static <T> Predicate<T> equals​(IMemberAccessor<?,​T> valueAccessor,
                                              Object item)
        Create a predicate that checks if a value is equal to a specified object.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        item - object to compare against
        Returns:
        a predicate that tests to true if the value to check is equal to the specified object
      • notEquals

        public static <T> Predicate<T> notEquals​(IMemberAccessor<?,​T> valueAccessor,
                                                 Object item)
        Create a predicate that checks if a value is not equal to a specified object.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        item - object to compare against
        Returns:
        a predicate that tests to true if the value to check is not equal to the specified object
      • is

        public static <T> Predicate<T> is​(T item)
        Create a predicate that checks if a value is a specified object. This check is performed using object identity.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        Parameters:
        item - object to compare against
        Returns:
        a predicate that tests to true if the value to check is the specified object
      • memberOf

        public static <T,​M> Predicate<T> memberOf​(IMemberAccessor<? extends M,​T> valueAccessor,
                                                        Set<? extends M> items)
        Create a predicate that checks if a value is included in a specified set.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        M - type of the range value that is compared
        Parameters:
        valueAccessor - accessor used to get the value to check from the input type
        items - set of objects to compare against
        Returns:
        a predicate that tests to true if the object to check is included in the specified set
      • matches

        public static <T> Predicate<T> matches​(IMemberAccessor<? extends String,​T> valueAccessor,
                                               String regexp)
        Create a predicate that checks if a string value matches a regular expression.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Type Parameters:
        T - type of objects passed into the predicate
        Parameters:
        valueAccessor - string accessor used to get the value to check from the input type
        regexp - the regular expression to match
        Returns:
        a predicate that tests to true if the string value matches the regular expression
      • contains

        public static <T> Predicate<T> contains​(IMemberAccessor<? extends String,​T> valueAccessor,
                                                String substring)
        Create a predicate that checks if a string value contains a specified substring.

        The predicate takes an input object as argument but the value that is checked is extracted from the input object using a member accessor.

        Parameters:
        valueAccessor - string accessor used to get the value to check from the input type
        substring - the substring to look for
        Returns:
        a predicate that tests to true if the string value contains the substring
      • getValidPattern

        public static Pattern getValidPattern​(String regexp)
        Compile a regular expression into a pattern if possible. If the expression can't be compiled, return a valid pattern that will give 0 matches (at least for single lines).
        Parameters:
        regexp - regular expression to compile
        Returns:
        a valid regular expression pattern instance