Class QueryBuilder<T,​Q extends QueryBuilder<T,​Q>>

  • Type Parameters:
    T - type of object the predicates can evaluate in memory.
    Direct Known Subclasses:
    AccountQueryBuilder, ApprovalQueryBuilder, ChangeQueryBuilder, GroupQueryBuilder, ProjectQueryBuilder

    public abstract class QueryBuilder<T,​Q extends QueryBuilder<T,​Q>>
    extends Object
    Base class to support writing parsers for query languages.

    Subclasses may document their supported query operators by declaring public methods that perform the query conversion into a Predicate. For example, to support "is:starred", "is:unread", and nothing else, a subclass may write:

     @Operator
     public Predicate is(String value) {
       if ("starred".equals(value)) {
         return new StarredPredicate();
       }
       if ("unread".equals(value)) {
         return new UnreadPredicate();
       }
       throw new IllegalArgumentException();
     }
     

    The available operator methods are discovered at runtime via reflection. Method names (after being converted to lowercase), correspond to operators in the query language, method string values correspond to the operator argument. Methods must be declared public, returning Predicate, accepting one String, and annotated with the QueryBuilder.Operator annotation.

    Subclasses may also declare a handler for values which appear without operator by overriding defaultField(String).

    Instances are non-singletons and should only be used once, in order to rescan the DynamicMap of plugin-provided operators on each query invocation.

    • Method Detail

      • find

        public static <T,​P extends Predicate<T>> P find​(Predicate<T> p,
                                                              Class<P> clazz)
        Locate a predicate in the predicate tree.
        Parameters:
        p - the predicate to find.
        clazz - type of the predicate instance.
        Returns:
        the predicate, null if not found.
      • find

        public static <T,​P extends OperatorPredicate<T>> P find​(Predicate<T> p,
                                                                      Class<P> clazz,
                                                                      String name)
        Locate a predicate in the predicate tree.
        Parameters:
        p - the predicate to find.
        clazz - type of the predicate instance.
        name - name of the operator.
        Returns:
        the first instance of a predicate having the given type, as found by a depth-first search.
      • parse

        public Predicate<T> parse​(String query)
                           throws QueryParseException
        Parse a user-supplied query string into a predicate.
        Parameters:
        query - the query string.
        Returns:
        predicate representing the user query.
        Throws:
        QueryParseException - the query string is invalid and cannot be parsed by this parser. This may be due to a syntax error, may be due to an operator not being supported, or due to an invalid value being passed to a recognized operator.
      • setOperatorAliases

        public void setOperatorAliases​(Map<String,​String> opAliases)
      • parse

        public List<Predicate<T>> parse​(List<String> queries)
                                 throws QueryParseException
        Parse multiple user-supplied query strings into a list of predicates.
        Parameters:
        queries - the query strings.
        Returns:
        predicates representing the user query, in the same order as the input.
        Throws:
        QueryParseException - one of the query strings is invalid and cannot be parsed by this parser. This may be due to a syntax error, may be due to an operator not being supported, or due to an invalid value being passed to a recognized operator.
      • defaultField

        protected Predicate<T> defaultField​(String value)
                                     throws QueryParseException
        Handle a value present outside of an operator.

        This default implementation always throws an "Unsupported query: " message containing the input text. Subclasses may override this method to perform do-what-i-mean guesses based on the input string.

        Parameters:
        value - the value supplied by itself in the query.
        Returns:
        predicate representing this value.
        Throws:
        QueryParseException - the parser does not recognize this value.