Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / FetchOptions.java
blob211a7f4fe1d9bce645c7c7567ebde24e73503035
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 package com.google.appengine.api.datastore;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
8 /**
9 * Describes the limit, offset, and chunk size to be applied when
10 * executing a {@link PreparedQuery}.
11 * <p>
12 * {@code limit} is the maximum number of results the query will return.
13 * <p>
14 * {@code offset} is the number of results to skip before returning any
15 * results. Results that are skipped due to offset do not count against
16 * {@code limit}.
17 * <p>
18 * {@code startCursor} and {@code endCursor} are previously generated cursors
19 * that point to locations in a result set. If specified queries will start
20 * and end at these locations.
21 * <p>
22 * {@code prefetchSize} is the number of results retrieved on the first call
23 * to the datastore.
24 * <p>
25 * {@code chunkSize} determines the internal chunking strategy of the
26 * {@link Iterator} returned by {@link PreparedQuery#asIterator(FetchOptions)}
27 * and the {@link Iterable} returned by
28 * {@link PreparedQuery#asIterable(FetchOptions)}.
29 * <p>
30 * Note that unlike {@code limit}, {@code offset} and {@code cursor},
31 * {@code prefetchSize} and {@code chunkSize} have no impact on the result of
32 * the {@link PreparedQuery}, but rather only the performance of the
33 * {@link PreparedQuery}.
34 * <p>
35 * Notes on usage:<br>
36 * The recommended way to instantiate a {@code FetchOptions} object is to
37 * statically import {@link Builder}.* and invoke a static
38 * creation method followed by an instance mutator (if needed):
40 * <pre>
41 * import static com.google.appengine.api.datastore.FetchOptions.Builder.*;
43 * ...
45 * // limit 10
46 * datastoreService.prepare(query).asList(withLimit(10));
48 * // limit 10, offset 5
49 * datastoreService.prepare(query).asList(withLimit(10).offset(5));
50 * </pre>
53 public final class FetchOptions {
55 /** @deprecated Instead of using DEFAULT_CHUNK_SIZE, do not specify a chunk size. */
56 @Deprecated
57 public static final int DEFAULT_CHUNK_SIZE = 20;
59 private Integer limit;
60 private Integer offset;
61 private Integer prefetchSize;
62 private Integer chunkSize;
63 private Cursor startCursor;
64 private Cursor endCursor;
65 private Boolean compile;
67 private FetchOptions() {
70 FetchOptions(FetchOptions original) {
71 this.limit = original.limit;
72 this.offset = original.offset;
73 this.prefetchSize = original.prefetchSize;
74 this.chunkSize = original.chunkSize;
75 this.startCursor = original.startCursor;
76 this.endCursor = original.endCursor;
77 this.compile = original.compile;
80 /**
81 * Sets the limit. Please read the class javadoc for an explanation of how
82 * limit is used.
83 * @param limit The limit to set. Must be non-negative.
84 * @return {@code this} (for chaining)
86 public FetchOptions limit(int limit) {
87 if (limit < 0) {
88 throw new IllegalArgumentException("Limit must be non-negative.");
90 this.limit = limit;
91 return this;
94 FetchOptions clearLimit() {
95 limit = null;
96 return this;
99 /**
100 * Sets the offset. Please read the class javadoc for an explanation of how
101 * offset is used.
102 * @param offset The offset to set. Must be 0 or greater.
103 * @return {@code this} (for chaining)
105 public FetchOptions offset(int offset) {
106 if (offset < 0) {
107 throw new IllegalArgumentException("Offset must be 0 or greater.");
109 this.offset = offset;
110 return this;
113 FetchOptions clearOffset() {
114 offset = null;
115 return this;
119 * Sets the chunk size. Please read the class javadoc for an explanation of
120 * how chunk size is used.
121 * @param chunkSize The chunk size to set. Must be greater than 0.
122 * @return {@code this} (for chaining)
124 public FetchOptions chunkSize(int chunkSize) {
125 if (chunkSize < 1) {
126 throw new IllegalArgumentException("Chunk size must be greater than 0.");
128 this.chunkSize = chunkSize;
129 return this;
132 FetchOptions clearChunkSize() {
133 chunkSize = null;
134 return this;
138 * Sets the number of entities to prefetch.
139 * @param prefetchSize The prefetch size to set. Must be >= 0.
140 * @return {@code this} (for chaining)
142 public FetchOptions prefetchSize(int prefetchSize) {
143 if (prefetchSize < 0) {
144 throw new IllegalArgumentException("Prefetch size must be 0 or greater.");
146 this.prefetchSize = prefetchSize;
147 return this;
150 FetchOptions clearPrefetchSize() {
151 prefetchSize = null;
152 return this;
156 * Sets the cursor to start the query from.
157 * @param cursor the cursor to set
158 * @return {@code this} (for chaining)
159 * @deprecated use {@link #startCursor} instead.
161 @Deprecated
162 public FetchOptions cursor(Cursor cursor) {
163 return startCursor(cursor);
167 * Sets the cursor at which to start the query.
169 * @param startCursor the cursor to set
170 * @return {@code this} (for chaining)
172 public FetchOptions startCursor(Cursor startCursor) {
173 if (startCursor == null) {
174 throw new NullPointerException("start cursor cannot be null.");
176 this.startCursor = startCursor;
177 return this;
181 * Sets the cursor at which to end the query.
183 * @param endCursor the cursor to set
184 * @return {@code this} (for chaining)
186 public FetchOptions endCursor(Cursor endCursor) {
187 if (endCursor == null) {
188 throw new NullPointerException("end cursor cannot be null.");
190 this.endCursor = endCursor;
191 return this;
194 FetchOptions clearStartCursor() {
195 startCursor = null;
196 return this;
199 FetchOptions clearEndCursor() {
200 endCursor = null;
201 return this;
204 FetchOptions compile(boolean compile) {
205 this.compile = compile;
206 return this;
209 FetchOptions clearCompile() {
210 compile = null;
211 return this;
215 * @return The limit, or {@code null} if no limit was provided.
217 public Integer getLimit() {
218 return limit;
222 * @return The offset, or {@code null} if no offset was provided.
224 public Integer getOffset() {
225 return offset;
229 * @return The chunk size, or {@code null} if no chunk size was provided.
231 public Integer getChunkSize() {
232 return chunkSize;
236 * @return The prefetch size, or {@code null} if no prefetch size was
237 * provided.
239 public Integer getPrefetchSize() {
240 return prefetchSize;
244 * @return The start cursor, or {@code null} if no cursor was provided.
245 * @deprecated use {@link #getStartCursor()} instead
247 @Deprecated
248 public Cursor getCursor() {
249 return getStartCursor();
253 * @return The start cursor, or {@code null} if no start cursor was provided.
255 public Cursor getStartCursor() {
256 return startCursor;
260 * @return The end cursor, or {@code null} if no end cursor was provided.
262 public Cursor getEndCursor() {
263 return endCursor;
266 Boolean getCompile() {
267 return compile;
270 @Override
271 public int hashCode() {
272 int result = 0;
274 if (prefetchSize != null) {
275 result = result * 31 + prefetchSize.hashCode();
278 if (chunkSize != null) {
279 result = result * 31 + chunkSize.hashCode();
282 if (limit != null) {
283 result = result * 31 + limit.hashCode();
286 if (offset != null) {
287 result = result * 31 + offset.hashCode();
290 if (startCursor != null) {
291 result = result * 31 + startCursor.hashCode();
294 if (endCursor != null) {
295 result = result * 31 + endCursor.hashCode();
298 if (compile != null) {
299 result = result * 31 + compile.hashCode();
302 return result;
305 @Override
306 public boolean equals(Object obj) {
307 if (obj == null) {
308 return false;
311 if (obj.getClass() != this.getClass()) {
312 return false;
315 FetchOptions that = (FetchOptions) obj;
317 if (prefetchSize != null) {
318 if (!prefetchSize.equals(that.prefetchSize)) {
319 return false;
321 } else if (that.prefetchSize != null) {
322 return false;
325 if (chunkSize != null) {
326 if (!chunkSize.equals(that.chunkSize)) {
327 return false;
329 } else if (that.chunkSize != null) {
330 return false;
333 if (limit != null) {
334 if (!limit.equals(that.limit)) {
335 return false;
337 } else if (that.limit != null) {
338 return false;
341 if (offset != null) {
342 if (!offset.equals(that.offset)) {
343 return false;
345 } else if (that.offset != null) {
346 return false;
349 if (startCursor != null) {
350 if (!startCursor.equals(that.startCursor)) {
351 return false;
353 } else if (that.startCursor != null) {
354 return false;
357 if (endCursor != null) {
358 if (!endCursor.equals(that.endCursor)) {
359 return false;
361 } else if (that.endCursor != null) {
362 return false;
365 if (compile != null) {
366 if (!compile.equals(that.compile)) {
367 return false;
369 } else if (that.compile != null) {
370 return false;
373 return true;
376 @Override
377 public String toString() {
378 List<String> result = new ArrayList<String>();
380 if (prefetchSize != null) {
381 result.add("prefetchSize=" + prefetchSize);
384 if (chunkSize != null) {
385 result.add("chunkSize=" + chunkSize);
388 if (limit != null) {
389 result.add("limit=" + limit);
392 if (offset != null) {
393 result.add("offset=" + offset);
396 if (startCursor != null) {
397 result.add("startCursor=" + startCursor);
400 if (endCursor != null) {
401 result.add("endCursor=" + endCursor);
404 if (compile != null) {
405 result.add("compile=" + compile);
407 return "FetchOptions" + result;
411 * Contains static creation methods for {@link FetchOptions}.
413 public static final class Builder {
416 * Create a {@link FetchOptions} with the given limit. Shorthand for
417 * <code>FetchOptions.withDefaults().limit(...);</code> Please read the
418 * {@link FetchOptions} class javadoc for an explanation of how limit
419 * is used.
420 * @param limit the limit to set.
421 * @return The newly created FetchOptions instance.
423 public static FetchOptions withLimit(int limit) {
424 return withDefaults().limit(limit);
428 * Create a {@link FetchOptions} with the given offset. Shorthand for
429 * <code>FetchOptions.withDefaults().offset(...);</code> Please read the
430 * {@link FetchOptions} class javadoc for an explanation of how offset
431 * is used.
432 * @param offset the offset to set.
433 * @return The newly created FetchOptions instance.
435 public static FetchOptions withOffset(int offset) {
436 return withDefaults().offset(offset);
440 * Create a {@link FetchOptions} with the given chunk size. Shorthand for
441 * <code>FetchOptions.withDefaults().chunkSize(...);</code> Please read the
442 * {@link FetchOptions} class javadoc for an explanation of how chunk size
443 * is used.
444 * @param chunkSize the chunkSize to set.
445 * @return The newly created FetchOptions instance.
447 public static FetchOptions withChunkSize(int chunkSize) {
448 return withDefaults().chunkSize(chunkSize);
452 * Create a {@link FetchOptions} with the given prefetch size.
453 * Shorthand for <code>FetchOptions.withDefaults().prefetchSize(...);</code>.
454 * Please read the {@link FetchOptions} class javadoc for an explanation of
455 * how prefetch size is used.
456 * @param prefetchSize the prefetchSize to set.
457 * @return The newly created FetchOptions instance.
459 public static FetchOptions withPrefetchSize(int prefetchSize) {
460 return withDefaults().prefetchSize(prefetchSize);
464 * Create a {@link FetchOptions} with the given cursor.
465 * Shorthand for <code>FetchOptions.withDefaults().cursor(cursor);</code>.
466 * Please read the {@link FetchOptions} class javadoc for an explanation of
467 * how cursors are used.
468 * @param cursor the cursor to set.
469 * @return The newly created FetchOptions instance.
470 * @deprecated use {@link #withStartCursor} instead.
472 @Deprecated
473 public static FetchOptions withCursor(Cursor cursor) {
474 return withStartCursor(cursor);
478 * Create a {@link FetchOptions} with the given start cursor.
479 * Shorthand for <code>FetchOptions.withDefaults().startCursor(cursor);</code>.
480 * Please read the {@link FetchOptions} class javadoc for an explanation of
481 * how cursors are used.
482 * @param startCursor the cursor to set.
483 * @return The newly created FetchOptions instance.
485 public static FetchOptions withStartCursor(Cursor startCursor) {
486 return withDefaults().startCursor(startCursor);
490 * Create a {@link FetchOptions} with the given end cursor.
491 * Shorthand for <code>FetchOptions.withDefaults().endCursor(cursor);</code>.
492 * Please read the {@link FetchOptions} class javadoc for an explanation of
493 * how cursors are used.
494 * @param endCursor the cursor to set.
495 * @return The newly created FetchOptions instance.
497 public static FetchOptions withEndCursor(Cursor endCursor) {
498 return withDefaults().endCursor(endCursor);
502 * Helper method for creating a {@link FetchOptions} instance with
503 * default values. The defaults are {@code null} for all values.
505 public static FetchOptions withDefaults() {
506 return new FetchOptions();
509 private Builder() {}