1818
1919import static com .google .common .base .Preconditions .checkArgument ;
2020import static com .google .common .base .Preconditions .checkNotNull ;
21- import static com .google .gcloud .storage .Blob .BlobSourceOption .convert ;
21+ import static com .google .gcloud .storage .Blob .BlobSourceOption .toSourceOptions ;
22+ import static com .google .gcloud .storage .Blob .BlobSourceOption .toGetOptions ;
2223
2324import com .google .common .base .Function ;
2425import com .google .common .collect .Lists ;
2930import com .google .gcloud .storage .Storage .SignUrlOption ;
3031
3132import java .net .URL ;
33+ import java .util .Arrays ;
3234import java .util .Collections ;
3335import java .util .List ;
3436import java .util .Objects ;
@@ -56,7 +58,7 @@ private BlobSourceOption(StorageRpc.Option rpcOption) {
5658 super (rpcOption , null );
5759 }
5860
59- private Storage .BlobSourceOption convert (BlobInfo blobInfo ) {
61+ private Storage .BlobSourceOption toSourceOptions (BlobInfo blobInfo ) {
6062 switch (rpcOption ()) {
6163 case IF_GENERATION_MATCH :
6264 return Storage .BlobSourceOption .generationMatch (blobInfo .generation ());
@@ -71,6 +73,21 @@ private Storage.BlobSourceOption convert(BlobInfo blobInfo) {
7173 }
7274 }
7375
76+ private Storage .BlobGetOption toGetOption (BlobInfo blobInfo ) {
77+ switch (rpcOption ()) {
78+ case IF_GENERATION_MATCH :
79+ return Storage .BlobGetOption .generationMatch (blobInfo .generation ());
80+ case IF_GENERATION_NOT_MATCH :
81+ return Storage .BlobGetOption .generationNotMatch (blobInfo .generation ());
82+ case IF_METAGENERATION_MATCH :
83+ return Storage .BlobGetOption .metagenerationMatch (blobInfo .metageneration ());
84+ case IF_METAGENERATION_NOT_MATCH :
85+ return Storage .BlobGetOption .metagenerationNotMatch (blobInfo .metageneration ());
86+ default :
87+ throw new AssertionError ("Unexpected enum value" );
88+ }
89+ }
90+
7491 public static BlobSourceOption generationMatch () {
7592 return new BlobSourceOption (StorageRpc .Option .IF_GENERATION_MATCH );
7693 }
@@ -87,11 +104,21 @@ public static BlobSourceOption metagenerationNotMatch() {
87104 return new BlobSourceOption (StorageRpc .Option .IF_METAGENERATION_NOT_MATCH );
88105 }
89106
90- static Storage .BlobSourceOption [] convert (BlobInfo blobInfo , BlobSourceOption ... options ) {
107+ static Storage .BlobSourceOption [] toSourceOptions (BlobInfo blobInfo ,
108+ BlobSourceOption ... options ) {
91109 Storage .BlobSourceOption [] convertedOptions = new Storage .BlobSourceOption [options .length ];
92110 int index = 0 ;
93111 for (BlobSourceOption option : options ) {
94- convertedOptions [index ++] = option .convert (blobInfo );
112+ convertedOptions [index ++] = option .toSourceOptions (blobInfo );
113+ }
114+ return convertedOptions ;
115+ }
116+
117+ static Storage .BlobGetOption [] toGetOptions (BlobInfo blobInfo , BlobSourceOption ... options ) {
118+ Storage .BlobGetOption [] convertedOptions = new Storage .BlobGetOption [options .length ];
119+ int index = 0 ;
120+ for (BlobSourceOption option : options ) {
121+ convertedOptions [index ++] = option .toGetOption (blobInfo );
95122 }
96123 return convertedOptions ;
97124 }
@@ -100,7 +127,7 @@ static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption...
100127 /**
101128 * Constructs a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
102129 * to issue requests.
103- *
130+ *
104131 * @param storage the storage service used for issuing requests
105132 * @param info blob's info
106133 */
@@ -112,7 +139,7 @@ public Blob(Storage storage, BlobInfo info) {
112139 /**
113140 * Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
114141 * get the latest blob information.
115- *
142+ *
116143 * @param storage the storage service used for issuing requests
117144 * @param bucket bucket's name
118145 * @param blob blob's name
@@ -126,7 +153,7 @@ public static Blob load(Storage storage, String bucket, String blob) {
126153 /**
127154 * Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
128155 * latest blob information.
129- *
156+ *
130157 * @param storage the storage service used for issuing requests
131158 * @param blobId blob's identifier
132159 * @return the {@code Blob} object or {@code null} if not found.
@@ -159,7 +186,10 @@ public BlobId id() {
159186 * @throws StorageException upon failure
160187 */
161188 public boolean exists (BlobSourceOption ... options ) {
162- return storage .get (info .blobId (), convert (info , options )) != null ;
189+ int length = options .length ;
190+ Storage .BlobGetOption [] getOptions = Arrays .copyOf (toGetOptions (info , options ), length + 1 );
191+ getOptions [length ] = Storage .BlobGetOption .fields ();
192+ return storage .get (info .blobId (), getOptions ) != null ;
163193 }
164194
165195 /**
@@ -180,7 +210,7 @@ public byte[] content(Storage.BlobSourceOption... options) {
180210 * @throws StorageException upon failure
181211 */
182212 public Blob reload (BlobSourceOption ... options ) {
183- return new Blob (storage , storage .get (info .blobId (), convert (info , options )));
213+ return new Blob (storage , storage .get (info .blobId (), toGetOptions (info , options )));
184214 }
185215
186216 /**
@@ -224,7 +254,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
224254 */
225255 public CopyWriter copyTo (BlobId targetBlob , BlobSourceOption ... options ) {
226256 CopyRequest copyRequest = CopyRequest .builder ().source (info .bucket (), info .name ())
227- .sourceOptions (convert (info , options )).target (targetBlob ).build ();
257+ .sourceOptions (toSourceOptions (info , options )).target (targetBlob ).build ();
228258 return storage .copy (copyRequest );
229259 }
230260
@@ -236,7 +266,7 @@ public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
236266 * @throws StorageException upon failure
237267 */
238268 public boolean delete (BlobSourceOption ... options ) {
239- return storage .delete (info .blobId (), convert (info , options ));
269+ return storage .delete (info .blobId (), toSourceOptions (info , options ));
240270 }
241271
242272 /**
@@ -275,7 +305,7 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
275305 * @throws StorageException upon failure
276306 */
277307 public BlobReadChannel reader (BlobSourceOption ... options ) {
278- return storage .reader (info .blobId (), convert (info , options ));
308+ return storage .reader (info .blobId (), toSourceOptions (info , options ));
279309 }
280310
281311 /**
0 commit comments