1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.blobstore.options;
20
21 import static com.google.common.base.Preconditions.checkArgument;
22 import static com.google.common.base.Preconditions.checkNotNull;
23
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class GetOptions {
47
48 public static final GetOptions NONE = new GetOptions();
49
50 private final List<String> ranges = new ArrayList<String>();
51 private Date ifModifiedSince;
52 private Date ifUnmodifiedSince;
53 private String ifMatch;
54 private String ifNoneMatch;
55
56
57
58
59
60
61
62 public GetOptions range(long start, long end) {
63 checkArgument(start >= 0, "start must be >= 0");
64 checkArgument(end >= 0, "end must be >= 0");
65 getRanges().add(String.format("%d-%d", start, end));
66 return this;
67 }
68
69
70
71
72 public GetOptions startAt(long start) {
73 checkArgument(start >= 0, "start must be >= 0");
74 getRanges().add(String.format("%d-", start));
75 return this;
76 }
77
78
79
80
81
82 public GetOptions tail(long length) {
83 checkArgument(length >= 0, "length must be >= 0");
84 getRanges().add(String.format("-%d", length));
85 return this;
86 }
87
88
89
90
91
92
93 public GetOptions ifModifiedSince(Date ifModifiedSince) {
94 checkArgument(getIfMatch() == null, "ifETagMatches() is not compatible with ifModifiedSince()");
95 checkArgument(getIfUnmodifiedSince() == null, "ifUnmodifiedSince() is not compatible with ifModifiedSince()");
96 this.ifModifiedSince = checkNotNull(ifModifiedSince, "ifModifiedSince");
97 return this;
98 }
99
100
101
102
103
104
105
106
107
108 public Date getIfModifiedSince() {
109 return this.ifModifiedSince;
110 }
111
112
113
114
115
116
117 public GetOptions ifUnmodifiedSince(Date ifUnmodifiedSince) {
118 checkArgument(getIfNoneMatch() == null, "ifETagDoesntMatch() is not compatible with ifUnmodifiedSince()");
119 checkArgument(getIfModifiedSince() == null, "ifModifiedSince() is not compatible with ifUnmodifiedSince()");
120 this.ifUnmodifiedSince = checkNotNull(ifUnmodifiedSince, "ifUnmodifiedSince");
121 return this;
122 }
123
124
125
126
127
128
129
130
131
132 public Date getIfUnmodifiedSince() {
133 return this.ifUnmodifiedSince;
134 }
135
136
137
138
139
140
141
142
143
144
145 public GetOptions ifETagMatches(String eTag) {
146 checkArgument(getIfNoneMatch() == null, "ifETagDoesntMatch() is not compatible with ifETagMatches()");
147 checkArgument(getIfModifiedSince() == null, "ifModifiedSince() is not compatible with ifETagMatches()");
148 this.ifMatch = checkNotNull(eTag, "eTag");
149 return this;
150 }
151
152
153
154
155
156
157
158
159
160 public String getIfMatch() {
161 return this.ifMatch;
162 }
163
164
165
166
167
168
169
170
171
172 public GetOptions ifETagDoesntMatch(String eTag) {
173 checkArgument(getIfMatch() == null, "ifETagMatches() is not compatible with ifETagDoesntMatch()");
174 checkArgument(getIfUnmodifiedSince() == null, "ifUnmodifiedSince() is not compatible with ifETagDoesntMatch()");
175 this.ifNoneMatch = checkNotNull(eTag, "eTag");
176 return this;
177 }
178
179
180
181
182
183
184
185
186
187 public String getIfNoneMatch() {
188 return this.ifNoneMatch;
189 }
190
191 public List<String> getRanges() {
192 return ranges;
193 }
194
195 public static class Builder {
196
197
198
199
200 public static GetOptions range(long start, long end) {
201 GetOptions options = new GetOptions();
202 return options.range(start, end);
203 }
204
205
206
207
208 public static GetOptions ifModifiedSince(Date ifModifiedSince) {
209 GetOptions options = new GetOptions();
210 return options.ifModifiedSince(ifModifiedSince);
211 }
212
213
214
215
216 public static GetOptions ifUnmodifiedSince(Date ifUnmodifiedSince) {
217 GetOptions options = new GetOptions();
218 return options.ifUnmodifiedSince(ifUnmodifiedSince);
219 }
220
221
222
223
224 public static GetOptions ifETagMatches(String eTag) throws UnsupportedEncodingException {
225 GetOptions options = new GetOptions();
226 return options.ifETagMatches(eTag);
227 }
228
229
230
231
232 public static GetOptions ifETagDoesntMatch(String eTag) throws UnsupportedEncodingException {
233 GetOptions options = new GetOptions();
234 return options.ifETagDoesntMatch(eTag);
235 }
236
237 }
238
239 @Override
240 public int hashCode() {
241 final int prime = 31;
242 int result = 1;
243 result = prime * result + ((ifMatch == null) ? 0 : ifMatch.hashCode());
244 result = prime * result + ((ifModifiedSince == null) ? 0 : ifModifiedSince.hashCode());
245 result = prime * result + ((ifNoneMatch == null) ? 0 : ifNoneMatch.hashCode());
246 result = prime * result + ((ifUnmodifiedSince == null) ? 0 : ifUnmodifiedSince.hashCode());
247 result = prime * result + ((ranges == null) ? 0 : ranges.hashCode());
248 return result;
249 }
250
251 @Override
252 public boolean equals(Object obj) {
253 if (this == obj)
254 return true;
255 if (obj == null)
256 return false;
257 if (getClass() != obj.getClass())
258 return false;
259 GetOptions other = (GetOptions) obj;
260 if (ifMatch == null) {
261 if (other.ifMatch != null)
262 return false;
263 } else if (!ifMatch.equals(other.ifMatch))
264 return false;
265 if (ifModifiedSince == null) {
266 if (other.ifModifiedSince != null)
267 return false;
268 } else if (!ifModifiedSince.equals(other.ifModifiedSince))
269 return false;
270 if (ifNoneMatch == null) {
271 if (other.ifNoneMatch != null)
272 return false;
273 } else if (!ifNoneMatch.equals(other.ifNoneMatch))
274 return false;
275 if (ifUnmodifiedSince == null) {
276 if (other.ifUnmodifiedSince != null)
277 return false;
278 } else if (!ifUnmodifiedSince.equals(other.ifUnmodifiedSince))
279 return false;
280 if (ranges == null) {
281 if (other.ranges != null)
282 return false;
283 } else if (!ranges.equals(other.ranges))
284 return false;
285 return true;
286 }
287
288 @Override
289 public String toString() {
290 return "[ranges=" + ranges + ", ifModifiedSince=" + ifModifiedSince + ", ifUnmodifiedSince=" + ifUnmodifiedSince
291 + ", ifMatch=" + ifMatch + ", ifNoneMatch=" + ifNoneMatch + "]";
292 }
293
294 }