Skip to content

Commit ba4bfa6

Browse files
committed
Add field selection to Bucket.exist
1 parent f11c05a commit ba4bfa6

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toGetOptions;
22+
import static com.google.gcloud.storage.Bucket.BucketSourceOption.toSourceOptions;
2123

2224
import com.google.common.base.Function;
2325
import com.google.common.base.MoreObjects;
@@ -35,6 +37,7 @@
3537
import java.io.ObjectInputStream;
3638
import java.io.Serializable;
3739
import java.util.ArrayList;
40+
import java.util.Arrays;
3841
import java.util.Collections;
3942
import java.util.Iterator;
4043
import java.util.List;
@@ -218,8 +221,11 @@ public BucketInfo info() {
218221
* @return true if this bucket exists, false otherwise
219222
* @throws StorageException upon failure
220223
*/
221-
public boolean exists() {
222-
return storage.get(info.name()) != null;
224+
public boolean exists(BucketSourceOption... options) {
225+
int length = options.length;
226+
Storage.BucketGetOption[] getOptions = Arrays.copyOf(toGetOptions(info, options), length + 1);
227+
getOptions[length] = Storage.BucketGetOption.fields();
228+
return storage.get(info.name(), getOptions) != null;
223229
}
224230

225231
/**
@@ -230,8 +236,7 @@ public boolean exists() {
230236
* @throws StorageException upon failure
231237
*/
232238
public Bucket reload(BucketSourceOption... options) {
233-
return new Bucket(storage, storage.get(info.name(),
234-
BucketSourceOption.toGetOptions(info, options)));
239+
return new Bucket(storage, storage.get(info.name(), toGetOptions(info, options)));
235240
}
236241

237242
/**
@@ -259,7 +264,7 @@ public Bucket update(BucketInfo bucketInfo, BucketTargetOption... options) {
259264
* @throws StorageException upon failure
260265
*/
261266
public boolean delete(BucketSourceOption... options) {
262-
return storage.delete(info.name(), BucketSourceOption.toSourceOptions(info, options));
267+
return storage.delete(info.name(), toSourceOptions(info, options));
263268
}
264269

265270
/**

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ public void testInfo() throws Exception {
7575

7676
@Test
7777
public void testExists_True() throws Exception {
78-
expect(storage.get(BUCKET_INFO.name())).andReturn(BUCKET_INFO);
78+
Storage.BucketGetOption[] expectedOptions = {Storage.BucketGetOption.fields()};
79+
expect(storage.get(BUCKET_INFO.name(), expectedOptions)).andReturn(BUCKET_INFO);
7980
replay(storage);
8081
assertTrue(bucket.exists());
8182
}
8283

8384
@Test
8485
public void testExists_False() throws Exception {
85-
expect(storage.get(BUCKET_INFO.name())).andReturn(null);
86+
Storage.BucketGetOption[] expectedOptions = {Storage.BucketGetOption.fields()};
87+
expect(storage.get(BUCKET_INFO.name(), expectedOptions)).andReturn(null);
8688
replay(storage);
8789
assertFalse(bucket.exists());
8890
}

0 commit comments

Comments
 (0)