Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / query / QueryTreeContext.java
blob824850a3adba67e9e9732fa61655d6184572f7aa
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;
7 import java.util.List;
8 import java.util.Set;
10 /**
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
14 * information.
17 * @param <T> the actual class used by specific tree visitors
19 public abstract class QueryTreeContext<T extends QueryTreeContext<T>> {
21 /**
22 * Enumeration of supported return types.
24 public enum Type {
25 BOOL, TEXT, NUMBER, DATE, LOCATION;
28 /**
29 * Enumeration of text terms rewrite mode.
31 public enum RewriteMode {
32 STRICT, FUZZY
35 /**
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 user types "..." as the query term.
41 public enum Kind {
42 VOID, LITERAL, PHRASE, FIELD, FUNCTION, EXPRESSION
45 private final List<T> children;
46 private RewriteMode rewriteMode;
47 private Set<Type> returnTypes;
48 private Kind kind;
49 private String text;
51 protected QueryTreeContext() {
52 children = new ArrayList<T>();
53 rewriteMode = null;
54 returnTypes = EnumSet.noneOf(Type.class);
55 kind = Kind.VOID;
58 /**
59 * @return a child context for this context
61 public T addChild() {
62 T childContext = newChildContext();
63 children.add(childContext);
64 return childContext;
67 /**
68 * @return a new child of type T
70 protected abstract T newChildContext();
72 /**
73 * @return iterable over all children contexts
75 public Iterable<T> children() {
76 return children;
79 /**
80 * @return the number of children contexts
82 public int getChildCount() {
83 return children.size();
86 /**
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);
94 /**
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());
129 return common;
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) {
138 return false;
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
161 * not be rewritten
163 public boolean isStrict() {
164 return RewriteMode.STRICT.equals(rewriteMode);
168 * @param kind the kind of the
170 public void setKind(Kind kind) {
171 this.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 returns if this context represents a field of a document
191 public boolean isField() {
192 return Kind.FIELD.equals(kind);
196 * @return sets the text associated with this context
198 public String getText() {
199 return text;
203 * @param text returns the text associated with this context
205 public void setText(String text) {
206 this.text = text;
209 @Override
210 public String toString() {
211 return text;