1616
1717package com .google .gcloud .storage ;
1818
19- import static com .google .common .base .Preconditions .checkArgument ;
2019import static com .google .common .base .Preconditions .checkNotNull ;
2120import static com .google .gcloud .storage .Bucket .BucketSourceOption .toGetOptions ;
2221import static com .google .gcloud .storage .Bucket .BucketSourceOption .toSourceOptions ;
2322
24- import com .google .common .base .Function ;
2523import com .google .common .base .MoreObjects ;
26- import com .google .common .collect .Lists ;
27- import com .google .common .collect .Sets ;
2824import com .google .gcloud .Page ;
2925import com .google .gcloud .spi .StorageRpc ;
3026import com .google .gcloud .storage .Storage .BlobGetOption ;
27+ import com .google .gcloud .storage .Storage .BlobTargetOption ;
28+ import com .google .gcloud .storage .Storage .BlobWriteOption ;
3129import com .google .gcloud .storage .Storage .BucketTargetOption ;
3230
3331import java .io .IOException ;
3432import java .io .InputStream ;
3533import java .io .ObjectInputStream ;
36- import java .io .Serializable ;
3734import java .util .ArrayList ;
3835import java .util .Arrays ;
3936import java .util .Collections ;
4037import java .util .List ;
4138import java .util .Objects ;
42- import java .util .Set ;
4339
4440/**
4541 * A Google cloud storage bucket.
@@ -68,7 +64,7 @@ private BucketSourceOption(StorageRpc.Option rpcOption) {
6864 super (rpcOption , null );
6965 }
7066
71- private Storage .BucketSourceOption toSourceOption (BucketInfo bucketInfo ) {
67+ private Storage .BucketSourceOption toSourceOptions (BucketInfo bucketInfo ) {
7268 switch (rpcOption ()) {
7369 case IF_METAGENERATION_MATCH :
7470 return Storage .BucketSourceOption .metagenerationMatch (bucketInfo .metageneration ());
@@ -112,7 +108,7 @@ static Storage.BucketSourceOption[] toSourceOptions(BucketInfo bucketInfo,
112108 new Storage .BucketSourceOption [options .length ];
113109 int index = 0 ;
114110 for (BucketSourceOption option : options ) {
115- convertedOptions [index ++] = option .toSourceOption (bucketInfo );
111+ convertedOptions [index ++] = option .toSourceOptions (bucketInfo );
116112 }
117113 return convertedOptions ;
118114 }
@@ -128,287 +124,6 @@ static Storage.BucketGetOption[] toGetOptions(BucketInfo bucketInfo,
128124 }
129125 }
130126
131- /**
132- * Class for specifying blob target options when {@code Bucket} methods are used.
133- */
134- public static class BlobTargetOption extends Option {
135-
136- private static final Function <BlobTargetOption , StorageRpc .Option > TO_ENUM =
137- new Function <BlobTargetOption , StorageRpc .Option >() {
138- @ Override
139- public StorageRpc .Option apply (BlobTargetOption blobTargetOption ) {
140- return blobTargetOption .rpcOption ();
141- }
142- };
143- private static final long serialVersionUID = 8345296337342509425L ;
144-
145- private BlobTargetOption (StorageRpc .Option rpcOption , Object value ) {
146- super (rpcOption , value );
147- }
148-
149- private StorageRpc .Tuple <BlobInfo , Storage .BlobTargetOption > toTargetOption (BlobInfo blobInfo ) {
150- BlobId blobId = blobInfo .blobId ();
151- switch (rpcOption ()) {
152- case PREDEFINED_ACL :
153- return StorageRpc .Tuple .of (blobInfo ,
154- Storage .BlobTargetOption .predefinedAcl ((Storage .PredefinedAcl ) value ()));
155- case IF_GENERATION_MATCH :
156- blobId = BlobId .of (blobId .bucket (), blobId .name (), (Long ) value ());
157- return StorageRpc .Tuple .of (blobInfo .toBuilder ().blobId (blobId ).build (),
158- Storage .BlobTargetOption .generationMatch ());
159- case IF_GENERATION_NOT_MATCH :
160- blobId = BlobId .of (blobId .bucket (), blobId .name (), (Long ) value ());
161- return StorageRpc .Tuple .of (blobInfo .toBuilder ().blobId (blobId ).build (),
162- Storage .BlobTargetOption .generationNotMatch ());
163- case IF_METAGENERATION_MATCH :
164- return StorageRpc .Tuple .of (blobInfo .toBuilder ().metageneration ((Long ) value ()).build (),
165- Storage .BlobTargetOption .metagenerationMatch ());
166- case IF_METAGENERATION_NOT_MATCH :
167- return StorageRpc .Tuple .of (blobInfo .toBuilder ().metageneration ((Long ) value ()).build (),
168- Storage .BlobTargetOption .metagenerationNotMatch ());
169- default :
170- throw new AssertionError ("Unexpected enum value" );
171- }
172- }
173-
174- /**
175- * Returns an option for specifying blob's predefined ACL configuration.
176- */
177- public static BlobTargetOption predefinedAcl (Storage .PredefinedAcl acl ) {
178- return new BlobTargetOption (StorageRpc .Option .PREDEFINED_ACL , acl );
179- }
180-
181- /**
182- * Returns an option that causes an operation to succeed only if the target blob does not exist.
183- * This option can not be provided together with {@link #generationMatch(long)} or
184- * {@link #generationNotMatch(long)}.
185- */
186- public static BlobTargetOption doesNotExist () {
187- return new BlobTargetOption (StorageRpc .Option .IF_GENERATION_MATCH , 0L );
188- }
189-
190- /**
191- * Returns an option for blob's data generation match. If this option is used the request will
192- * fail if generation does not match the provided value. This option can not be provided
193- * together with {@link #generationNotMatch(long)} or {@link #doesNotExist()}.
194- */
195- public static BlobTargetOption generationMatch (long generation ) {
196- return new BlobTargetOption (StorageRpc .Option .IF_GENERATION_MATCH , generation );
197- }
198-
199- /**
200- * Returns an option for blob's data generation mismatch. If this option is used the request
201- * will fail if blob's generation matches the provided value. This option can not be provided
202- * together with {@link #generationMatch(long)} or {@link #doesNotExist()}.
203- */
204- public static BlobTargetOption generationNotMatch (long generation ) {
205- return new BlobTargetOption (StorageRpc .Option .IF_GENERATION_NOT_MATCH , generation );
206- }
207-
208- /**
209- * Returns an option for blob's metageneration match. If this option is used the request will
210- * fail if metageneration does not match the provided value. This option can not be provided
211- * together with {@link #metagenerationNotMatch(long)}.
212- */
213- public static BlobTargetOption metagenerationMatch (long metageneration ) {
214- return new BlobTargetOption (StorageRpc .Option .IF_METAGENERATION_MATCH , metageneration );
215- }
216-
217- /**
218- * Returns an option for blob's metageneration mismatch. If this option is used the request will
219- * fail if metageneration matches the provided value. This option can not be provided together
220- * with {@link #metagenerationMatch(long)}.
221- */
222- public static BlobTargetOption metagenerationNotMatch (long metageneration ) {
223- return new BlobTargetOption (StorageRpc .Option .IF_METAGENERATION_NOT_MATCH , metageneration );
224- }
225-
226- static StorageRpc .Tuple <BlobInfo , Storage .BlobTargetOption []> toTargetOptions (
227- BlobInfo info , BlobTargetOption ... options ) {
228- Set <StorageRpc .Option > optionSet =
229- Sets .immutableEnumSet (Lists .transform (Arrays .asList (options ), TO_ENUM ));
230- checkArgument (!(optionSet .contains (StorageRpc .Option .IF_METAGENERATION_NOT_MATCH )
231- && optionSet .contains (StorageRpc .Option .IF_METAGENERATION_MATCH )),
232- "metagenerationMatch and metagenerationNotMatch options can not be both provided" );
233- checkArgument (!(optionSet .contains (StorageRpc .Option .IF_GENERATION_NOT_MATCH )
234- && optionSet .contains (StorageRpc .Option .IF_GENERATION_MATCH )),
235- "Only one option of generationMatch, doesNotExist or generationNotMatch can be provided" );
236- Storage .BlobTargetOption [] convertedOptions = new Storage .BlobTargetOption [options .length ];
237- BlobInfo targetInfo = info ;
238- int index = 0 ;
239- for (BlobTargetOption option : options ) {
240- StorageRpc .Tuple <BlobInfo , Storage .BlobTargetOption > target =
241- option .toTargetOption (targetInfo );
242- targetInfo = target .x ();
243- convertedOptions [index ++] = target .y ();
244- }
245- return StorageRpc .Tuple .of (targetInfo , convertedOptions );
246- }
247- }
248-
249- /**
250- * Class for specifying blob write options when {@code Bucket} methods are used.
251- */
252- public static class BlobWriteOption implements Serializable {
253-
254- private static final Function <BlobWriteOption , Storage .BlobWriteOption .Option > TO_ENUM =
255- new Function <BlobWriteOption , Storage .BlobWriteOption .Option >() {
256- @ Override
257- public Storage .BlobWriteOption .Option apply (BlobWriteOption blobWriteOption ) {
258- return blobWriteOption .option ;
259- }
260- };
261- private static final long serialVersionUID = 4722190734541993114L ;
262-
263- private final Storage .BlobWriteOption .Option option ;
264- private final Object value ;
265-
266- private StorageRpc .Tuple <BlobInfo , Storage .BlobWriteOption > toWriteOption (BlobInfo blobInfo ) {
267- BlobId blobId = blobInfo .blobId ();
268- switch (option ) {
269- case PREDEFINED_ACL :
270- return StorageRpc .Tuple .of (blobInfo ,
271- Storage .BlobWriteOption .predefinedAcl ((Storage .PredefinedAcl ) value ));
272- case IF_GENERATION_MATCH :
273- blobId = BlobId .of (blobId .bucket (), blobId .name (), (Long ) value );
274- return StorageRpc .Tuple .of (blobInfo .toBuilder ().blobId (blobId ).build (),
275- Storage .BlobWriteOption .generationMatch ());
276- case IF_GENERATION_NOT_MATCH :
277- blobId = BlobId .of (blobId .bucket (), blobId .name (), (Long ) value );
278- return StorageRpc .Tuple .of (blobInfo .toBuilder ().blobId (blobId ).build (),
279- Storage .BlobWriteOption .generationNotMatch ());
280- case IF_METAGENERATION_MATCH :
281- return StorageRpc .Tuple .of (blobInfo .toBuilder ().metageneration ((Long ) value ).build (),
282- Storage .BlobWriteOption .metagenerationMatch ());
283- case IF_METAGENERATION_NOT_MATCH :
284- return StorageRpc .Tuple .of (blobInfo .toBuilder ().metageneration ((Long ) value ).build (),
285- Storage .BlobWriteOption .metagenerationNotMatch ());
286- case IF_MD5_MATCH :
287- return StorageRpc .Tuple .of (blobInfo .toBuilder ().md5 ((String ) value ).build (),
288- Storage .BlobWriteOption .md5Match ());
289- case IF_CRC32C_MATCH :
290- return StorageRpc .Tuple .of (blobInfo .toBuilder ().crc32c ((String ) value ).build (),
291- Storage .BlobWriteOption .crc32cMatch ());
292- default :
293- throw new AssertionError ("Unexpected enum value" );
294- }
295- }
296-
297- private BlobWriteOption (Storage .BlobWriteOption .Option option , Object value ) {
298- this .option = option ;
299- this .value = value ;
300- }
301-
302- @ Override
303- public int hashCode () {
304- return Objects .hash (option , value );
305- }
306-
307- @ Override
308- public boolean equals (Object obj ) {
309- if (obj == null ) {
310- return false ;
311- }
312- if (!(obj instanceof BlobWriteOption )) {
313- return false ;
314- }
315- final BlobWriteOption other = (BlobWriteOption ) obj ;
316- return this .option == other .option && Objects .equals (this .value , other .value );
317- }
318-
319- /**
320- * Returns an option for specifying blob's predefined ACL configuration.
321- */
322- public static BlobWriteOption predefinedAcl (Storage .PredefinedAcl acl ) {
323- return new BlobWriteOption (Storage .BlobWriteOption .Option .PREDEFINED_ACL , acl );
324- }
325-
326- /**
327- * Returns an option that causes an operation to succeed only if the target blob does not exist.
328- * This option can not be provided together with {@link #generationMatch(long)} or
329- * {@link #generationNotMatch(long)}.
330- */
331- public static BlobWriteOption doesNotExist () {
332- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_GENERATION_MATCH , 0L );
333- }
334-
335- /**
336- * Returns an option for blob's data generation match. If this option is used the request will
337- * fail if generation does not match the provided value. This option can not be provided
338- * together with {@link #generationNotMatch(long)} or {@link #doesNotExist()}.
339- */
340- public static BlobWriteOption generationMatch (long generation ) {
341- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_GENERATION_MATCH , generation );
342- }
343-
344- /**
345- * Returns an option for blob's data generation mismatch. If this option is used the request
346- * will fail if generation matches the provided value. This option can not be provided
347- * together with {@link #generationMatch(long)} or {@link #doesNotExist()}.
348- */
349- public static BlobWriteOption generationNotMatch (long generation ) {
350- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_GENERATION_NOT_MATCH ,
351- generation );
352- }
353-
354- /**
355- * Returns an option for blob's metageneration match. If this option is used the request will
356- * fail if metageneration does not match the provided value. This option can not be provided
357- * together with {@link #metagenerationNotMatch(long)}.
358- */
359- public static BlobWriteOption metagenerationMatch (long metageneration ) {
360- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_METAGENERATION_MATCH ,
361- metageneration );
362- }
363-
364- /**
365- * Returns an option for blob's metageneration mismatch. If this option is used the request will
366- * fail if metageneration matches the provided value. This option can not be provided together
367- * with {@link #metagenerationMatch(long)}.
368- */
369- public static BlobWriteOption metagenerationNotMatch (long metageneration ) {
370- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_METAGENERATION_NOT_MATCH ,
371- metageneration );
372- }
373-
374- /**
375- * Returns an option for blob's data MD5 hash match. If this option is used the request will
376- * fail if blobs' data MD5 hash does not match the provided value.
377- */
378- public static BlobWriteOption md5Match (String md5 ) {
379- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_MD5_MATCH , md5 );
380- }
381-
382- /**
383- * Returns an option for blob's data CRC32C checksum match. If this option is used the request
384- * will fail if blobs' data CRC32C checksum does not match the provided value.
385- */
386- public static BlobWriteOption crc32cMatch (String crc32c ) {
387- return new BlobWriteOption (Storage .BlobWriteOption .Option .IF_CRC32C_MATCH , crc32c );
388- }
389-
390- static StorageRpc .Tuple <BlobInfo , Storage .BlobWriteOption []> toWriteOptions (
391- BlobInfo info , BlobWriteOption ... options ) {
392- Set <Storage .BlobWriteOption .Option > optionSet =
393- Sets .immutableEnumSet (Lists .transform (Arrays .asList (options ), TO_ENUM ));
394- checkArgument (!(optionSet .contains (Storage .BlobWriteOption .Option .IF_METAGENERATION_NOT_MATCH )
395- && optionSet .contains (Storage .BlobWriteOption .Option .IF_METAGENERATION_MATCH )),
396- "metagenerationMatch and metagenerationNotMatch options can not be both provided" );
397- checkArgument (!(optionSet .contains (Storage .BlobWriteOption .Option .IF_GENERATION_NOT_MATCH )
398- && optionSet .contains (Storage .BlobWriteOption .Option .IF_GENERATION_MATCH )),
399- "Only one option of generationMatch, doesNotExist or generationNotMatch can be provided" );
400- Storage .BlobWriteOption [] convertedOptions = new Storage .BlobWriteOption [options .length ];
401- BlobInfo writeInfo = info ;
402- int index = 0 ;
403- for (BlobWriteOption option : options ) {
404- StorageRpc .Tuple <BlobInfo , Storage .BlobWriteOption > write = option .toWriteOption (writeInfo );
405- writeInfo = write .x ();
406- convertedOptions [index ++] = write .y ();
407- }
408- return StorageRpc .Tuple .of (writeInfo , convertedOptions );
409- }
410- }
411-
412127 /**
413128 * Builder for {@code Bucket}.
414129 */
@@ -642,9 +357,7 @@ public List<Blob> get(String blobName1, String blobName2, String... blobNames) {
642357 public Blob create (String blob , byte [] content , String contentType , BlobTargetOption ... options ) {
643358 BlobInfo blobInfo = BlobInfo .builder (BlobId .of (name (), blob ))
644359 .contentType (MoreObjects .firstNonNull (contentType , Storage .DEFAULT_CONTENT_TYPE )).build ();
645- StorageRpc .Tuple <BlobInfo , Storage .BlobTargetOption []> target =
646- BlobTargetOption .toTargetOptions (blobInfo , options );
647- return storage .create (target .x (), content , target .y ());
360+ return storage .create (blobInfo , content , options );
648361 }
649362
650363 /**
@@ -664,9 +377,7 @@ public Blob create(String blob, InputStream content, String contentType,
664377 BlobWriteOption ... options ) {
665378 BlobInfo blobInfo = BlobInfo .builder (BlobId .of (name (), blob ))
666379 .contentType (MoreObjects .firstNonNull (contentType , Storage .DEFAULT_CONTENT_TYPE )).build ();
667- StorageRpc .Tuple <BlobInfo , Storage .BlobWriteOption []> write =
668- BlobWriteOption .toWriteOptions (blobInfo , options );
669- return storage .create (write .x (), content , write .y ());
380+ return storage .create (blobInfo , content , options );
670381 }
671382
672383 /**
0 commit comments