1717package com .google .gcloud .storage ;
1818
1919import static com .google .api .client .repackaged .com .google .common .base .Preconditions .checkNotNull ;
20+ import static com .google .common .collect .Iterables .transform ;
21+ import static com .google .common .collect .Lists .newArrayList ;
2022
23+ import com .google .api .client .util .DateTime ;
2124import com .google .api .services .storage .model .Bucket ;
25+ import com .google .api .services .storage .model .BucketAccessControl ;
26+ import com .google .api .services .storage .model .ObjectAccessControl ;
27+ import com .google .common .base .Function ;
2228import com .google .common .base .MoreObjects ;
2329import com .google .common .collect .ImmutableList ;
2430import com .google .common .collect .ImmutableMap ;
31+ import com .google .common .collect .Iterables ;
2532
2633import java .io .Serializable ;
2734import java .util .List ;
@@ -35,7 +42,7 @@ public final class BucketInfo implements Serializable {
3542 private final String etag ;
3643 private final long createTime ;
3744 private final long metageneration ;
38- private final Cors cors ;
45+ private final ImmutableList < Cors > cors ;
3946 private final ImmutableList <Acl > acl ;
4047 private final ImmutableList <Acl > defaultAcl ;
4148 private final Location location ;
@@ -84,6 +91,11 @@ public static StorageClass of(String value) {
8491 return option == null ? new StorageClass (value ) : option .storageClass ;
8592 }
8693
94+ @ Override
95+ public String toString () {
96+ return value ();
97+ }
98+
8799 public String value () {
88100 return value ;
89101 }
@@ -134,6 +146,11 @@ public static Location of(String value) {
134146 return option == null ? new Location (value ) : option .location ;
135147 }
136148
149+ @ Override
150+ public String toString () {
151+ return value ();
152+ }
153+
137154 public String value () {
138155 return value ;
139156 }
@@ -148,9 +165,9 @@ public final static class Builder {
148165 private String etag ;
149166 private Long createTime ;
150167 private Long metageneration ;
151- private Cors cors ;
152- private Iterable <Acl > acl ;
153- private Iterable <Acl > defaultAcl ;
168+ private Iterable < Cors > cors = ImmutableList . of () ;
169+ private Iterable <Acl > acl = ImmutableList . of () ;
170+ private Iterable <Acl > defaultAcl = ImmutableList . of () ;
154171
155172 Builder (String id , String name ) {
156173 this .id = id ;
@@ -182,7 +199,7 @@ Builder metageneration(Long metageneration) {
182199 return this ;
183200 }
184201
185- Builder cors (Cors cors ) {
202+ Builder cors (Iterable < Cors > cors ) {
186203 this .cors = cors ;
187204 return this ;
188205 }
@@ -210,7 +227,7 @@ private BucketInfo(Builder builder) {
210227 metageneration = MoreObjects .firstNonNull (builder .metageneration , 0L );
211228 location = builder .location ;
212229 storageClass = builder .storageClass ;
213- cors = builder .cors ;
230+ cors = ImmutableList . copyOf ( builder .cors ) ;
214231 acl = ImmutableList .copyOf (builder .acl );
215232 defaultAcl = ImmutableList .copyOf (builder .defaultAcl );
216233 }
@@ -243,15 +260,15 @@ public StorageClass storageClass() {
243260 return storageClass ;
244261 }
245262
246- public Cors cors () {
263+ public List < Cors > cors () {
247264 return cors ;
248265 }
249266
250267 public List <Acl > acl () {
251268 return acl ;
252269 }
253270
254- public List <Acl > defaultObjectAcl () {
271+ public List <Acl > defaultAcl () {
255272 return defaultAcl ;
256273 }
257274
@@ -267,33 +284,64 @@ public Builder toBuilder() {
267284 .storageClass (storageClass );
268285 }
269286
270- void fromPb (Bucket bucket ) {
287+ BucketInfo fromPb (Bucket bucket ) {
271288 Builder builder = new Builder (bucket .getId (), bucket .getName ())
272289 .createTime (bucket .getTimeCreated ().getValue ())
273290 .etag (bucket .getEtag ())
274291 .metageneration (bucket .getMetageneration ())
275292 .location (Location .of (bucket .getLocation ()))
276- .storageClass (StorageClass .of (bucket .getStorageClass ()));
277-
278- /*
279- cors = builder.cors;
280- acl = ImmutableList.copyOf(builder.acl);
281- defaultAcl = ImmutableList.copyOf(builder.defaultAcl);
282- */
283-
293+ .storageClass (StorageClass .of (bucket .getStorageClass ()))
294+ .cors (transform (bucket .getCors (), new Function <Bucket .Cors , Cors >() {
295+ @ Override public Cors apply (Bucket .Cors cors ) {
296+ return Cors .fromPb (cors );
297+ }
298+ }))
299+ .acl (transform (bucket .getAcl (), new Function <BucketAccessControl , Acl >() {
300+ @ Override public Acl apply (BucketAccessControl bucketAccessControl ) {
301+ return Acl .fromPb (bucketAccessControl );
302+ }
303+ }))
304+ .defaultAcl (transform (bucket .getDefaultObjectAcl (), new Function <ObjectAccessControl , Acl >() {
305+ @ Override public Acl apply (ObjectAccessControl objectAccessControl ) {
306+ return Acl .fromPb (objectAccessControl );
307+ }
308+ }));
309+ return builder .build ();
284310 }
285311
286- /*
287312 Bucket toPb () {
288- id = builder.id;
289- name = builder.name;
290- etag = builder.etag;
291- createTime = MoreObjects.firstNonNull(builder.createTime, 0L);
292- metageneration = MoreObjects.firstNonNull(builder.metageneration, 0L);
293- location = builder.location;
294- storageClass = builder.storageClass;
295- cors = builder.cors;
296- acl = ImmutableList.copyOf(builder.acl);
297- defaultAcl = ImmutableList.copyOf(builder.defaultAcl);
298- }*/
313+ Bucket bucket = new Bucket ();
314+ bucket .setId (id );
315+ bucket .setName (name );
316+ bucket .setEtag (etag );
317+ if (createTime > 0 ) {
318+ bucket .setTimeCreated (new DateTime (createTime ));
319+ }
320+ if (metageneration > 0 ) {
321+ bucket .setMetageneration (metageneration );
322+ }
323+ if (location != null ) {
324+ bucket .setLocation (location .value ());
325+ }
326+ if (storageClass != null ) {
327+ bucket .setStorageClass (storageClass .value ());
328+ }
329+ bucket .setCors (newArrayList (Iterables .transform (cors , new Function <Cors , Bucket .Cors >() {
330+ @ Override public Bucket .Cors apply (Cors cors ) {
331+ return cors .toPb ();
332+ }
333+ })));
334+ bucket .setAcl (newArrayList (Iterables .transform (acl , new Function <Acl , BucketAccessControl >() {
335+ @ Override public BucketAccessControl apply (Acl acl ) {
336+ return new BucketAccessControl ().setEntity (acl .toEntity ());
337+ }
338+ })));
339+ bucket .setDefaultObjectAcl (
340+ newArrayList (Iterables .transform (defaultAcl , new Function <Acl , ObjectAccessControl >() {
341+ @ Override public ObjectAccessControl apply (Acl acl ) {
342+ return new ObjectAccessControl ().setEntity (acl .toEntity ());
343+ }
344+ })));
345+ return bucket ;
346+ }
299347}
0 commit comments