1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.images
;
5 import com
.google
.appengine
.api
.blobstore
.BlobKey
;
6 import static com
.google
.appengine
.api
.images
.ImagesService
.SERVING_SIZES_LIMIT
;
9 * Allow users to customize the behavior of creating a image serving URL using
10 * the {@link ImagesService}.
13 public final class ServingUrlOptions
{
15 private BlobKey blobKey
;
17 private Boolean secureUrl
;
19 private Integer imageSize
;
23 private String googleStorageFileName
;
25 private ServingUrlOptions() {
28 public ServingUrlOptions
blobKey(BlobKey blobKey
) {
29 if (googleStorageFileName
!= null) {
30 throw new IllegalArgumentException("Cannot specify both a blobKey and a " +
31 "googleStorageFileName.");
33 this.blobKey
= blobKey
;
37 boolean hasBlobKey() {
38 return blobKey
!= null;
41 BlobKey
getBlobKey() {
42 if (blobKey
== null) {
43 throw new IllegalStateException("blobKey has not been set.");
48 public ServingUrlOptions
googleStorageFileName(String fileName
) {
49 if (blobKey
!= null) {
50 throw new IllegalArgumentException("Cannot specify both a blobKey and a " +
51 "googleStorageFileName.");
53 this.googleStorageFileName
= fileName
;
57 boolean hasGoogleStorageFileName() {
58 return googleStorageFileName
!= null;
61 String
getGoogleStorageFileName() {
62 if (googleStorageFileName
== null) {
63 throw new IllegalStateException("googleStorageFileName has not been set.");
65 return googleStorageFileName
;
68 public ServingUrlOptions
secureUrl(boolean secureUrl
) {
69 this.secureUrl
= secureUrl
;
73 boolean hasSecureUrl() {
74 return secureUrl
!= null;
77 boolean getSecureUrl() {
78 if (secureUrl
== null) {
79 throw new IllegalStateException("secureUrl has not been set.");
84 public ServingUrlOptions
crop(boolean crop
) {
95 throw new IllegalStateException("crop has not been set.");
100 public ServingUrlOptions
imageSize(int imageSize
) {
101 if (imageSize
> SERVING_SIZES_LIMIT
|| imageSize
< 0) {
102 throw new IllegalArgumentException("Unsupported size: " + imageSize
+
103 ". Valid sizes must be between 0 and " + SERVING_SIZES_LIMIT
);
105 this.imageSize
= imageSize
;
109 boolean hasImageSize() {
110 return imageSize
!= null;
114 if (imageSize
== null) {
115 throw new IllegalStateException("imageSize has not been set.");
121 public int hashCode() {
123 if (blobKey
!= null) {
124 hash
= hash
* 37 + blobKey
.hashCode();
126 if (googleStorageFileName
!= null) {
127 hash
= hash
* 37 + googleStorageFileName
.hashCode();
129 if (secureUrl
!= null) {
130 hash
= hash
* 37 + secureUrl
.hashCode();
133 hash
= hash
* 37 + crop
.hashCode();
135 if (imageSize
!= null) {
136 hash
= hash
* 37 + imageSize
.hashCode();
142 public boolean equals(Object object
) {
143 if (object
instanceof ServingUrlOptions
) {
144 ServingUrlOptions key
= (ServingUrlOptions
) object
;
146 if (hasBlobKey() != key
.hasBlobKey()) {
150 if (!blobKey
.equals(key
.blobKey
)) {
155 if (hasGoogleStorageFileName() != key
.hasGoogleStorageFileName()) {
158 if (hasGoogleStorageFileName()) {
159 if (!googleStorageFileName
.equals(key
.googleStorageFileName
)) {
164 if (hasSecureUrl() != key
.hasSecureUrl()) {
167 if (hasSecureUrl()) {
168 if (!secureUrl
.equals(key
.secureUrl
)) {
173 if (hasCrop() != key
.hasCrop()) {
177 if (!crop
.equals(key
.crop
)) {
182 if (hasImageSize() != key
.hasImageSize()) {
185 if (hasImageSize()) {
186 if (!imageSize
.equals(key
.imageSize
)) {
196 public String
toString() {
197 StringBuilder buffer
= new StringBuilder("ServingUrlOptions: blobKey=");
198 if (blobKey
!= null) {
199 buffer
.append(blobKey
.toString());
201 buffer
.append("None");
203 buffer
.append(", googleStorageFileName=");
204 if (googleStorageFileName
!= null) {
205 buffer
.append(googleStorageFileName
);
207 buffer
.append("None");
209 buffer
.append(", secureUrl=");
210 if (secureUrl
!= null) {
211 buffer
.append(secureUrl
);
213 buffer
.append("false");
215 buffer
.append(", hasCrop=");
219 buffer
.append("false");
221 buffer
.append(", imageSize=");
222 if (imageSize
!= null) {
223 buffer
.append(imageSize
);
225 buffer
.append("Not Set");
228 return buffer
.toString();
232 * Contains static creation methods for {@link ServingUrlOptions}.
234 public static final class Builder
{
236 * Returns default {@link ServingUrlOptions} and calls
237 * {@link ServingUrlOptions#blobKey(BlobKey)}.
239 public static ServingUrlOptions
withBlobKey(BlobKey blobKey
) {
240 return withDefaults().blobKey(blobKey
);
244 * Returns default {@link ServingUrlOptions} and calls
245 * {@link ServingUrlOptions#googleStorageFileName(String)}.
247 public static ServingUrlOptions
withGoogleStorageFileName(String googleStorageFileName
) {
248 return withDefaults().googleStorageFileName(googleStorageFileName
);
252 * Returns default {@link ServingUrlOptions}.
254 private static ServingUrlOptions
withDefaults() {
255 return new ServingUrlOptions();