Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / images / ServingUrlOptions.java
blob4cf3ecee9ee98f0963eb544c63726a33bb718ccc
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;
8 /**
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;
21 private Boolean crop;
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;
34 return this;
37 boolean hasBlobKey() {
38 return blobKey != null;
41 BlobKey getBlobKey() {
42 if (blobKey == null) {
43 throw new IllegalStateException("blobKey has not been set.");
45 return blobKey;
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;
54 return this;
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;
70 return this;
73 boolean hasSecureUrl() {
74 return secureUrl != null;
77 boolean getSecureUrl() {
78 if (secureUrl == null) {
79 throw new IllegalStateException("secureUrl has not been set.");
81 return secureUrl;
84 public ServingUrlOptions crop(boolean crop) {
85 this.crop = crop;
86 return this;
89 boolean hasCrop() {
90 return crop != null;
93 boolean getCrop() {
94 if (crop == null) {
95 throw new IllegalStateException("crop has not been set.");
97 return crop;
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;
106 return this;
109 boolean hasImageSize() {
110 return imageSize != null;
113 int getImageSize() {
114 if (imageSize == null) {
115 throw new IllegalStateException("imageSize has not been set.");
117 return imageSize;
120 @Override
121 public int hashCode() {
122 int hash = 17;
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();
132 if (crop != null) {
133 hash = hash * 37 + crop.hashCode();
135 if (imageSize != null) {
136 hash = hash * 37 + imageSize.hashCode();
138 return hash;
141 @Override
142 public boolean equals(Object object) {
143 if (object instanceof ServingUrlOptions) {
144 ServingUrlOptions key = (ServingUrlOptions) object;
146 if (hasBlobKey() != key.hasBlobKey()) {
147 return false;
149 if (hasBlobKey()) {
150 if (!blobKey.equals(key.blobKey)) {
151 return false;
155 if (hasGoogleStorageFileName() != key.hasGoogleStorageFileName()) {
156 return false;
158 if (hasGoogleStorageFileName()) {
159 if (!googleStorageFileName.equals(key.googleStorageFileName)) {
160 return false;
164 if (hasSecureUrl() != key.hasSecureUrl()) {
165 return false;
167 if (hasSecureUrl()) {
168 if (!secureUrl.equals(key.secureUrl)) {
169 return false;
173 if (hasCrop() != key.hasCrop()) {
174 return false;
176 if (hasCrop()) {
177 if (!crop.equals(key.crop)) {
178 return false;
182 if (hasImageSize() != key.hasImageSize()) {
183 return false;
185 if (hasImageSize()) {
186 if (!imageSize.equals(key.imageSize)) {
187 return false;
190 return true;
192 return false;
195 @Override
196 public String toString() {
197 StringBuilder buffer = new StringBuilder("ServingUrlOptions: blobKey=");
198 if (blobKey != null) {
199 buffer.append(blobKey.toString());
200 } else {
201 buffer.append("None");
203 buffer.append(", googleStorageFileName=");
204 if (googleStorageFileName != null) {
205 buffer.append(googleStorageFileName);
206 } else {
207 buffer.append("None");
209 buffer.append(", secureUrl=");
210 if (secureUrl != null) {
211 buffer.append(secureUrl);
212 } else {
213 buffer.append("false");
215 buffer.append(", hasCrop=");
216 if (crop != null) {
217 buffer.append(crop);
218 } else {
219 buffer.append("false");
221 buffer.append(", imageSize=");
222 if (imageSize != null) {
223 buffer.append(imageSize);
224 } else {
225 buffer.append("Not Set");
227 buffer.append(".");
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();
258 private Builder() {