1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
;
5 import com
.google
.appengine
.api
.search
.SearchServicePb
.ListDocumentsParams
;
6 import com
.google
.appengine
.api
.search
.checkers
.ListRequestChecker
;
9 * A request to list objects in an index. You can specify a number of
10 * restrictions, such as the number of objects to return, the id of the
11 * first object to return, whether to only return keys, etc.
14 * ListRequest request = ListRequest().newBuilder()
16 * .setStartId("some-id")
17 * .setReturningIdsOnly(true)
22 public class ListRequest
{
25 * The builder of {@link ListRequest}s.
27 public static class Builder
{
28 private String startId
; private Boolean includeStart
; private Integer limit
; private Boolean returningIdsOnly
;
32 returningIdsOnly
= false;
35 private Builder(ListRequest request
) {
36 startId
= request
.getStartId();
37 includeStart
= request
.isIncludeStart();
38 limit
= request
.getLimit();
39 returningIdsOnly
= request
.isReturningIdsOnly();
43 * Sets the Id of the first object to return. You may exclude this
44 * object by using the {@link #setIncludeStart(boolean)} method.
46 * @param startId the Id of the first object to return
47 * @return this builder
48 * @throws IllegalArgumentException if invalid object Id is given
50 public Builder
setStartId(String startId
) {
51 this.startId
= ListRequestChecker
.checkStartDocId(startId
);
56 * Sets whether or not to include the object whose ID is specified via
57 * the {@link #setStartId(String)} method.
59 * @param includeStart whether or not to return the start index
60 * @return this builder
62 public Builder
setIncludeStart(boolean includeStart
) {
63 this.includeStart
= includeStart
;
68 * Sets the maximum number of objects to return.
70 * @param limit the maximum number of objects to return
71 * @return this builder
72 * @throws IllegalArgumentException if negative or too large limit is given
74 public Builder
setLimit(Integer limit
) {
75 this.limit
= ListRequestChecker
.checkLimit(limit
);
80 * @see {@link #setReturningIdsOnly(boolean)}
83 public Builder
setKeysOnly(boolean keysOnly
) {
84 return setReturningIdsOnly(keysOnly
);
88 * Sets whether just objects containing just their key are returned, or
89 * whether the complete objects are returned.
91 * @param returningIdsOnly whether to only return object keys
92 * @return this builder
94 public Builder
setReturningIdsOnly(boolean returningIdsOnly
) {
95 this.returningIdsOnly
= returningIdsOnly
;
100 * @return builds and returns a brand new instance of
101 * a {@link ListRequest} using values set on this builder
103 public ListRequest
build() {
104 return new ListRequest(this);
108 private final String startId
;
109 private final boolean includeStart
;
110 private final int limit
;
111 private final boolean returningIdsOnly
;
113 protected ListRequest(Builder builder
) {
114 startId
= builder
.startId
;
115 includeStart
= (startId
== null)
116 ? Boolean
.FALSE
: Util
.defaultIfNull(builder
.includeStart
, Boolean
.TRUE
);
117 limit
= Util
.defaultIfNull(builder
.limit
, ListRequestChecker
.DEFAULT_LIMIT
);
118 returningIdsOnly
= Util
.defaultIfNull(builder
.returningIdsOnly
, Boolean
.FALSE
);
121 public static Builder
newBuilder() {
122 return new Builder();
125 public static final Builder
newBuilder(ListRequest request
) {
126 return new Builder(request
);
130 * @return the Id of the first object to return
132 public String
getStartId() {
137 * @return whether or not the object with the start Id is returned
139 public boolean isIncludeStart() {
144 * @return the maximum number of objects returned by this request
146 public int getLimit() {
151 * @see {@link #isReturningIdsOnly()}
154 public Boolean
isKeysOnly() {
155 return isReturningIdsOnly();
159 * @return whether or not index schema is returned with each index
161 public Boolean
isReturningIdsOnly() {
162 return returningIdsOnly
;
166 public int hashCode() {
167 final int prime
= 31;
169 result
= prime
* result
+ (includeStart ?
1 : 0);
170 result
= prime
* result
+ (returningIdsOnly ?
1 : 0);
171 result
= prime
* result
+ limit
;
172 result
= prime
* result
+ ((startId
== null) ?
0 : startId
.hashCode());
177 public boolean equals(Object obj
) {
184 if (getClass() != obj
.getClass()) {
187 ListRequest other
= (ListRequest
) obj
;
188 return includeStart
== other
.includeStart
189 && returningIdsOnly
== other
.returningIdsOnly
190 && limit
== other
.limit
191 && Util
.equalObjects(startId
, other
.startId
);
195 public String
toString() {
196 return "ListRequest(includeStart=" + includeStart
+ ", startId=" + startId
197 + ", limit=" + limit
+ ", returningIdsOnly=" + returningIdsOnly
+ ")";
200 ListDocumentsParams
.Builder
copyToProtocolBuffer() {
201 ListDocumentsParams
.Builder builder
= ListDocumentsParams
.newBuilder();
203 builder
.setKeysOnly(true);
205 if (getStartId() != null) {
206 builder
.setStartDocId(getStartId());
208 if (!isIncludeStart()) {
209 builder
.setIncludeStartDoc(false);
211 builder
.setLimit(getLimit());