1 // Copyright 2012 Google Inc. All rights reserved.
2 package com
.google
.appengine
.api
.taskqueue
;
4 import java
.util
.Arrays
;
5 import java
.util
.concurrent
.TimeUnit
;
8 * Contains various options for lease requests following the builder pattern.
9 * Calls to {@link LeaseOptions} methods may be chained to specify
10 * multiple options in the one {@link LeaseOptions} object.
11 * <p>Notes on usage:<br> The recommended way to
12 * instantiate a {@link LeaseOptions} object is to statically import
13 * {@link Builder}.* and invoke a static creation method followed by
17 * import static com.google.appengine.api.taskqueue.LeaseOptions.Builder.*;
20 * tasks = pullQueue.leaseTasks(withLeasePeriod(2, TimeUnit.HOURS).countLimit(1000));
24 public final class LeaseOptions
{
27 private TimeUnit unit
;
28 private Long countLimit
;
29 private Double deadlineInSeconds
;
30 private boolean groupByTag
;
33 private LeaseOptions() {
36 this.countLimit
= null;
37 this.deadlineInSeconds
= null;
38 this.groupByTag
= false;
43 * A copy constructor for {@link LeaseOptions}.
45 public LeaseOptions(LeaseOptions options
) {
46 lease
= options
.lease
;
48 countLimit
= options
.countLimit
;
49 deadlineInSeconds
= options
.deadlineInSeconds
;
53 * Returns the lease period for lease requests.
54 * May be {@code null} if not specified.
61 * Returns the lease period unit for lease requests.
62 * May be {@code null} if not specified.
69 * Returns the count limit for lease requests.
70 * May be {@code null} if not specified.
72 Long
getCountLimit() {
77 * Returns the deadline for lease requests.
78 * May be {@code null} if not specified.
80 Double
getDeadlineInSeconds() {
81 return deadlineInSeconds
;
85 * Returns if leased tasks should be grouped by tag
87 boolean getGroupByTag() {
92 * Returns the task tag for lease requests.
99 * Sets the lease period for lease requests. Must be positive.
100 * @throws IllegalArgumentException
102 public LeaseOptions
leasePeriod(long lease
, TimeUnit unit
) {
104 throw new IllegalArgumentException("Unit for lease period must not be null");
107 throw new IllegalArgumentException("Lease period must be greater than 0, got " +
116 * Sets the count limit for lease requests. Must be positive.
117 * @throws IllegalArgumentException
119 public LeaseOptions
countLimit(long countLimit
) {
120 if (countLimit
<= 0) {
121 throw new IllegalArgumentException("Number of tasks to lease must be greater than 0");
123 this.countLimit
= countLimit
;
128 * Sets the deadline for lease requests. Must be positive.
129 * @throws IllegalArgumentException
131 public LeaseOptions
deadlineInSeconds( Double deadlineInSeconds
) {
132 if (deadlineInSeconds
!= null && deadlineInSeconds
<= 0.0) {
133 throw new IllegalArgumentException("Deadline must be > 0, got " +
136 this.deadlineInSeconds
= deadlineInSeconds
;
141 * Indicates that all tasks being leased must have the same tag. Redundant if tag is specified.
142 * @throws IllegalArgumentException
144 public LeaseOptions
groupByTag() {
145 this.groupByTag
= true;
150 * Sets the tag for lease requests. Must not be null.
151 * @throws IllegalArgumentException
153 public LeaseOptions
tag(byte[] tag
) {
155 throw new IllegalArgumentException("Tag must not be null");
157 this.groupByTag
= true;
163 * Sets the tag for lease requests. Must not be null.
164 * @throws IllegalArgumentException
166 public LeaseOptions
tag(String tag
) {
168 throw new IllegalArgumentException("Tag must not be null");
170 this.groupByTag
= true;
171 this.tag
= tag
.getBytes();
176 public int hashCode() {
177 final int prime
= 31;
179 result
= prime
* result
+
180 ((lease
== null) ?
0 : lease
.hashCode());
181 result
= prime
* result
+
182 ((unit
== null) ?
0 : unit
.hashCode());
183 result
= prime
* result
+
184 ((countLimit
== null) ?
0 : countLimit
.hashCode());
185 result
= prime
* result
+
186 ((deadlineInSeconds
== null) ?
0 : deadlineInSeconds
.hashCode());
187 result
= prime
* result
+
188 (groupByTag ?
1 : 0);
189 result
= prime
* result
+
190 ((tag
== null) ?
0 : Arrays
.hashCode(tag
));
195 public boolean equals(Object obj
) {
202 if (getClass() != obj
.getClass()) {
205 LeaseOptions other
= (LeaseOptions
) obj
;
207 if (other
.lease
!= null) {
210 } else if (!lease
.equals(other
.lease
)) {
214 if (other
.unit
!= null) {
217 } else if (!unit
.equals(other
.unit
)) {
220 if (countLimit
== null) {
221 if (other
.countLimit
!= null) {
224 } else if (!countLimit
.equals(other
.countLimit
)) {
227 if (deadlineInSeconds
== null) {
228 if (other
.deadlineInSeconds
!= null) {
231 } else if (!deadlineInSeconds
.equals(other
.deadlineInSeconds
)) {
234 if (groupByTag
!= other
.groupByTag
) {
237 if (!Arrays
.equals(tag
, other
.tag
)) {
244 public String
toString() {
245 return "LeaseOptions[lease=" + lease
+ " " + unit
+ ",countLimit=" + countLimit
+
246 ",deadlineInSeconds=" + deadlineInSeconds
+ ",groupByTag=" + groupByTag
+ "]";
250 * Provides static creation methods for {@link LeaseOptions}.
252 public static final class Builder
{
254 * Returns default {@link LeaseOptions} and calls
255 * {@link LeaseOptions#leasePeriod(long, TimeUnit)}.
257 public static LeaseOptions
withLeasePeriod(long lease
, TimeUnit unit
) {
258 return withDefaults().leasePeriod(lease
, unit
);
262 * Returns default {@link LeaseOptions} and calls
263 * {@link LeaseOptions#countLimit(long)}.
265 public static LeaseOptions
withCountLimit(long countLimit
) {
266 return withDefaults().countLimit(countLimit
);
270 * Returns default {@link LeaseOptions} and calls
271 * {@link LeaseOptions#deadlineInSeconds(Double)}.
273 public static LeaseOptions
withDeadlineInSeconds( Double deadlineInSeconds
) {
274 return withDefaults().deadlineInSeconds(deadlineInSeconds
);
278 * Returns default {@link LeaseOptions} and calls
279 * {@link LeaseOptions#tag(byte[])}.
281 public static LeaseOptions
withTag(byte[] tag
) {
282 return withDefaults().tag(tag
);
286 * Returns default {@link LeaseOptions} and calls
287 * {@link LeaseOptions#tag(String)}.
289 public static LeaseOptions
withTag(String tag
) {
290 return withDefaults().tag(tag
);
294 * Returns default {@link LeaseOptions} with default values.
296 private static LeaseOptions
withDefaults() {
297 return new LeaseOptions();