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

View File

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

View File

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

View File

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

View File

@ -17,59 +17,33 @@
package org.jclouds.blobstore.options;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
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.base.Optional;
import com.google.common.collect.ImmutableMap;
@AutoValue
@Beta
public final class CopyOptions {
public abstract class CopyOptions {
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() {
return new Builder();
return new AutoValue_CopyOptions.Builder();
}
public static class Builder {
ContentMetadata contentMetadata;
Map<String, String> userMetadata;
@Nullable
public abstract ContentMetadata contentMetadata();
@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) {
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);
}
public abstract CopyOptions build();
}
}

View File

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