Class Restrictions


  • public class Restrictions
    extends Object
    A factory class to create restrictions.
    Version:
    2.0
    Author:
    Samuel, Nicklas
    Last modified
    $Date: 2015-12-04 08:47:16 +0100 (fr, 04 dec 2015) $
    • Constructor Detail

      • Restrictions

        public Restrictions()
    • Method Detail

      • and

        public static Restriction and​(Restriction... r)
                               throws InvalidDataException
        Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...
        Parameters:
        r - The restrictions to combine
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If any of the elements is null or the array doesn't contain at least one element
      • nullSafeAnd

        public static Restriction nullSafeAnd​(Restriction... r)
                                       throws InvalidDataException
        Combine one or more restrictions with AND ignoring null elements.
        Parameters:
        r - The restrictions to combine
        Returns:
        The new restriction, or null if the array was null or didn't contain any not-null elements
        Throws:
        InvalidDataException
        Since:
        2.15
      • nullSafeAnd

        public static Restriction nullSafeAnd​(Collection<? extends Restriction> restrictions)
                                       throws InvalidDataException
        Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...
        Parameters:
        restrictions - The restrictions to combine
        Returns:
        The new restriction, or null if the collection was null or didn't contain any not-null elements
        Throws:
        InvalidDataException
        Since:
        3.5
      • or

        public static Restriction or​(Restriction... r)
                              throws InvalidDataException
        Combine one or more restrictions with OR: new restriction = r[0] OR r[1] ...
        Parameters:
        r - The restrictions to combine
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If any of the elements is null or the array doesn't contain at least one element
      • nullSafeOr

        public static Restriction nullSafeOr​(Collection<? extends Restriction> restrictions)
                                      throws InvalidDataException
        Combine one or more restrictions with OR ignoring null elements.
        Parameters:
        restrictions - The restrictions to combine
        Returns:
        The new restriction, or null if the array was null or didn't contain any not-null elements
        Throws:
        InvalidDataException
        Since:
        3.5
      • nullSafeOr

        public static Restriction nullSafeOr​(Restriction... r)
                                      throws InvalidDataException
        Combine one or more restrictions with OR ignoring null elements.
        Parameters:
        r - The restrictions to combine
        Returns:
        The new restriction, or null if the array was null or didn't contain any not-null elements
        Throws:
        InvalidDataException
        Since:
        2.15
      • eq

        public static Restriction eq​(Expression e1,
                                     Expression e2)
                              throws InvalidDataException
        Compare if two expressions are equal: new restriction = e1 == e2. If one of the expressions are null, the IS NULL operation is used: new restriction = e1 IS NULL (or e2 IS NULL).
        Parameters:
        e1 - The left expression
        e2 - The right expression
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If both expressions are null
      • neq

        public static Restriction neq​(Expression e1,
                                      Expression e2)
                               throws InvalidDataException
        Compare if two expressions are inequal: new restriction = e1 <> e2. If one of the expressions are null, the NOT IS NULL operation is used: new restriction = NOT e1 IS NULL (or NOT e2 IS NULL).
        Parameters:
        e1 - The left expression
        e2 - The right expression
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If both expressions are null
      • between

        public static Restriction between​(Expression e,
                                          Expression low,
                                          Expression high)
                                   throws InvalidDataException
        Compare if one expression falls between two other expressions: new expression = low <= e AND e < high. Since the inclusion/exclusion of the low and high limits are dependent of the database BASE hase defined the between this way to make sure that BASE will treat BETWEEN the same way regardless of database.
        Parameters:
        e - The expression to check against the limits
        low - The expression for the low limit
        high - The expression for the high limit
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If any of the expressions are null
      • betweenInclusive

        public static Restriction betweenInclusive​(Expression e,
                                                   Expression low,
                                                   Expression high)
                                            throws InvalidDataException
        Compare if one expression falls between two other expressions (inclusive): new expression = low <= e AND e <= high. Since the inclusion/exclusion of the low and high limits are dependent of the database BASE hase defined the between this way to make sure that BASE will treat BETWEEN the same way regardless of database.
        Parameters:
        e - The expression to check against the limits
        low - The expression for the low limit
        high - The expression for the high limit
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If any of the expressions are null
        Since:
        3.0
      • in

        public static Restriction in​(Expression e1,
                                     Expression... e2)
                              throws InvalidDataException
        Check if an expression is contained in a set of other expressions: e1 IN (e2[0], e2[1], ...)
        Parameters:
        e1 - The expression to check
        e2 - The set of expression to check against
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If the set is null or empty
      • in

        public static Restriction in​(Expression e1,
                                     Query subquery)
        Create a subquery restriction: e1 IN (subquery). The expression is useful in the WHERE part of a query only. For example, to find all users that are members of a group having a name starting with A.
                Query groupFilter = User.getQuery();
                groupFilter.join(Hql.innerJoin("groups", "grp"));
                groupFilter.restrict(Restrictions.like(Hql.property("grp", "name"), Expressions.string("A%")));
                Query userQuery = User.getQuery();
                userQuery.restrict(Restrictions.in(Hql.property("id"), groupFilter));
                
        Parameters:
        e1 - The left expression
        subquery - The subquery
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If the subquery is null
        Since:
        3.5
      • like_in

        public static Restriction like_in​(Expression e1,
                                          Expression... e2)
        Check if one expression matches any expressions in an array: (e1 LIKE e2.1) OR (e1 LIKE e2.2) OR ....
        Parameters:
        e1 - The left expression
        e2 - The array with expression to match against, null values are allowed and compared with IS NULL instead
        Returns:
        The new restriction.
        Throws:
        InvalidDataException - If any of the expressions are null.
        Since:
        2.6
      • notLike_in

        public static Restriction notLike_in​(Expression e1,
                                             Expression... e2)
        Check if one expression doesn't matches any of the expressions in an array. (e1 NOT LIKE e2.1) AND (e1 NOT LIKE e2.2) AND...
        Parameters:
        e1 - The left expression. Null is not allowed
        e2 - The array with expressions that shouldn't match, null values are allows and compared with NOT IS NULL
        Returns:
        The new restriction
        Throws:
        InvalidDataException - If any of the required expressions are null.
        Since:
        2.6
      • rlike

        public static Restriction rlike​(Expression e,
                                        Expression regexp)
        Check if an expression matches a regular expression. The support for this is database dependent, but both MySQL and Postgres supports it. If the database doesn't support regular expression, this method will create a restriction for an exact match.
        Parameters:
        e - The expression to match
        regexp - The regular expression to test against
        Returns:
        A restriction
        Throws:
        InvalidDataException - If any of the values are null
        Since:
        2.8
      • conditionalRestriction

        public static Restriction conditionalRestriction​(Filter<? extends Query> filter,
                                                         Restriction ifTrue,
                                                         Restriction ifFalse,
                                                         boolean nullMatchNone)
        Create a restriction that falls back to one of two restrictions depending on if the query passes a filter or not. A typical use case is to apply a restriction depending on the return type of the query. This method will allow "null" restrictions but they are converted to "1=1" or "1=0" depending on the nullMatchNone parameter.
        Parameters:
        filter - A filter, if null the 'ifTrue' restriction is returned
        ifTrue - The restriction to apply if the filter evaluates to TRUE, or null to not apply any restriction in this case
        ifFalse - The restriction to apply if the filter evaluates to FALSE, or null to not apply any restriction in this case
        nullMatchNone - FALSE to match all items if the filter is null, TRUE to match no items if the filter is null
        Returns:
        A conditional restriction, or the 'ifTrue' restriction if the filter is null, or null if both restrictions are null
        Since:
        3.7
      • getFirstNull

        private static int getFirstNull​(Object[] array)
        Test if there is null in the array.
        Returns:
        The index of the first null element, or -1 if no null elements are found, or 0 if the array itself is null
      • countNotNulls

        private static int countNotNulls​(Object[] array)
      • countNotNulls

        private static int countNotNulls​(Collection<?> array)
      • copyNotNulls

        private static <T> T[] copyNotNulls​(T[] source,
                                            T[] dest)
      • copyNotNulls

        private static <T> T[] copyNotNulls​(Collection<? extends T> source,
                                            T[] dest)