Class Criteria<T>

  • Type Parameters:
    T - The generic criteria value type.
    Direct Known Subclasses:
    Between, Bool, Enumerated, IgnoreCase, Like, Not, Numeric, Order

    public abstract class Criteria<T>
    extends Object

    This is used by Page and BaseEntityService.getPage(Page, boolean). It defines a set of criteria which could be supplied as value of "required criteria" and "optional criteria" maps.

    There are so far the following criteria:

    • Not - to negate the value
    • Like - to search a string value
    • Order - to perform "less than" or "greater than" searches
    • Between - to perform "between" searches
    • Enumerated - to parse value as enum
    • Numeric - to parse value as number
    • Bool - to parse value as boolean
    • IgnoreCase - to perform "ignore case" searches

    You can create your own ones if you want to have more fine grained control over how criteria values are parsed and turned into a predicate.

    An elaborate use case can be found in OptimusFaces project.

    Author:
    Bauke Scholtz
    See Also:
    Page, BaseEntityService
    • Constructor Detail

      • Criteria

        protected Criteria​(T value)
        Create criteria based on given value.
        Parameters:
        value - The criteria value.
        Throws:
        IllegalArgumentException - When given criteria value cannot be reasonably parsed.
    • Method Detail

      • build

        public abstract Predicate build​(Expression<?> path,
                                        CriteriaBuilder criteriaBuilder,
                                        Criteria.ParameterBuilder parameterBuilder)
        Returns a predicate for the criteria value. Below is an example implementation:
         return criteriaBuilder.equal(path, parameterBuilder.create(getValue()));
         
        Parameters:
        path - Entity property path. You can use this to inspect the target entity property.
        criteriaBuilder - So you can build a predicate with a ParameterExpression.
        parameterBuilder - You must use this to create a ParameterExpression for the criteria value.
        Returns:
        A predicate for the criteria value.
      • applies

        public boolean applies​(Object modelValue)
        Returns whether this criteria value would apply to the given model value. This must basically represent the "plain Java" equivalent of the SQL behavior as achieved by build(Expression, CriteriaBuilder, ParameterBuilder).
        Parameters:
        modelValue - The model value to test this criteria on.
        Returns:
        Whether this criteria value would apply to the given model value.
        Throws:
        IllegalArgumentException - When given model value cannot be reasonably parsed.
        UnsupportedOperationException - When this method is not implemented yet.
      • getValue

        public T getValue()
        Returns the criteria value.
        Returns:
        The criteria value.
      • unwrap

        public static Object unwrap​(Object possibleCriteria)
        Unwraps the criteria value from given object which could possibly represent a Criteria.
        Parameters:
        possibleCriteria - Any object which could possibly represent a Criteria.
        Returns:
        The unwrapped criteria value when given object actually represents a Criteria, else the original value unmodified.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object