Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / ListIndexesRequest.java
blob25de2ecdb851d97378394265d7ea04f0d1526057
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.ListIndexesRequestChecker;
7 import com.google.appengine.api.search.checkers.Preconditions;
9 /**
10 * A request to list indexes. You can specify a number of
11 * restrictions, such as the number of indexes to return, the prefix
12 * with which names of the returned indexes must begin, etc.
14 * A namespace may be specified, otherwise the default namespace will
15 * be used. Only the indexes defined in the namespace, default or
16 * otherwise, will be returned.
18 * <pre>
19 * ListIndexesRequest request = ListIndexesRequest().newBuilder()
20 * .setIndexNamePrefix("a")
21 * .setOffset(100)
22 * .setLimit(10)
23 * .build()
24 * </pre>
27 public final class ListIndexesRequest {
29 /**
30 * The builder of {@link ListIndexesRequest}s.
32 public static final class Builder {
33 private Integer offset; private String indexNamePrefix; private Boolean includeStartIndex; private String startIndexName; private Integer limit; private Boolean schemaFetched; private String namespace;
35 private Builder() {
36 includeStartIndex = true;
39 private Builder(ListIndexesRequest request) {
40 offset = request.getOffset();
41 indexNamePrefix = request.getIndexNamePrefix();
42 includeStartIndex = request.isIncludeStartIndex();
43 startIndexName = request.getStartIndexName();
44 limit = request.getLimit();
45 schemaFetched = request.isSchemaFetched();
46 namespace = request.getNamespace();
49 /**
50 * Sets the offset of the first index to return. This method comes with
51 * a performance penalty and if you just want to page through all indexes
52 * you should consider {@link #setStartIndexName(String)} method.
54 * @param offset the offset of the first returned index
55 * @return this builder
56 * @throws IllegalArgumentException if negative or too large offset is given
58 public Builder setOffset(Integer offset) {
59 this.offset = ListIndexesRequestChecker.checkOffset(offset);
60 return this;
63 /**
64 * Sets the prefix to be matched against the names of returned indexes.
65 * If the prefix is set to, say "a", only indexes with names starting with
66 * 'a' will be returned.
68 * @param indexNamePrefix the prefix used to select returned indexes
69 * @return this builder
70 * @throws IllegalArgumentException if invalid index name is given
72 public Builder setIndexNamePrefix(String indexNamePrefix) {
73 this.indexNamePrefix = ListIndexesRequestChecker.checkIndexNamePrefix(indexNamePrefix);
74 return this;
77 /**
78 * Sets whether or not to include the index whose name is specified via
79 * the {@link #setStartIndexName(String)} method.
81 * @param includeStartIndex whether or not to return the start index
82 * @return this builder
84 public Builder setIncludeStartIndex(boolean includeStartIndex) {
85 this.includeStartIndex = includeStartIndex;
86 return this;
89 /**
90 * Sets the name of the first index to return. You may exclude this index by
91 * using the {@link #setIncludeStartIndex(boolean)} method.
93 * @param startIndexName the name of the first index to be returned
94 * @return this builder
95 * @throws IllegalArgumentException if invalid start index name is given
97 public Builder setStartIndexName(String startIndexName) {
98 this.startIndexName = ListIndexesRequestChecker.checkStartIndexName(startIndexName);
99 return this;
103 * Sets the maximum number of indexes to return.
105 * @param limit the number of indexes to return
106 * @return this builder
107 * @throws IllegalArgumentException if negative or too large limit is given
109 public Builder setLimit(Integer limit) {
110 this.limit = ListIndexesRequestChecker.checkLimit(limit);
111 return this;
115 * Sets whether or not the schema is returned with indexes. An index schema
116 * is a map from field names to field types.
118 * @param schemaFetched whether or not schemas are present in returned indexes
119 * @return this builder
121 public Builder setSchemaFetched(boolean schemaFetched) {
122 this.schemaFetched = schemaFetched;
123 return this;
127 * Sets the namespace to use for this request. Only indexes
128 * defined within this namespace will be listed.
130 * @param namespace The namespace for this request.
131 * @return this builder
133 public Builder setNamespace(String namespace) {
134 this.namespace = namespace;
135 return this;
139 * @return builds and returns a brand new instance of
140 * a {@link ListIndexesRequest} using values set on this builder
142 public ListIndexesRequest build() {
143 return new ListIndexesRequest(this);
147 private final Integer offset;
148 private final String indexNamePrefix;
149 private final Boolean includeStartIndex;
150 private final String startIndexName;
151 private final Integer limit;
152 private final Boolean schemaFetched;
153 private final String namespace;
155 private ListIndexesRequest(Builder builder) {
156 offset = builder.offset;
157 indexNamePrefix = builder.indexNamePrefix;
158 startIndexName = builder.startIndexName;
159 includeStartIndex = (startIndexName == null)
160 ? null : Util.defaultIfNull(builder.includeStartIndex, Boolean.TRUE);
161 limit = Util.defaultIfNull(builder.limit, ListIndexesRequestChecker.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(ListIndexesRequest 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;
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 ListIndexesRequest 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 (includeStartIndex != null) {
247 builder.setIncludeStartIndex(includeStartIndex);
249 if (startIndexName != null) {
250 builder.setStartIndexName(startIndexName);
252 if (limit != null) {
253 builder.setLimit(limit);
255 if (namespace != null) {
256 builder.setNamespace(namespace);
258 return builder;
261 @Override
262 public int hashCode() {
263 final int prime = 31;
264 int result = 1;
265 result = prime * result + ((includeStartIndex == null) ? 0 : includeStartIndex.hashCode());
266 result = prime * result + ((indexNamePrefix == null) ? 0 : indexNamePrefix.hashCode());
267 result = prime * result + ((limit == null) ? 0 : limit.hashCode());
268 result = prime * result + ((offset == null) ? 0 : offset.hashCode());
269 result = prime * result + ((schemaFetched == null) ? 0 : schemaFetched.hashCode());
270 result = prime * result + ((startIndexName == null) ? 0 : startIndexName.hashCode());
271 return result;
274 @Override
275 public boolean equals(Object obj) {
276 if (this == obj) {
277 return true;
279 if (obj == null) {
280 return false;
282 if (getClass() != obj.getClass()) {
283 return false;
285 ListIndexesRequest other = (ListIndexesRequest) obj;
286 return Util.equalObjects(includeStartIndex, other.includeStartIndex)
287 && Util.equalObjects(indexNamePrefix, other.indexNamePrefix)
288 && Util.equalObjects(limit, other.limit)
289 && Util.equalObjects(offset, other.offset)
290 && Util.equalObjects(schemaFetched, other.schemaFetched)
291 && Util.equalObjects(startIndexName, other.startIndexName);
294 @Override
295 public String toString() {
296 return "ListIndexesRequest(offset=" + offset + ", indexNamePrefix=" + indexNamePrefix
297 + ", includeStartIndex=" + includeStartIndex + ", startIndexName=" + startIndexName
298 + ", limit=" + limit + ", schemaFetched=" + schemaFetched
299 + ")";