Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / GetIndexesRequest.java
blob983506b9eed39f95a6fec9292602e963283ab37b
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.Preconditions;
8 import com.google.appengine.api.search.checkers.SearchApiLimits;
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 = Util.defaultIfNull(builder.includeStartIndex, Boolean.TRUE);
161 limit = Util.defaultIfNull(builder.limit, SearchApiLimits.SEARCH_DEFAULT_LIMIT);
162 schemaFetched = builder.schemaFetched;
163 namespace = builder.namespace;
164 checkValid();
167 public static final Builder newBuilder() {
168 return new Builder();
171 public static final Builder newBuilder(GetIndexesRequest request) {
172 return new Builder(request);
176 * @return the offset of the first returned index
178 public Integer getOffset() {
179 return offset;
183 * @return the prefix matching names of all returned indexes
185 public String getIndexNamePrefix() {
186 return indexNamePrefix;
190 * @return whether or not the index with the start index name is returned
192 public boolean isIncludeStartIndex() {
193 return includeStartIndex == null ? true : includeStartIndex;
197 * @return the name of the first index to be returned
199 public String getStartIndexName() {
200 return startIndexName;
204 * @return the maximum number of indexes returned by this request
206 public Integer getLimit() {
207 return limit;
211 * @return whether or not index schema is returned with each index
213 public Boolean isSchemaFetched() {
214 return schemaFetched;
218 * @return the namespace for this request, or null for the default
219 * namespace.
221 public String getNamespace() {
222 return namespace;
225 private GetIndexesRequest checkValid() {
226 if (limit != null) {
227 Preconditions.checkArgument(limit > 0, "Limit must be positive");
229 if (offset != null) {
230 Preconditions.checkArgument(offset >= 0, "Offset must be non-negative");
232 return this;
235 ListIndexesParams.Builder copyToProtocolBuffer() {
236 ListIndexesParams.Builder builder = ListIndexesParams.newBuilder();
237 if (schemaFetched != null) {
238 builder.setFetchSchema(schemaFetched);
240 if (offset != null) {
241 builder.setOffset(offset);
243 if (indexNamePrefix != null) {
244 builder.setIndexNamePrefix(indexNamePrefix);
246 if (startIndexName != null) {
247 builder.setStartIndexName(startIndexName);
248 builder.setIncludeStartIndex(includeStartIndex);
250 if (limit != null) {
251 builder.setLimit(limit);
253 if (namespace != null) {
254 builder.setNamespace(namespace);
256 return builder;
259 @Override
260 public int hashCode() {
261 final int prime = 31;
262 int result = 1;
263 result = prime * result + ((includeStartIndex == null) ? 0 : includeStartIndex.hashCode());
264 result = prime * result + ((indexNamePrefix == null) ? 0 : indexNamePrefix.hashCode());
265 result = prime * result + ((limit == null) ? 0 : limit.hashCode());
266 result = prime * result + ((offset == null) ? 0 : offset.hashCode());
267 result = prime * result + ((schemaFetched == null) ? 0 : schemaFetched.hashCode());
268 result = prime * result + ((startIndexName == null) ? 0 : startIndexName.hashCode());
269 return result;
272 @Override
273 public boolean equals(Object obj) {
274 if (this == obj) {
275 return true;
277 if (obj == null) {
278 return false;
280 if (getClass() != obj.getClass()) {
281 return false;
283 GetIndexesRequest other = (GetIndexesRequest) obj;
284 return Util.equalObjects(includeStartIndex, other.includeStartIndex)
285 && Util.equalObjects(indexNamePrefix, other.indexNamePrefix)
286 && Util.equalObjects(limit, other.limit)
287 && Util.equalObjects(offset, other.offset)
288 && Util.equalObjects(schemaFetched, other.schemaFetched)
289 && Util.equalObjects(startIndexName, other.startIndexName);
292 @Override
293 public String toString() {
294 return "GetIndexesRequest(offset=" + offset + ", indexNamePrefix=" + indexNamePrefix
295 + ", includeStartIndex=" + includeStartIndex + ", startIndexName=" + startIndexName
296 + ", limit=" + limit + ", schemaFetched=" + schemaFetched
297 + ")";