Version 1.7.4
[gae.git] / java / src / main / com / google / appengine / api / search / GetIndexesRequest.java
blob4a810496dcf1d8f4813132cfa8fce82cd2da87d8
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search;
5 import com.google.appengine.api.search.SearchServicePb.ListIndexesParams;
6 import com.google.appengine.api.search.checkers.GetIndexesRequestChecker;
7 import com.google.appengine.api.search.checkers.SearchApiLimits;
8 import com.google.appengine.api.search.checkers.Preconditions;
10 /**
11 * A request to get a range of indexes. You can specify a number of
12 * restrictions, such as the number of indexes to return, the prefix
13 * with which names of the returned indexes must begin, etc.
15 * A namespace may be specified, otherwise the default namespace will
16 * be used. Only the indexes defined in the namespace, default or
17 * otherwise, will be returned.
19 * <pre>
20 * GetIndexesRequest request = GetIndexesRequest().newBuilder()
21 * .setIndexNamePrefix("a")
22 * .setOffset(100)
23 * .setLimit(10)
24 * .build();
25 * </pre>
28 public final class GetIndexesRequest {
30 /**
31 * The builder of {@link GetIndexesRequest}s.
33 public static final class Builder {
34 private Integer offset; private String indexNamePrefix; private Boolean includeStartIndex; private String startIndexName; private Integer limit; private Boolean schemaFetched; private String namespace;
36 private Builder() {
37 includeStartIndex = true;
40 private Builder(GetIndexesRequest request) {
41 offset = request.getOffset();
42 indexNamePrefix = request.getIndexNamePrefix();
43 includeStartIndex = request.isIncludeStartIndex();
44 startIndexName = request.getStartIndexName();
45 limit = request.getLimit();
46 schemaFetched = request.isSchemaFetched();
47 namespace = request.getNamespace();
50 /**
51 * Sets the offset of the first index to return. This method comes with
52 * a performance penalty and if you just want to page through all indexes
53 * you should consider {@link #setStartIndexName(String)} method.
55 * @param offset the offset of the first returned index
56 * @return this builder
57 * @throws IllegalArgumentException if negative or too large offset is given
59 public Builder setOffset(Integer offset) {
60 this.offset = GetIndexesRequestChecker.checkOffset(offset);
61 return this;
64 /**
65 * Sets the prefix to be matched against the names of returned indexes.
66 * If the prefix is set to, say "a", only indexes with names starting with
67 * 'a' will be returned.
69 * @param indexNamePrefix the prefix used to select returned indexes
70 * @return this builder
71 * @throws IllegalArgumentException if invalid index name is given
73 public Builder setIndexNamePrefix(String indexNamePrefix) {
74 this.indexNamePrefix = GetIndexesRequestChecker.checkIndexNamePrefix(indexNamePrefix);
75 return this;
78 /**
79 * Sets whether or not to include the index whose name is specified via
80 * the {@link #setStartIndexName(String)} method.
82 * @param includeStartIndex whether or not to return the start index
83 * @return this builder
85 public Builder setIncludeStartIndex(boolean includeStartIndex) {
86 this.includeStartIndex = includeStartIndex;
87 return this;
90 /**
91 * Sets the name of the first index to return. You may exclude this index by
92 * using the {@link #setIncludeStartIndex(boolean)} method.
94 * @param startIndexName the name of the first index to be returned
95 * @return this builder
96 * @throws IllegalArgumentException if invalid start index name is given
98 public Builder setStartIndexName(String startIndexName) {
99 this.startIndexName = GetIndexesRequestChecker.checkStartIndexName(startIndexName);
100 return this;
104 * Sets the maximum number of indexes to return.
106 * @param limit the number of indexes to return
107 * @return this builder
108 * @throws IllegalArgumentException if negative or too large limit is given
110 public Builder setLimit(Integer limit) {
111 this.limit = GetIndexesRequestChecker.checkLimit(limit);
112 return this;
116 * Sets whether or not the schema is returned with indexes. An index schema
117 * is a map from field names to field types.
119 * @param schemaFetched whether or not schemas are present in returned indexes
120 * @return this builder
122 public Builder setSchemaFetched(boolean schemaFetched) {
123 this.schemaFetched = schemaFetched;
124 return this;
128 * Sets the namespace to use for this request. Only indexes
129 * defined within this namespace will be fetched.
131 * @param namespace The namespace for this request.
132 * @return this builder
134 public Builder setNamespace(String namespace) {
135 this.namespace = namespace;
136 return this;
140 * @return builds and returns a brand new instance of
141 * a {@link GetIndexesRequest} using values set on this builder
143 public GetIndexesRequest build() {
144 return new GetIndexesRequest(this);
148 private final Integer offset;
149 private final String indexNamePrefix;
150 private final Boolean includeStartIndex;
151 private final String startIndexName;
152 private final Integer limit;
153 private final Boolean schemaFetched;
154 private final String namespace;
156 private GetIndexesRequest(Builder builder) {
157 offset = builder.offset;
158 indexNamePrefix = builder.indexNamePrefix;
159 startIndexName = builder.startIndexName;
160 includeStartIndex = (startIndexName == null)
161 ? null : Util.defaultIfNull(builder.includeStartIndex, Boolean.TRUE);
162 limit = Util.defaultIfNull(builder.limit, SearchApiLimits.SEARCH_DEFAULT_LIMIT);
163 schemaFetched = builder.schemaFetched;
164 namespace = builder.namespace;
165 checkValid();
168 public static final Builder newBuilder() {
169 return new Builder();
172 public static final Builder newBuilder(GetIndexesRequest request) {
173 return new Builder(request);
177 * @return the offset of the first returned index
179 public Integer getOffset() {
180 return offset;
184 * @return the prefix matching names of all returned indexes
186 public String getIndexNamePrefix() {
187 return indexNamePrefix;
191 * @return whether or not the index with the start index name is returned
193 public boolean isIncludeStartIndex() {
194 return includeStartIndex;
198 * @return the name of the first index to be returned
200 public String getStartIndexName() {
201 return startIndexName;
205 * @return the maximum number of indexes returned by this request
207 public Integer getLimit() {
208 return limit;
212 * @return whether or not index schema is returned with each index
214 public Boolean isSchemaFetched() {
215 return schemaFetched;
219 * @return the namespace for this request, or null for the default
220 * namespace.
222 public String getNamespace() {
223 return namespace;
226 private GetIndexesRequest checkValid() {
227 if (limit != null) {
228 Preconditions.checkArgument(limit > 0, "Limit must be positive");
230 if (offset != null) {
231 Preconditions.checkArgument(offset >= 0, "Offset must be non-negative");
233 return this;
236 ListIndexesParams.Builder copyToProtocolBuffer() {
237 ListIndexesParams.Builder builder = ListIndexesParams.newBuilder();
238 if (schemaFetched != null) {
239 builder.setFetchSchema(schemaFetched);
241 if (offset != null) {
242 builder.setOffset(offset);
244 if (indexNamePrefix != null) {
245 builder.setIndexNamePrefix(indexNamePrefix);
247 if (includeStartIndex != null) {
248 builder.setIncludeStartIndex(includeStartIndex);
250 if (startIndexName != null) {
251 builder.setStartIndexName(startIndexName);
253 if (limit != null) {
254 builder.setLimit(limit);
256 if (namespace != null) {
257 builder.setNamespace(namespace);
259 return builder;
262 @Override
263 public int hashCode() {
264 final int prime = 31;
265 int result = 1;
266 result = prime * result + ((includeStartIndex == null) ? 0 : includeStartIndex.hashCode());
267 result = prime * result + ((indexNamePrefix == null) ? 0 : indexNamePrefix.hashCode());
268 result = prime * result + ((limit == null) ? 0 : limit.hashCode());
269 result = prime * result + ((offset == null) ? 0 : offset.hashCode());
270 result = prime * result + ((schemaFetched == null) ? 0 : schemaFetched.hashCode());
271 result = prime * result + ((startIndexName == null) ? 0 : startIndexName.hashCode());
272 return result;
275 @Override
276 public boolean equals(Object obj) {
277 if (this == obj) {
278 return true;
280 if (obj == null) {
281 return false;
283 if (getClass() != obj.getClass()) {
284 return false;
286 GetIndexesRequest other = (GetIndexesRequest) obj;
287 return Util.equalObjects(includeStartIndex, other.includeStartIndex)
288 && Util.equalObjects(indexNamePrefix, other.indexNamePrefix)
289 && Util.equalObjects(limit, other.limit)
290 && Util.equalObjects(offset, other.offset)
291 && Util.equalObjects(schemaFetched, other.schemaFetched)
292 && Util.equalObjects(startIndexName, other.startIndexName);
295 @Override
296 public String toString() {
297 return "GetIndexesRequest(offset=" + offset + ", indexNamePrefix=" + indexNamePrefix
298 + ", includeStartIndex=" + includeStartIndex + ", startIndexName=" + startIndexName
299 + ", limit=" + limit + ", schemaFetched=" + schemaFetched
300 + ")";