T
- type of object the predicates can evaluate in memory.public abstract class QueryBuilder<T> extends Object
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(final 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)
.
Modifier and Type | Class and Description |
---|---|
static class |
QueryBuilder.Definition<T,Q extends QueryBuilder<T>>
Defines the operators known by a QueryBuilder.
|
protected static interface |
QueryBuilder.Operator
Denotes a method which is a query operator.
|
static interface |
QueryBuilder.OperatorFactory<T,Q extends QueryBuilder<T>>
Converts a value string passed to an operator into a
Predicate . |
Modifier and Type | Field and Description |
---|---|
protected QueryBuilder.Definition<T,? extends QueryBuilder<T>> |
builderDef |
protected Map<String,QueryBuilder.OperatorFactory> |
opFactories |
Modifier | Constructor and Description |
---|---|
protected |
QueryBuilder(QueryBuilder.Definition<T,? extends QueryBuilder<T>> def) |
Modifier and Type | Method and Description |
---|---|
protected Predicate<T> |
defaultField(String value)
Handle a value present outside of an operator.
|
protected static com.google.gerrit.server.query.QueryParseException |
error(String msg) |
protected static com.google.gerrit.server.query.QueryParseException |
error(String msg,
Throwable why) |
static <T,P extends Predicate<T>> |
find(Predicate<T> p,
Class<P> clazz)
Locate a predicate in the predicate tree.
|
static <T,P extends OperatorPredicate<T>> |
find(Predicate<T> p,
Class<P> clazz,
String name)
Locate a predicate in the predicate tree.
|
List<Predicate<T>> |
parse(List<String> queries)
Parse multiple user-supplied query strings into a list of predicates.
|
Predicate<T> |
parse(String query)
Parse a user-supplied query string into a predicate.
|
protected final QueryBuilder.Definition<T,? extends QueryBuilder<T>> builderDef
protected final Map<String,QueryBuilder.OperatorFactory> opFactories
protected QueryBuilder(QueryBuilder.Definition<T,? extends QueryBuilder<T>> def)
public static <T,P extends Predicate<T>> P find(Predicate<T> p, Class<P> clazz)
p
- the predicate to find.clazz
- type of the predicate instance.public static <T,P extends OperatorPredicate<T>> P find(Predicate<T> p, Class<P> clazz, String name)
p
- the predicate to find.clazz
- type of the predicate instance.name
- name of the operator.public Predicate<T> parse(String query) throws com.google.gerrit.server.query.QueryParseException
query
- the query string.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.public List<Predicate<T>> parse(List<String> queries) throws com.google.gerrit.server.query.QueryParseException
queries
- the query strings.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.protected Predicate<T> defaultField(String value) throws com.google.gerrit.server.query.QueryParseException
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.
value
- the value supplied by itself in the query.QueryParseException
- the parser does not recognize this value.protected static com.google.gerrit.server.query.QueryParseException error(String msg)