App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / api / blobstore / UploadOptions.java
blob46bba8cba4b967c1a29a1f9dbc7662c326339c6f
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.blobstore;
5 /**
6 * Allows users to customize the behavior of a single upload to the
7 * {@link BlobstoreService}.
9 */
10 public final class UploadOptions {
12 private Long maxUploadSizeBytesPerBlob;
14 private Long maxUploadSizeBytes;
16 private String gsBucketName;
18 private UploadOptions() {
21 /**
22 * Sets the maximum size in bytes for any one blob in the upload. If any blob
23 * in the upload exceeds this value then a 413 error will be returned to the
24 * client.
25 * @param maxUploadSizeBytesPerBlob The maximum size in bytes that any one
26 * blob in the upload can be.
27 * @return {@code this} (for chaining)
29 public UploadOptions maxUploadSizeBytesPerBlob(long maxUploadSizeBytesPerBlob) {
30 if (maxUploadSizeBytesPerBlob < 1) {
31 throw new IllegalArgumentException("maxUploadSizeBytesPerBlob must be positive.");
33 this.maxUploadSizeBytesPerBlob = maxUploadSizeBytesPerBlob;
34 return this;
37 boolean hasMaxUploadSizeBytesPerBlob() {
38 return maxUploadSizeBytesPerBlob != null;
41 long getMaxUploadSizeBytesPerBlob() {
42 if (maxUploadSizeBytesPerBlob == null) {
43 throw new IllegalStateException("maxUploadSizeBytesPerBlob has not been set.");
45 return maxUploadSizeBytesPerBlob;
48 /**
49 * Sets the maximum size in bytes that for the total upload. If the upload
50 * exceeds this value then a 413 error will be returned to the client.
51 * @param maxUploadSizeBytes The maximum size in bytes for the upload.
52 * @return {@code this} (for chaining)
54 public UploadOptions maxUploadSizeBytes(long maxUploadSizeBytes) {
55 if (maxUploadSizeBytes < 1) {
56 throw new IllegalArgumentException("maxUploadSizeBytes must be positive.");
58 this.maxUploadSizeBytes = maxUploadSizeBytes;
59 return this;
62 boolean hasMaxUploadSizeBytes() {
63 return maxUploadSizeBytes != null;
66 long getMaxUploadSizeBytes() {
67 if (maxUploadSizeBytes == null) {
68 throw new IllegalStateException("maxUploadSizeBytes has not been set.");
70 return maxUploadSizeBytes;
73 public UploadOptions googleStorageBucketName(String bucketName) {
74 this.gsBucketName = bucketName;
75 return this;
78 boolean hasGoogleStorageBucketName() {
79 return this.gsBucketName != null;
82 String getGoogleStorageBucketName() {
83 if (gsBucketName == null) {
84 throw new IllegalStateException("gsBucketName has not been set.");
86 return gsBucketName;
89 @Override
90 public int hashCode() {
91 int hash = 17;
92 if (maxUploadSizeBytesPerBlob != null) {
93 hash = hash * 37 + maxUploadSizeBytesPerBlob.hashCode();
95 if (maxUploadSizeBytes != null) {
96 hash = hash * 37 + maxUploadSizeBytes.hashCode();
98 if (gsBucketName != null) {
99 hash = hash * 37 + gsBucketName.hashCode();
101 return hash;
104 @Override
105 public boolean equals(Object object) {
106 if (object instanceof UploadOptions) {
107 UploadOptions key = (UploadOptions) object;
109 if (hasMaxUploadSizeBytesPerBlob() != key.hasMaxUploadSizeBytesPerBlob()) {
110 return false;
112 if (hasMaxUploadSizeBytesPerBlob()) {
113 if (!maxUploadSizeBytesPerBlob.equals(key.getMaxUploadSizeBytesPerBlob())) {
114 return false;
118 if (hasMaxUploadSizeBytes() != key.hasMaxUploadSizeBytes()) {
119 return false;
121 if (hasMaxUploadSizeBytes()) {
122 if (!maxUploadSizeBytes.equals(key.getMaxUploadSizeBytes())) {
123 return false;
127 if (hasGoogleStorageBucketName() != key.hasGoogleStorageBucketName()) {
128 return false;
130 if (hasGoogleStorageBucketName()) {
131 if (!gsBucketName.equals(key.gsBucketName)) {
132 return false;
135 return true;
137 return false;
140 @Override
141 public String toString() {
142 StringBuilder buffer = new StringBuilder("UploadOptions: maxUploadSizeBytes=");
143 if (maxUploadSizeBytes != null) {
144 buffer.append(maxUploadSizeBytes);
145 } else {
146 buffer.append("unlimited");
148 buffer.append(", maxUploadSizeBytesPerBlob=");
149 if (maxUploadSizeBytesPerBlob != null) {
150 buffer.append(maxUploadSizeBytesPerBlob);
151 } else {
152 buffer.append("unlimited");
154 buffer.append(", gsBucketName=");
155 if (gsBucketName != null) {
156 buffer.append(gsBucketName);
157 } else {
158 buffer.append("None");
160 buffer.append(".");
161 return buffer.toString();
165 * Contains static creation methods for {@link UploadOptions}.
167 public static final class Builder {
169 * Returns default {@link UploadOptions} and calls
170 * {@link UploadOptions#maxUploadSizeBytes(long)}.
172 public static UploadOptions withMaxUploadSizeBytes(long maxUploadSizeBytes) {
173 return withDefaults().maxUploadSizeBytes(maxUploadSizeBytes);
177 * Returns default {@link UploadOptions} and calls
178 * {@link UploadOptions#maxUploadSizeBytesPerBlob(long)}.
180 public static UploadOptions withMaxUploadSizeBytesPerBlob(long maxUploadSizeBytesPerBlob) {
181 return withDefaults().maxUploadSizeBytesPerBlob(maxUploadSizeBytesPerBlob);
185 * Returns default {@link UploadOptions} and calls
186 * {@link UploadOptions#googleStorageBucketName(String)}.
188 public static UploadOptions withGoogleStorageBucketName(String gsBucketName) {
189 return withDefaults().googleStorageBucketName(gsBucketName);
193 * Returns default {@link UploadOptions} with default values.
195 public static UploadOptions withDefaults() {
196 return new UploadOptions();
199 private Builder() {