From c87bd8db392749a797a5db4fe3d32a455142731f Mon Sep 17 00:00:00 2001 From: aozarov Date: Fri, 8 May 2015 17:17:07 -0700 Subject: [PATCH 1/2] change sourceOptions --- .../gcloud/examples/StorageExample.java | 18 +- .../google/gcloud/storage/BatchRequest.java | 8 +- .../google/gcloud/storage/BatchResponse.java | 8 +- .../com/google/gcloud/storage/Option.java | 2 +- .../google/gcloud/storage/StorageService.java | 154 +++++++++++++----- .../gcloud/storage/StorageServiceImpl.java | 113 +++++++------ 6 files changed, 203 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/google/gcloud/examples/StorageExample.java b/src/main/java/com/google/gcloud/examples/StorageExample.java index 442e3b84d13b..b545e8323dcd 100644 --- a/src/main/java/com/google/gcloud/examples/StorageExample.java +++ b/src/main/java/com/google/gcloud/examples/StorageExample.java @@ -117,14 +117,14 @@ private static class InfoAction extends BlobsAction { public void run(StorageService storage, Blob... blobs) { if (blobs.length == 1) { if (blobs[0].name().isEmpty()) { - System.out.println(storage.get(Bucket.of(blobs[0].bucket()))); + System.out.println(storage.get(blobs[0].bucket())); } else { - System.out.println(storage.get(blobs[0])); + System.out.println(storage.get(blobs[0].bucket(), blobs[0].name())); } } else { BatchRequest.Builder batch = BatchRequest.builder(); for (Blob blob : blobs) { - batch.get(blob); + batch.get(blob.bucket(), blob.name()); } BatchResponse response = storage.apply(batch.build()); System.out.println(response.gets()); @@ -149,11 +149,11 @@ private static class DeleteAction extends BlobsAction { @Override public void run(StorageService storage, Blob... blobs) { if (blobs.length == 1) { - System.out.println(storage.delete(blobs[0])); + System.out.println(storage.delete(blobs[0].bucket(), blobs[0].name())); } else { BatchRequest.Builder batch = BatchRequest.builder(); for (Blob blob : blobs) { - batch.delete(blob); + batch.delete(blob.bucket(), blob.name()); } BatchResponse response = storage.apply(batch.build()); System.out.println(response.deletes()); @@ -236,7 +236,7 @@ private static class DownloadAction extends StorageAction> { @Override public void run(StorageService storage, Tuple tuple) throws IOException { - Blob blob = storage.get(tuple.x()); + Blob blob = storage.get(tuple.x().bucket(), tuple.x().name()); if (blob == null) { System.out.println("No such object"); return; @@ -246,9 +246,9 @@ public void run(StorageService storage, Tuple tuple) throws IOExcept writeTo = new PrintStream(new FileOutputStream(tuple.y().toFile())); } if (blob.size() < 1024) { - writeTo.write(storage.load(blob)); + writeTo.write(storage.load(blob.bucket(), blob.name())); } else { - try (BlobReadChannel reader = storage.reader(blob)) { + try (BlobReadChannel reader = storage.reader(blob.bucket(), blob.name())) { WritableByteChannel channel = Channels.newChannel(writeTo); ByteBuffer bytes = ByteBuffer.allocate(64 * 1024); while (reader.read(bytes) > 0) { @@ -299,7 +299,7 @@ CopyRequest parse(String... args) { if (args.length != 4) { throw new IllegalArgumentException(); } - return CopyRequest.of(Blob.of(args[0], args[1]), Blob.of(args[2], args[3])); + return CopyRequest.of(args[0], args[1], Blob.of(args[2], args[3])); } @Override diff --git a/src/main/java/com/google/gcloud/storage/BatchRequest.java b/src/main/java/com/google/gcloud/storage/BatchRequest.java index 84122a83d4ff..9fbee876cf45 100644 --- a/src/main/java/com/google/gcloud/storage/BatchRequest.java +++ b/src/main/java/com/google/gcloud/storage/BatchRequest.java @@ -46,8 +46,8 @@ private Builder() {} /** * Delete the given blob. */ - public void delete(Blob blob, BlobSourceOption... options) { - toDelete.put(blob, options); + public void delete(String bucket, String blob, BlobSourceOption... options) { + toDelete.put(Blob.of(bucket, blob), options); } /** @@ -60,8 +60,8 @@ public void update(Blob blob, BlobTargetOption... options) { /** * Retrieve metadata for the given blob. */ - public void get(Blob blob, BlobSourceOption... options) { - toGet.put(blob, options); + public void get(String bucket, String blob, BlobSourceOption... options) { + toGet.put(Blob.of(bucket, blob), options); } public BatchRequest build() { diff --git a/src/main/java/com/google/gcloud/storage/BatchResponse.java b/src/main/java/com/google/gcloud/storage/BatchResponse.java index bef428c37a74..0cce1300bcec 100644 --- a/src/main/java/com/google/gcloud/storage/BatchResponse.java +++ b/src/main/java/com/google/gcloud/storage/BatchResponse.java @@ -27,9 +27,11 @@ */ public class BatchResponse implements Serializable { - private List> deleteResult; - private List> updateResult; - private List> getResult; + private static final long serialVersionUID = 1057416839397037706L; + + private final List> deleteResult; + private final List> updateResult; + private final List> getResult; public static class Result implements Serializable { diff --git a/src/main/java/com/google/gcloud/storage/Option.java b/src/main/java/com/google/gcloud/storage/Option.java index 7e130603b79c..798db688c8ec 100644 --- a/src/main/java/com/google/gcloud/storage/Option.java +++ b/src/main/java/com/google/gcloud/storage/Option.java @@ -36,7 +36,7 @@ class Option implements Serializable { Option(StorageRpc.Option rpcOption, Object value) { this.rpcOption = checkNotNull(rpcOption); - this.value = checkNotNull(value); + this.value = value; } StorageRpc.Option rpcOption() { diff --git a/src/main/java/com/google/gcloud/storage/StorageService.java b/src/main/java/com/google/gcloud/storage/StorageService.java index a97b20b7253a..2294704eac33 100644 --- a/src/main/java/com/google/gcloud/storage/StorageService.java +++ b/src/main/java/com/google/gcloud/storage/StorageService.java @@ -16,6 +16,7 @@ package com.google.gcloud.storage; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.ImmutableList; @@ -66,6 +67,10 @@ private BucketTargetOption(StorageRpc.Option rpcOption, Object value) { super(rpcOption, value); } + private BucketTargetOption(StorageRpc.Option rpcOption) { + this(rpcOption, null); + } + public static BucketTargetOption predefinedAcl(PredefinedAcl acl) { return new BucketTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry()); } @@ -74,8 +79,12 @@ public static BucketTargetOption predefinedDefaultObjectAcl(PredefinedAcl acl) { return new BucketTargetOption(StorageRpc.Option.PREDEFINED_DEFAULT_OBJECT_ACL, acl.entry()); } - public static BucketTargetOption metagenerationMatch(boolean match) { - return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match); + public static BucketTargetOption metagenerationMatch() { + return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH); + } + + public static BucketTargetOption metagenerationNotMatch() { + return new BucketTargetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH); } } @@ -83,12 +92,16 @@ class BucketSourceOption extends Option { private static final long serialVersionUID = 5185657617120212117L; - private BucketSourceOption(StorageRpc.Option rpcOption, Object value) { - super(rpcOption, value); + private BucketSourceOption(StorageRpc.Option rpcOption, long metageneration) { + super(rpcOption, metageneration); } - public static BucketSourceOption metagenerationMatch(boolean match) { - return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match); + public static BucketSourceOption metagenerationMatch(long metageneration) { + return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration); + } + + public static BucketSourceOption metagenerationNotMatch(long metageneration) { + return new BucketSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration); } } @@ -100,16 +113,28 @@ private BlobTargetOption(StorageRpc.Option rpcOption, Object value) { super(rpcOption, value); } + private BlobTargetOption(StorageRpc.Option rpcOption) { + this(rpcOption, null); + } + public static BlobTargetOption predefinedAcl(PredefinedAcl acl) { return new BlobTargetOption(StorageRpc.Option.PREDEFINED_ACL, acl.entry()); } - public static BlobTargetOption generationMath(boolean match) { - return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_MATCH, match); + public static BlobTargetOption generationMatch() { + return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_MATCH); } - public static BlobTargetOption metagenerationMatch(boolean match) { - return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match); + public static BlobTargetOption generationNotMatch() { + return new BlobTargetOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH); + } + + public static BlobTargetOption metagenerationMatch() { + return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_MATCH); + } + + public static BlobTargetOption metagenerationNotMatch() { + return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH); } } @@ -117,16 +142,24 @@ class BlobSourceOption extends Option { private static final long serialVersionUID = -3712768261070182991L; - private BlobSourceOption(StorageRpc.Option rpcOption, Object value) { + private BlobSourceOption(StorageRpc.Option rpcOption, long value) { super(rpcOption, value); } - public static BlobSourceOption generationMath(boolean match) { - return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, match); + public static BlobSourceOption generationMatch(long generation) { + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, generation); + } + + public static BlobSourceOption generationNotMatch(long generation) { + return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation); } - public static BlobSourceOption metagenerationMatch(boolean match) { - return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, match); + public static BlobSourceOption metagenerationMatch(long metageneration) { + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_MATCH, metageneration); + } + + public static BlobSourceOption metagenerationNotMatch(long metageneration) { + return new BlobSourceOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metageneration); } } @@ -226,8 +259,11 @@ public Builder addSource(String... blobs) { return addSource(Arrays.asList(blobs)); } - public Builder addSource(String blob, long matchGeneration) { - sourceBlobs.add(new SourceBlob(blob, matchGeneration)); + /** + * Add a source with a specific generation to match. + */ + public Builder addSource(String blob, long generation) { + sourceBlobs.add(new SourceBlob(blob, generation)); return this; } @@ -242,6 +278,7 @@ public Builder targetOptions(BlobTargetOption... options) { } public ComposeRequest build() { + checkArgument(!sourceBlobs.isEmpty()); checkNotNull(target); return new ComposeRequest(this); } @@ -278,20 +315,23 @@ class CopyRequest implements Serializable { private static final long serialVersionUID = -2606508373751748775L; - private final Blob source; + private final String sourceBucket; + private final String sourceBlob; private final List sourceOptions; private final Blob target; private final List targetOptions; public static class Builder { - private Blob source; + private String sourceBucket; + private String sourceBlob; private final Set sourceOptions = new LinkedHashSet<>(); private Blob target; private final Set targetOptions = new LinkedHashSet<>(); - public Builder source(Blob source) { - this.source = source; + public Builder source(String bucket, String blob) { + this.sourceBucket = bucket; + this.sourceBlob = blob; return this; } @@ -311,21 +351,27 @@ public Builder targetOptions(BlobTargetOption... options) { } public CopyRequest build() { - checkNotNull(source); + checkNotNull(sourceBucket); + checkNotNull(sourceBlob); checkNotNull(target); return new CopyRequest(this); } } private CopyRequest(Builder builder) { - source = checkNotNull(builder.source); + sourceBucket = checkNotNull(builder.sourceBucket); + sourceBlob = checkNotNull(builder.sourceBlob); sourceOptions = ImmutableList.copyOf(builder.sourceOptions); target = checkNotNull(builder.target); targetOptions = ImmutableList.copyOf(builder.targetOptions); } - public Blob source() { - return source; + public String sourceBucket() { + return sourceBucket; + } + + public String sourceBlob() { + return sourceBlob; } public List sourceOptions() { @@ -340,8 +386,8 @@ public List targetOptions() { return targetOptions; } - public static CopyRequest of(Blob source, Blob target) { - return builder().source(source).target(target).build(); + public static CopyRequest of(String sourceBucket, String sourceBlob, Blob target) { + return builder().source(sourceBucket, sourceBlob).target(target).build(); } public static Builder builder() { @@ -349,7 +395,6 @@ public static Builder builder() { } } - /** * Create a new bucket. * @@ -367,72 +412,107 @@ public static Builder builder() { Blob create(Blob blob, byte[] content, BlobTargetOption... options); /** - * Returns a complete bucket information. + * Return the requested bucket. * * @throws StorageServiceException upon failure */ - Bucket get(Bucket bucket, BucketSourceOption... options); + Bucket get(String bucket, BucketSourceOption... options); /** + * Return the requested blob. + * * @throws StorageServiceException upon failure */ - Blob get(Blob blob, BlobSourceOption... options); + Blob get(String bucket, String blob, BlobSourceOption... options); /** + * List the project's buckets. + * * @throws StorageServiceException upon failure */ ListResult list(BucketListOption... options); /** - * Lists blobs for a bucket. + * List the bucket's blobs. + * * @throws StorageServiceException upon failure */ ListResult list(String bucket, BlobListOption... options); /** + * Update bucket information. + * + * @return the updated bucket * @throws StorageServiceException upon failure */ Bucket update(Bucket bucket, BucketTargetOption... options); /** + * Update blob information. + * + * @return the updated blob * @throws StorageServiceException upon failure */ Blob update(Blob blob, BlobTargetOption... options); /** + * Delete the requested bucket. + * + * @return true if bucket was deleted * @throws StorageServiceException upon failure */ - boolean delete(Bucket bucket, BucketSourceOption... options); + boolean delete(String bucket, BucketSourceOption... options); /** + * Delete the requested blob. + * + * @return true if blob was deleted * @throws StorageServiceException upon failure */ - boolean delete(Blob blob, BlobSourceOption... options); + boolean delete(String bucket, String blob, BlobSourceOption... options); /** + * Send a compose request. + * + * @return the composed blob. * @throws StorageServiceException upon failure */ Blob compose(ComposeRequest composeRequest); /** + * Send a copy request. + * + * @return the copied blob. * @throws StorageServiceException upon failure */ Blob copy(CopyRequest copyRequest); /** + * Load the content of the given blob. + * + * @return the blob's content. * @throws StorageServiceException upon failure */ - byte[] load(Blob blob, BlobSourceOption... options); - + byte[] load(String bucket, String blob, BlobSourceOption... options); + /** + * Send a batch request. + * + * @return the batch response + * @throws StorageServiceException upon failure + */ BatchResponse apply(BatchRequest batchRequest); /** + * Return a channel for reading the blob's content. + * * @throws StorageServiceException upon failure */ - BlobReadChannel reader(Blob blob, BlobSourceOption... options); + BlobReadChannel reader(String bucket, String blob, BlobSourceOption... options); /** + * Create a blob and return a channel for writing its content. + * * @throws StorageServiceException upon failure */ BlobWriteChannel writer(Blob blob, BlobTargetOption... options); diff --git a/src/main/java/com/google/gcloud/storage/StorageServiceImpl.java b/src/main/java/com/google/gcloud/storage/StorageServiceImpl.java index fa911e431997..e577cf2c0aa3 100644 --- a/src/main/java/com/google/gcloud/storage/StorageServiceImpl.java +++ b/src/main/java/com/google/gcloud/storage/StorageServiceImpl.java @@ -18,6 +18,15 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.gcloud.RetryHelper.runWithRetries; +import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER; +import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_NOT_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_METAGENERATION_NOT_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH; +import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH; import static java.util.concurrent.Executors.callable; import com.google.api.services.storage.model.StorageObject; @@ -108,9 +117,9 @@ public StorageObject call() { } @Override - public Bucket get(Bucket bucket, BucketSourceOption... options) { - final com.google.api.services.storage.model.Bucket bucketPb = bucket.toPb(); - final Map optionsMap = optionMap(bucket, options); + public Bucket get(String bucket, BucketSourceOption... options) { + final com.google.api.services.storage.model.Bucket bucketPb = Bucket.of(bucket).toPb(); + final Map optionsMap = optionMap(options); return Bucket.fromPb(runWithRetries( new Callable() { @Override @@ -121,9 +130,9 @@ public com.google.api.services.storage.model.Bucket call() { } @Override - public Blob get(Blob blob, BlobSourceOption... options) { - final StorageObject storedObject = blob.toPb(); - final Map optionsMap = optionMap(blob, options); + public Blob get(String bucket, String blob, BlobSourceOption... options) { + final StorageObject storedObject = Blob.of(bucket, blob).toPb(); + final Map optionsMap = optionMap(options); return Blob.fromPb(runWithRetries(new Callable() { @Override public StorageObject call() { @@ -134,7 +143,7 @@ public StorageObject call() { @Override public ListResult list(BucketListOption... options) { - final Map optionsMap = optionMap(null, null, options); + final Map optionsMap = optionMap(options); Tuple> result = runWithRetries( new Callable>>() { @Override @@ -153,7 +162,7 @@ public Bucket apply(com.google.api.services.storage.model.Bucket bucketPb) { @Override public ListResult list(final String bucket, BlobListOption... options) { - final Map optionsMap = optionMap(null, null, options); + final Map optionsMap = optionMap(options); Tuple> result = runWithRetries( new Callable>>() { @Override @@ -196,9 +205,9 @@ public StorageObject call() { } @Override - public boolean delete(Bucket bucket, BucketSourceOption... options) { - final com.google.api.services.storage.model.Bucket bucketPb = bucket.toPb(); - final Map optionsMap = optionMap(bucket, options); + public boolean delete(String bucket, BucketSourceOption... options) { + final com.google.api.services.storage.model.Bucket bucketPb = Bucket.of(bucket).toPb(); + final Map optionsMap = optionMap(options); return runWithRetries(new Callable() { @Override public Boolean call() { @@ -208,9 +217,9 @@ public Boolean call() { } @Override - public boolean delete(Blob blob, BlobSourceOption... options) { - final StorageObject storageObject = blob.toPb(); - final Map optionsMap = optionMap(blob, options); + public boolean delete(String bucket, String blob, BlobSourceOption... options) { + final StorageObject storageObject = Blob.of(bucket, blob).toPb(); + final Map optionsMap = optionMap(options); return runWithRetries(new Callable() { @Override public Boolean call() { @@ -240,10 +249,11 @@ public StorageObject call() { @Override public Blob copy(CopyRequest copyRequest) { - final StorageObject source = copyRequest.source().toPb(); + final StorageObject source = + Blob.of(copyRequest.sourceBucket(), copyRequest.sourceBlob()).toPb(); copyRequest.sourceOptions(); - final Map sourceOptions = optionMap(copyRequest.source().generation(), - copyRequest.source().metageneration(), copyRequest.sourceOptions(), true); + final Map sourceOptions = + optionMap(null, null, copyRequest.sourceOptions(), true); final StorageObject target = copyRequest.target().toPb(); final Map targetOptions = optionMap(copyRequest.target().generation(), copyRequest.target().metageneration(), copyRequest.targetOptions()); @@ -256,9 +266,9 @@ public StorageObject call() { } @Override - public byte[] load(Blob blob, BlobSourceOption... options) { - final StorageObject storageObject = blob.toPb(); - final Map optionsMap = optionMap(blob, options); + public byte[] load(String bucket, String blob, BlobSourceOption... options) { + final StorageObject storageObject = Blob.of(bucket, blob).toPb(); + final Map optionsMap = optionMap(options); return runWithRetries(new Callable() { @Override public byte[] call() { @@ -421,9 +431,9 @@ public byte[] call() { } @Override - public BlobReadChannel reader(Blob blob, BlobSourceOption... options) { - Map optionsMap = optionMap(blob, options); - return new BlobReadChannelImpl(options(), blob, optionsMap); + public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... options) { + Map optionsMap = optionMap(options); + return new BlobReadChannelImpl(options(), Blob.of(bucket, blob), optionsMap); } private static class BlobWriterChannelImpl implements BlobWriteChannel { @@ -559,35 +569,46 @@ public BlobWriteChannel writer(Blob blob, BlobTargetOption... options) { Object prev = temp.put(option.rpcOption(), option.value()); checkArgument(prev == null, "Duplicate option %s", option); } - Boolean value = (Boolean) temp.remove(StorageRpc.Option.DELIMITER); + Boolean value = (Boolean) temp.remove(DELIMITER); if (Boolean.TRUE.equals(value)) { - temp.put(StorageRpc.Option.DELIMITER, options().pathDelimiter()); + temp.put(DELIMITER, options().pathDelimiter()); } - value = (Boolean) temp.remove(StorageRpc.Option.IF_GENERATION_MATCH); - if (value != null) { - checkArgument(generation != null, "missing generation value"); - if (value) { - temp.put(useAsSource ? StorageRpc.Option.IF_SOURCE_GENERATION_MATCH - : StorageRpc.Option.IF_GENERATION_MATCH, generation); - } else { - temp.put(useAsSource ? StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH - : StorageRpc.Option.IF_GENERATION_NOT_MATCH, generation); - } - } - value = (Boolean) temp.remove(StorageRpc.Option.IF_METAGENERATION_MATCH); - if (value != null) { - checkArgument(metaGeneration != null, "missing metaGeneration value"); - if (value) { - temp.put(useAsSource ? StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH - : StorageRpc.Option.IF_METAGENERATION_MATCH, metaGeneration); - } else { - temp.put(useAsSource ? StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH - : StorageRpc.Option.IF_METAGENERATION_NOT_MATCH, metaGeneration); - } + if (useAsSource) { + addToOptionMap(IF_GENERATION_MATCH, IF_SOURCE_GENERATION_MATCH, generation, temp); + addToOptionMap(IF_GENERATION_NOT_MATCH, IF_SOURCE_GENERATION_NOT_MATCH, generation, temp); + addToOptionMap(IF_METAGENERATION_MATCH, IF_SOURCE_METAGENERATION_MATCH, metaGeneration, temp); + addToOptionMap(IF_METAGENERATION_NOT_MATCH, + IF_SOURCE_METAGENERATION_NOT_MATCH, metaGeneration, temp); + } else { + addToOptionMap(IF_GENERATION_MATCH, generation, temp); + addToOptionMap(IF_GENERATION_NOT_MATCH, generation, temp); + addToOptionMap(IF_METAGENERATION_MATCH, metaGeneration, temp); + addToOptionMap(IF_METAGENERATION_NOT_MATCH, metaGeneration, temp); } return ImmutableMap.copyOf(temp); } + private static void addToOptionMap(StorageRpc.Option option, T defaultValue, + Map map) { + addToOptionMap(option, option, defaultValue, map); + } + + private static void addToOptionMap(StorageRpc.Option getOption, StorageRpc.Option putOption, + T defaultValue, Map map) { + if (map.containsKey(getOption)) { + @SuppressWarnings("unchecked") + T value = (T) map.remove(getOption); + checkArgument(value != null || defaultValue != null, + "Option " + getOption.value() + " is missing a value"); + value = MoreObjects.firstNonNull(value, defaultValue); + map.put(putOption, value); + } + } + + private Map optionMap(Option... options) { + return optionMap(null, null, Arrays.asList(options)); + } + private Map optionMap(Long generation, Long metaGeneration, Option... options) { return optionMap(generation, metaGeneration, Arrays.asList(options)); From 92c6df8f5110c7088d7f66af0dc4ca317107c697 Mon Sep 17 00:00:00 2001 From: aozarov Date: Fri, 8 May 2015 17:41:10 -0700 Subject: [PATCH 2/2] some doc --- .../java/com/google/gcloud/storage/package-info.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/gcloud/storage/package-info.java b/src/main/java/com/google/gcloud/storage/package-info.java index 0aa916666e8c..e938755271eb 100644 --- a/src/main/java/com/google/gcloud/storage/package-info.java +++ b/src/main/java/com/google/gcloud/storage/package-info.java @@ -17,7 +17,15 @@ /** * A client to Google Cloud Storage. * - * @see Google Cloud Storageg + *

A simple usage example: + *

{@code
+ * StorageServiceOptions options = StorageServiceOptions.builder().projectId("project").build();
+ * StorageService storage = StorageServiceFactory.instance().get(options);
+ * byte[] content = readContent();
+ * Blob blob = storage.create(Blob.of("bucket", "blob_name"), content);
+ * } 
+ * + * @see Google Cloud Storage */ package com.google.gcloud.storage;