1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.query
;
5 import java
.util
.ArrayList
;
6 import java
.util
.EnumSet
;
11 * The base class for specific query tree context used by the walker.
12 * This class is used to maintain additional information gathered while
13 * walking the tree. On this level it is used to collect return type
17 * @param <T> the actual class used by specific tree visitors
19 public abstract class QueryTreeContext
<T
extends QueryTreeContext
<T
>> {
22 * Enumeration of supported return types.
25 BOOL
, TEXT
, NUMBER
, DATE
, LOCATION
, DISTANCE
;
29 * Enumeration of text terms rewrite mode.
31 public enum RewriteMode
{
36 * Enumeration of the kind of the term that has a given return
37 * type. This enum makes more precise the return type. For example,
38 * if the return type is TEXT, and the Kind is PHRASE, this means
39 * that the caller supplied "..." as the query term.
42 VOID
, LITERAL
, PHRASE
, FIELD
, FUNCTION
, EXPRESSION
45 private final List
<T
> children
;
46 private RewriteMode rewriteMode
;
47 private Set
<Type
> returnTypes
;
51 protected QueryTreeContext() {
52 children
= new ArrayList
<T
>();
54 returnTypes
= EnumSet
.noneOf(Type
.class);
59 * @return a child context for this context
62 T childContext
= newChildContext();
63 children
.add(childContext
);
68 * @return a new child of type T
70 protected abstract T
newChildContext();
73 * @return iterable over all children contexts
75 public Iterable
<T
> children() {
80 * @return the number of children contexts
82 public int getChildCount() {
83 return children
.size();
87 * @param index the index of the child to get
88 * @return the child context at the given index
90 public T
getChild(int index
) {
91 return children
.get(index
);
95 * @param type additional type to be added to current return types
97 public void addReturnType(Type type
) {
98 returnTypes
.add(type
);
102 * @param type the unique return type for this context
104 public void setReturnType(Type type
) {
105 returnTypes
= EnumSet
.of(type
);
109 * @param type a set of types to be set as the only return types
111 public void setReturnTypes(Set
<Type
> type
) {
112 returnTypes
= EnumSet
.copyOf(type
);
116 * @return the set of return types
118 protected Set
<Type
> getReturnTypes() {
119 return EnumSet
.copyOf(returnTypes
);
123 * @param other the other context whose types are to be inspected
124 * @return a set of types common to this and the other context
126 public Set
<Type
> getCommonReturnTypes(T other
) {
127 Set
<Type
> common
= getReturnTypes();
128 common
.retainAll(other
.getReturnTypes());
133 * @param returnType the type to be checked against types of this context
134 * @return whether or not it is compatible with at least one type
136 public boolean isCompatibleWith(Type returnType
) {
137 if (returnType
== null) {
140 Set
<Type
> set
= EnumSet
.of(returnType
);
141 set
.retainAll(returnTypes
);
142 return !set
.isEmpty();
146 * @param mode the rewrite mode for the value represented by this context
148 public void setRewriteMode(RewriteMode mode
) {
149 this.rewriteMode
= mode
;
153 * @return whether or not the value associated with this context is rewritable
155 public boolean isFuzzy() {
156 return RewriteMode
.FUZZY
.equals(rewriteMode
);
160 * @return whether or not the value associated with this context must
163 public boolean isStrict() {
164 return RewriteMode
.STRICT
.equals(rewriteMode
);
168 * @param kind the kind of the
170 public void setKind(Kind kind
) {
175 * @return whether or not this context represents a phrase (quoted text)
177 public boolean isPhrase() {
178 return Kind
.PHRASE
.equals(kind
);
182 * @return if this context represents a function
184 public boolean isFunction() {
185 return Kind
.FUNCTION
.equals(kind
);
189 * @return if this context represents a literal constant
191 public boolean isLiteral() {
192 return Kind
.LITERAL
.equals(kind
);
196 * @return returns if this context represents a field of a document
198 public boolean isField() {
199 return Kind
.FIELD
.equals(kind
);
203 * @return sets the text associated with this context
205 public String
getText() {
210 * @param text returns the text associated with this context
212 public void setText(String text
) {
217 public String
toString() {