Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / taskqueue / LeaseOptions.java
blobb07e3ba13abc96521ea3482c024fbdba5df5f806
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;
7 /**
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
14 * instance mutators:
16 * <pre>
17 * import static com.google.appengine.api.taskqueue.LeaseOptions.Builder.*;
19 * ...
20 * tasks = pullQueue.leaseTasks(withLeasePeriod(2, TimeUnit.HOURS).countLimit(1000));
21 * </pre>
24 public final class LeaseOptions {
26 private Long lease;
27 private TimeUnit unit;
28 private Long countLimit;
29 private Double deadlineInSeconds;
30 private boolean groupByTag;
31 private byte[] tag;
33 private LeaseOptions() {
34 this.lease = null;
35 this.unit = null;
36 this.countLimit = null;
37 this.deadlineInSeconds = null;
38 this.groupByTag = false;
39 this.tag = null;
42 /**
43 * A copy constructor for {@link LeaseOptions}.
45 public LeaseOptions(LeaseOptions options) {
46 lease = options.lease;
47 unit = options.unit;
48 countLimit = options.countLimit;
49 deadlineInSeconds = options.deadlineInSeconds;
52 /**
53 * Returns the lease period for lease requests.
54 * May be {@code null} if not specified.
56 Long getLease() {
57 return lease;
60 /**
61 * Returns the lease period unit for lease requests.
62 * May be {@code null} if not specified.
64 TimeUnit getUnit() {
65 return unit;
68 /**
69 * Returns the count limit for lease requests.
70 * May be {@code null} if not specified.
72 Long getCountLimit() {
73 return countLimit;
76 /**
77 * Returns the deadline for lease requests.
78 * May be {@code null} if not specified.
80 Double getDeadlineInSeconds() {
81 return deadlineInSeconds;
84 /**
85 * Returns if leased tasks should be grouped by tag
87 boolean getGroupByTag() {
88 return groupByTag;
91 /**
92 * Returns the task tag for lease requests.
94 byte[] getTag() {
95 return tag;
98 /**
99 * Sets the lease period for lease requests. Must be positive.
100 * @throws IllegalArgumentException
102 public LeaseOptions leasePeriod(long lease, TimeUnit unit) {
103 if (unit == null) {
104 throw new IllegalArgumentException("Unit for lease period must not be null");
106 if (lease <= 0) {
107 throw new IllegalArgumentException("Lease period must be greater than 0, got " +
108 lease + " " + unit);
110 this.lease = lease;
111 this.unit = unit;
112 return this;
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;
124 return this;
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 " +
134 deadlineInSeconds);
136 this.deadlineInSeconds = deadlineInSeconds;
137 return this;
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;
146 return this;
150 * Sets the tag for lease requests. Must not be null.
151 * @throws IllegalArgumentException
153 public LeaseOptions tag(byte[] tag) {
154 if (tag == null) {
155 throw new IllegalArgumentException("Tag must not be null");
157 this.groupByTag = true;
158 this.tag = tag;
159 return this;
163 * Sets the tag for lease requests. Must not be null.
164 * @throws IllegalArgumentException
166 public LeaseOptions tag(String tag) {
167 if (tag == null) {
168 throw new IllegalArgumentException("Tag must not be null");
170 this.groupByTag = true;
171 this.tag = tag.getBytes();
172 return this;
175 @Override
176 public int hashCode() {
177 final int prime = 31;
178 int result = 1;
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));
191 return result;
194 @Override
195 public boolean equals(Object obj) {
196 if (this == obj) {
197 return true;
199 if (obj == null) {
200 return false;
202 if (getClass() != obj.getClass()) {
203 return false;
205 LeaseOptions other = (LeaseOptions) obj;
206 if (lease == null) {
207 if (other.lease != null) {
208 return false;
210 } else if (!lease.equals(other.lease)) {
211 return false;
213 if (unit == null) {
214 if (other.unit != null) {
215 return false;
217 } else if (!unit.equals(other.unit)) {
218 return false;
220 if (countLimit == null) {
221 if (other.countLimit != null) {
222 return false;
224 } else if (!countLimit.equals(other.countLimit)) {
225 return false;
227 if (deadlineInSeconds == null) {
228 if (other.deadlineInSeconds != null) {
229 return false;
231 } else if (!deadlineInSeconds.equals(other.deadlineInSeconds)) {
232 return false;
234 if (groupByTag != other.groupByTag) {
235 return false;
237 if (!Arrays.equals(tag, other.tag)) {
238 return false;
240 return true;
243 @Override
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();
300 private Builder() {