Convert CopyOptions into an AutoValue

This commit requires an interface change since AutoValue lacks support
for Optional and uses Nullable annotations instead.
This commit is contained in:
Andrew Gaul 2016-02-09 20:37:57 -08:00
parent c8bbb44f37
commit 293d3f864e
6 changed files with 49 additions and 80 deletions

View File

@ -253,12 +253,11 @@ public class RegionScopedSwiftBlobStore implements BlobStore {
CopyOptions options) { CopyOptions options) {
ObjectApi objectApi = api.getObjectApi(regionId, toContainer); ObjectApi objectApi = api.getObjectApi(regionId, toContainer);
Map<String, String> userMetadata;
Map<String, String> systemMetadata = Maps.newHashMap(); Map<String, String> systemMetadata = Maps.newHashMap();
ContentMetadata contentMetadata = options.getContentMetadata().orNull(); ContentMetadata contentMetadata = options.contentMetadata();
Map<String, String> userMetadata = options.userMetadata();
if (contentMetadata != null || if (contentMetadata != null || userMetadata != null) {
options.getUserMetadata().isPresent()) {
if (contentMetadata != null) { if (contentMetadata != null) {
String contentDisposition = contentMetadata.getContentDisposition(); String contentDisposition = contentMetadata.getContentDisposition();
if (contentDisposition != null) { if (contentDisposition != null) {
@ -280,9 +279,7 @@ public class RegionScopedSwiftBlobStore implements BlobStore {
systemMetadata.put(HttpHeaders.CONTENT_TYPE, contentType); systemMetadata.put(HttpHeaders.CONTENT_TYPE, contentType);
} }
} }
if (options.getUserMetadata().isPresent()) { if (userMetadata == null) {
userMetadata = options.getUserMetadata().get();
} else {
userMetadata = Maps.newHashMap(); userMetadata = Maps.newHashMap();
} }
} else { } else {

View File

@ -71,7 +71,6 @@ import org.jclouds.s3.options.PutObjectOptions;
import org.jclouds.s3.util.S3Utils; import org.jclouds.s3.util.S3Utils;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -272,37 +271,37 @@ public class S3BlobStore extends BaseBlobStore {
CopyOptions options) { CopyOptions options) {
CopyObjectOptions s3Options = new CopyObjectOptions(); CopyObjectOptions s3Options = new CopyObjectOptions();
Optional<ContentMetadata> contentMetadata = options.getContentMetadata(); ContentMetadata contentMetadata = options.contentMetadata();
if (contentMetadata.isPresent()) { if (contentMetadata != null) {
String cacheControl = contentMetadata.get().getCacheControl(); String cacheControl = contentMetadata.getCacheControl();
if (cacheControl != null) { if (cacheControl != null) {
s3Options.cacheControl(cacheControl); s3Options.cacheControl(cacheControl);
} }
String contentDisposition = contentMetadata.get().getContentDisposition(); String contentDisposition = contentMetadata.getContentDisposition();
if (contentDisposition != null) { if (contentDisposition != null) {
s3Options.contentDisposition(contentDisposition); s3Options.contentDisposition(contentDisposition);
} }
String contentEncoding = contentMetadata.get().getContentEncoding(); String contentEncoding = contentMetadata.getContentEncoding();
if (contentEncoding != null) { if (contentEncoding != null) {
s3Options.contentEncoding(contentEncoding); s3Options.contentEncoding(contentEncoding);
} }
String contentLanguage = contentMetadata.get().getContentLanguage(); String contentLanguage = contentMetadata.getContentLanguage();
if (contentLanguage != null) { if (contentLanguage != null) {
s3Options.contentLanguage(contentLanguage); s3Options.contentLanguage(contentLanguage);
} }
String contentType = contentMetadata.get().getContentType(); String contentType = contentMetadata.getContentType();
if (contentType != null) { if (contentType != null) {
s3Options.contentType(contentType); s3Options.contentType(contentType);
} }
} }
Optional<Map<String, String>> userMetadata = options.getUserMetadata(); Map<String, String> userMetadata = options.userMetadata();
if (userMetadata.isPresent()) { if (userMetadata != null) {
s3Options.overrideMetadataWith(userMetadata.get()); s3Options.overrideMetadataWith(userMetadata);
} }
return sync.copyObject(fromContainer, fromName, toContainer, toName, s3Options).getETag(); return sync.copyObject(fromContainer, fromName, toContainer, toName, s3Options).getETag();

View File

@ -541,8 +541,8 @@ public final class LocalBlobStore implements BlobStore {
builder.contentLength(contentLength); builder.contentLength(contentLength);
} }
if (options.getContentMetadata().isPresent()) { ContentMetadata contentMetadata = options.contentMetadata();
ContentMetadata contentMetadata = options.getContentMetadata().get(); if (contentMetadata != null) {
String cacheControl = contentMetadata.getCacheControl(); String cacheControl = contentMetadata.getCacheControl();
if (cacheControl != null) { if (cacheControl != null) {
builder.cacheControl(cacheControl); builder.cacheControl(cacheControl);
@ -570,9 +570,10 @@ public final class LocalBlobStore implements BlobStore {
.contentLanguage(metadata.getContentLanguage()) .contentLanguage(metadata.getContentLanguage())
.contentType(metadata.getContentType()); .contentType(metadata.getContentType());
} }
Optional<Map<String, String>> userMetadata = options.getUserMetadata();
if (userMetadata.isPresent()) { Map<String, String> userMetadata = options.userMetadata();
builder.userMetadata(userMetadata.get()); if (userMetadata != null) {
builder.userMetadata(userMetadata);
} else { } else {
builder.userMetadata(blob.getMetadata().getUserMetadata()); builder.userMetadata(blob.getMetadata().getUserMetadata());
} }

View File

@ -52,7 +52,6 @@ import org.jclouds.io.PayloadSlicer;
import org.jclouds.util.Closeables2; import org.jclouds.util.Closeables2;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -270,8 +269,8 @@ public abstract class BaseBlobStore implements BlobStore {
} }
ContentMetadata metadata; ContentMetadata metadata;
if (options.getContentMetadata().isPresent()) { if (options.contentMetadata() != null) {
metadata = options.getContentMetadata().get(); metadata = options.contentMetadata();
} else { } else {
metadata = blob.getMetadata().getContentMetadata(); metadata = blob.getMetadata().getContentMetadata();
} }
@ -281,9 +280,9 @@ public abstract class BaseBlobStore implements BlobStore {
.contentLanguage(metadata.getContentLanguage()) .contentLanguage(metadata.getContentLanguage())
.contentType(metadata.getContentType()); .contentType(metadata.getContentType());
Optional<Map<String, String>> userMetadata = options.getUserMetadata(); Map<String, String> userMetadata = options.userMetadata();
if (userMetadata.isPresent()) { if (userMetadata != null) {
builder.userMetadata(userMetadata.get()); builder.userMetadata(userMetadata);
} else { } else {
builder.userMetadata(blob.getMetadata().getUserMetadata()); builder.userMetadata(blob.getMetadata().getUserMetadata());
} }

View File

@ -17,59 +17,33 @@
package org.jclouds.blobstore.options; package org.jclouds.blobstore.options;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map; import java.util.Map;
import org.jclouds.io.ContentMetadata; import org.jclouds.io.ContentMetadata;
import org.jclouds.javax.annotation.Nullable;
import com.google.auto.value.AutoValue;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
@AutoValue
@Beta @Beta
public final class CopyOptions { public abstract class CopyOptions {
public static final CopyOptions NONE = builder().build(); public static final CopyOptions NONE = builder().build();
private final Optional<ContentMetadata> contentMetadata;
private final Optional<Map<String, String>> userMetadata;
private CopyOptions(Builder builder) {
this.contentMetadata = Optional.fromNullable(builder.contentMetadata);
this.userMetadata = Optional.fromNullable(builder.userMetadata);
}
public Optional<ContentMetadata> getContentMetadata() {
return contentMetadata;
}
public Optional<Map<String, String>> getUserMetadata() {
return userMetadata;
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new AutoValue_CopyOptions.Builder();
} }
public static class Builder { @Nullable
ContentMetadata contentMetadata; public abstract ContentMetadata contentMetadata();
Map<String, String> userMetadata; @Nullable
public abstract Map<String, String> userMetadata();
Builder() { @AutoValue.Builder
} public abstract static class Builder {
public abstract Builder contentMetadata(ContentMetadata contentMetadata);
public abstract Builder userMetadata(Map<String, String> userMetadata);
public Builder contentMetadata(ContentMetadata contentMetadata) { public abstract CopyOptions build();
this.contentMetadata = checkNotNull(contentMetadata, "contentMetadata");
return this;
}
public Builder userMetadata(Map<String, String> userMetadata) {
this.userMetadata = ImmutableMap.copyOf(userMetadata);
return this;
}
public CopyOptions build() {
return new CopyOptions(this);
}
} }
} }

View File

@ -69,7 +69,6 @@ import org.jclouds.io.MutableContentMetadata;
import org.jclouds.io.PayloadSlicer; import org.jclouds.io.PayloadSlicer;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -237,39 +236,39 @@ public class AzureBlobStore extends BaseBlobStore {
CopyOptions options) { CopyOptions options) {
CopyBlobOptions.Builder azureOptions = CopyBlobOptions.builder(); CopyBlobOptions.Builder azureOptions = CopyBlobOptions.builder();
Optional<Map<String, String>> userMetadata = options.getUserMetadata(); Map<String, String> userMetadata = options.userMetadata();
if (userMetadata.isPresent()) { if (userMetadata != null) {
azureOptions.overrideUserMetadata(userMetadata.get()); azureOptions.overrideUserMetadata(userMetadata);
} }
URI source = context.getSigner().signGetBlob(fromContainer, fromName).getEndpoint(); URI source = context.getSigner().signGetBlob(fromContainer, fromName).getEndpoint();
String eTag = sync.copyBlob(source, toContainer, toName, azureOptions.build()); String eTag = sync.copyBlob(source, toContainer, toName, azureOptions.build());
Optional<ContentMetadata> contentMetadata = options.getContentMetadata(); ContentMetadata contentMetadata = options.contentMetadata();
if (contentMetadata.isPresent()) { if (contentMetadata != null) {
ContentMetadataBuilder builder = ContentMetadataBuilder.create(); ContentMetadataBuilder builder = ContentMetadataBuilder.create();
String cacheControl = contentMetadata.get().getCacheControl(); String cacheControl = contentMetadata.getCacheControl();
if (cacheControl != null) { if (cacheControl != null) {
builder.cacheControl(cacheControl); builder.cacheControl(cacheControl);
} }
String contentDisposition = contentMetadata.get().getContentDisposition(); String contentDisposition = contentMetadata.getContentDisposition();
if (contentDisposition != null) { if (contentDisposition != null) {
builder.contentDisposition(contentDisposition); builder.contentDisposition(contentDisposition);
} }
String contentEncoding = contentMetadata.get().getContentEncoding(); String contentEncoding = contentMetadata.getContentEncoding();
if (contentEncoding != null) { if (contentEncoding != null) {
builder.contentEncoding(contentEncoding); builder.contentEncoding(contentEncoding);
} }
String contentLanguage = contentMetadata.get().getContentLanguage(); String contentLanguage = contentMetadata.getContentLanguage();
if (contentLanguage != null) { if (contentLanguage != null) {
builder.contentLanguage(contentLanguage); builder.contentLanguage(contentLanguage);
} }
String contentType = contentMetadata.get().getContentType(); String contentType = contentMetadata.getContentType();
if (contentType != null) { if (contentType != null) {
builder.contentType(contentType); builder.contentType(contentType);
} }