Class ConstraintHelper


  • public final class ConstraintHelper
    extends java.lang.Object
    Static methods to create Lists of Constraint objects in a query and Constraints in a query relating to a given QueryClass, plus helper methods for dealing with Constraints.
    'left' and 'right' arguments for each constraint type are defined as follows: SimpleConstraint: left = arg1, right = arg2 (both QueryEvaluables) ClassConstraint: left = QueryClass arg1, right = a QueryClass or example object arg2 ContainsConstraint: left = the QueryReference - i.e. field of containing class right = the QueryClass this reference is constrained to SubQueryConstraint: left = the QueryClass or QueryEvaluable constrained to be in the subquery right = the query ConstraintSet: N/A
    Author:
    Matthew Wakeling, Richard Smith
    • Method Detail

      • createList

        public static java.util.List<Constraint> createList​(Query query)
        Converts a constraint from a query into a List of Constraint objects.
        Parameters:
        query - a Query object to list Constraints for
        Returns:
        a List of Constraint objects
      • createList

        public static java.util.List<Constraint> createList​(Query query,
                                                            FromElement fromElement)
        Return a List of Constraint objects that relate to the given FromElement.
        Parameters:
        query - a Query object to to list contraints for
        fromElement - a FromElement that returned constraints relate to
        Returns:
        a List of Constraint objects
      • traverseConstraints

        public static void traverseConstraints​(Constraint c,
                                               ConstraintTraverseAction ca)
        Traverse the given Constraint tree (ie. recursively look for nested constraints if the argument is a ConstraintSet) and call ConstraintTraverseAction for each Constraint.
        Parameters:
        c - the Constraint to traverse - could be a ConstraintSet (possibly with nested ConstraintSets)
        ca - ConstraintTraverseAction.apply() is called for each Constraint found
      • listRelatedConstraints

        public static java.util.List<Constraint> listRelatedConstraints​(Query query,
                                                                        FromElement fromElement)
        Return a List of Constraint objects that relate to the given FromElement.
        Parameters:
        query - a Query object to to list contraints for
        fromElement - a FromElement that returned constraints relate to
        Returns:
        a List of Constraint objects
      • filter

        public static java.util.List<Constraint> filter​(java.util.List<Constraint> list,
                                                        FromElement fromElement,
                                                        boolean related)
        Return a subset of the given List that contains only Constraints that relate to the given FromElement or constraints associated with nothing if fromElement is null.
        Parameters:
        list - a list of Constraints to filter
        fromElement - a fromElement that returned constraints relate to
        related - if tru list all releted constraints, otherwise just associated
        Returns:
        a List of Constraint objects
      • addToList

        public static void addToList​(java.util.List<Constraint> list,
                                     Constraint constraint)
        Adds all the constraints present in the argument into the given List.
        Parameters:
        list - a List of Constraints, to which to add more entries
        constraint - a Constraint to pick apart
      • isAssociatedWith

        public static boolean isAssociatedWith​(Constraint constraint,
                                               FromElement fromElement)
        Returns true if the constraint is associated with the given FromElement. Associated with means relating directly to a field of the QueryClass but NOT a cross-reference contraint (which compares two arbitrary fields of different QueryClasses.
        Parameters:
        constraint - the constraint in question
        fromElement - the FromElement to check
        Returns:
        true if associated
      • isAssociatedWithNothing

        public static boolean isAssociatedWithNothing​(Constraint c)
        Returns true if the given constraint is associated with no particular FromElement.
        Parameters:
        c - the constraint to examine
        Returns:
        true if constraint is not associated with a FromElement
      • isRelatedTo

        public static boolean isRelatedTo​(Constraint constraint,
                                          FromElement fromElement)
        Returns true if the constraint is associated with the given FromElement.
        Parameters:
        constraint - the constraint in question
        fromElement - the FromElement to check
        Returns:
        true if associated
      • isRelatedToNothing

        public static boolean isRelatedToNothing​(Constraint c)
        Returns true if the given constraint is related to no FromElement. This should only return true if c is a SimpleConstraint that only references constants.
        Parameters:
        c - the constraint to examine
        Returns:
        true if constraint is not associated with a FromElement
      • isCrossReference

        public static boolean isCrossReference​(Constraint constraint)
        Returns true if the Constraint is a cross-reference between two QueryClasses. A constraint is deemed to be a cross-reference if it compares fields of two different QueryClasses, either directly or via QueryExpressions.
        Parameters:
        constraint - the contraint to test
        Returns:
        true if the contraint is a cross-reference
      • getQueryFields

        protected static java.util.Set<QueryField> getQueryFields​(QueryEvaluable qe)
        Descends into QueryExpression and QueryFunction objects to find all instances of QueryField. Will return a single element set for QueryField and an empty set for QueryValue.
        Parameters:
        qe - a QueryEvalubale to find QueryFields for
        Returns:
        a set of QueryFields
      • getLeftArgument

        public static java.lang.Object getLeftArgument​(Constraint constraint)
        Get the left argument of the given constraint, will return null if passed a ConstraintSet or a MultipleInBagConstraint.
        Parameters:
        constraint - a constraint
        Returns:
        the left argument of given constraint
      • getRightArgument

        public static java.lang.Object getRightArgument​(Constraint constraint)
        Get the right argument of the given constraint, will return null if passed a ConstraintSet or a MultipleInBagConstraint.
        Parameters:
        constraint - a constraint
        Returns:
        the right argument of given constraint