From 2022bf3fbb75fc023ac165f04f74ca6550aceaff Mon Sep 17 00:00:00 2001 From: Adam Lowe Date: Thu, 12 Jul 2012 15:35:33 +0100 Subject: [PATCH] swift: issue 971 adding builders and applying ConstructorProperties to domain objects --- .../swift/domain/AccountMetadata.java | 119 ++++++---- .../swift/domain/ContainerMetadata.java | 200 +++++++++------- .../swift/domain/internal/ObjectInfoImpl.java | 217 ++++++++++-------- ...rseAccountMetadataResponseFromHeaders.java | 9 +- ...arseContainerListFromJsonResponseTest.java | 4 +- .../swift/internal/StubSwiftAsyncClient.java | 2 +- 6 files changed, 333 insertions(+), 218 deletions(-) diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/AccountMetadata.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/AccountMetadata.java index 13a6456934..e52736982a 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/AccountMetadata.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/AccountMetadata.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to jclouds, Inc. (jclouds) under one or more * contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,6 +18,11 @@ */ package org.jclouds.openstack.swift.domain; +import java.beans.ConstructorProperties; + +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; + /** * * @author James Murty @@ -25,64 +30,94 @@ package org.jclouds.openstack.swift.domain; */ public class AccountMetadata { - public AccountMetadata(long containerCount, long bytes) { + public static Builder builder() { + return new ConcreteBuilder(); + } + + public Builder toBuilder() { + return new ConcreteBuilder().fromAccountMetadata(this); + } + + public static abstract class Builder> { + protected abstract T self(); + + protected long containerCount; + protected long bytes; + + /** + * @see AccountMetadata#getContainerCount() + */ + public T containerCount(long containerCount) { + this.containerCount = containerCount; + return self(); + } + + /** + * @see AccountMetadata#getBytes() + */ + public T bytes(long bytes) { + this.bytes = bytes; + return self(); + } + + public AccountMetadata build() { + return new AccountMetadata(containerCount, bytes); + } + + public T fromAccountMetadata(AccountMetadata in) { + return this + .containerCount(in.getContainerCount()) + .bytes(in.getBytes()); + } + } + + private static class ConcreteBuilder extends Builder { + @Override + protected ConcreteBuilder self() { + return this; + } + } + + private final long containerCount; + private final long bytes; + + @ConstructorProperties({ + "containerCount", "bytes" + }) + protected AccountMetadata(long containerCount, long bytes) { this.containerCount = containerCount; this.bytes = bytes; } - public AccountMetadata() { + public long getContainerCount() { + return this.containerCount; } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("ResourceMetadata [bytes=").append(bytes) - .append(", containerCount=").append(containerCount).append("]"); - return builder.toString(); + public long getBytes() { + return this.bytes; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (int) (bytes ^ (bytes >>> 32)); - result = prime * result + (int) (containerCount ^ (containerCount >>> 32)); - return result; + return Objects.hashCode(containerCount, bytes); } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - AccountMetadata other = (AccountMetadata) obj; - if (bytes != other.bytes) - return false; - if (containerCount != other.containerCount) - return false; - return true; + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + AccountMetadata that = AccountMetadata.class.cast(obj); + return Objects.equal(this.containerCount, that.containerCount) + && Objects.equal(this.bytes, that.bytes); } - private long containerCount; - private long bytes; - - public void setContainerCount(long count) { - this.containerCount = count; + protected ToStringHelper string() { + return Objects.toStringHelper(this) + .add("containerCount", containerCount).add("bytes", bytes); } - public long getContainerCount() { - return containerCount; + @Override + public String toString() { + return string().toString(); } - - public void setBytes(long bytes) { - this.bytes = bytes; - } - - public long getBytes() { - return bytes; - } - } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/ContainerMetadata.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/ContainerMetadata.java index 5e50fa47d8..7db36216db 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/ContainerMetadata.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/ContainerMetadata.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to jclouds, Inc. (jclouds) under one or more * contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,117 +18,163 @@ */ package org.jclouds.openstack.swift.domain; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.beans.ConstructorProperties; import java.util.Map; -import com.google.common.collect.Maps; -import com.google.gson.annotations.SerializedName; +import org.jclouds.javax.annotation.Nullable; +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; +import com.google.common.collect.ImmutableMap; /** - * + * Class ContainerMetadata + * * @author Adrian Cole - * */ -public class ContainerMetadata implements Comparable { - private String name; - private long count; - private long bytes; - @SerializedName("X-Container-Read") - private String readACL; - private Map metadata = Maps.newLinkedHashMap(); +public class ContainerMetadata { - - public ContainerMetadata() { + public static Builder builder() { + return new ConcreteBuilder(); } - public ContainerMetadata(String name, long count, long bytes, String readACL, Map metadata) { - this.name = name; + public Builder toBuilder() { + return new ConcreteBuilder().fromContainerMetadata(this); + } + + public static abstract class Builder> { + protected abstract T self(); + + protected String name; + protected long count; + protected long bytes; + protected String readACL; + protected Map metadata = ImmutableMap.of(); + + /** + * @see ContainerMetadata#getName() + */ + public T name(String name) { + this.name = name; + return self(); + } + + /** + * @see ContainerMetadata#getCount() + */ + public T count(long count) { + this.count = count; + return self(); + } + + /** + * @see ContainerMetadata#getBytes() + */ + public T bytes(long bytes) { + this.bytes = bytes; + return self(); + } + + /** + * @see ContainerMetadata#getReadACL() + */ + public T readACL(String readACL) { + this.readACL = readACL; + return self(); + } + + /** + * @see ContainerMetadata#getMetadata() + */ + public T metadata(Map metadata) { + this.metadata = ImmutableMap.copyOf(checkNotNull(metadata, "metadata")); + return self(); + } + + public ContainerMetadata build() { + return new ContainerMetadata(name, count, bytes, readACL, metadata); + } + + public T fromContainerMetadata(ContainerMetadata in) { + return this + .name(in.getName()) + .count(in.getCount()) + .bytes(in.getBytes()) + .readACL(in.getReadACL()) + .metadata(in.getMetadata()); + } + } + + private static class ConcreteBuilder extends Builder { + @Override + protected ConcreteBuilder self() { + return this; + } + } + + private final String name; + private final long count; + private final long bytes; + private final String readACL; + private final Map metadata; + + @ConstructorProperties({ + "name", "count", "bytes", "X-Container-Read", "metadata" + }) + protected ContainerMetadata(String name, long count, long bytes, @Nullable String readACL, @Nullable Map metadata) { + this.name = checkNotNull(name, "name"); this.count = count; this.bytes = bytes; this.readACL = readACL; - this.metadata = metadata; + this.metadata = metadata == null ? ImmutableMap.of() : ImmutableMap.copyOf(metadata); + } + + public String getName() { + return this.name; } public long getCount() { - return count; + return this.count; } - public void setCount(long count) { - this.count = count; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - public long getBytes() { - return bytes; + return this.bytes; } - - public void setBytes(long bytes) { - this.bytes = bytes; + + @Nullable + public String getReadACL() { + return this.readACL; } - - public boolean isPublic() { - if (readACL == null) - return false; - return readACL.equals(".r:*,.rlistings"); - } - - public void setReadACL(String readACL) { - this.readACL = readACL; - - } - + public Map getMetadata() { - return metadata; + return this.metadata; } - + @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (int) (bytes ^ (bytes >>> 32)); - result = prime * result + (int) (count ^ (count >>> 32)); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; + return Objects.hashCode(name, count, bytes); } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ContainerMetadata other = (ContainerMetadata) obj; - if (bytes != other.bytes) - return false; - if (count != other.count) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + ContainerMetadata that = ContainerMetadata.class.cast(obj); + return Objects.equal(this.name, that.name) + && Objects.equal(this.count, that.count) + && Objects.equal(this.bytes, that.bytes); } - public int compareTo(ContainerMetadata o) { - if (getName() == null) - return -1; - return (this == o) ? 0 : getName().compareTo(o.getName()); + protected ToStringHelper string() { + return Objects.toStringHelper(this) + .add("name", name).add("count", count).add("bytes", bytes).add("readACL", readACL).add("metadata", metadata); } @Override public String toString() { - return "ContainerMetadata [name=" + name + ", count=" + count + ", bytes=" - + bytes + ", isPublic=" + isPublic() + "]"; + return string().toString(); } } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/internal/ObjectInfoImpl.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/internal/ObjectInfoImpl.java index 79379c790e..3d88cace84 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/internal/ObjectInfoImpl.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/domain/internal/ObjectInfoImpl.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to jclouds, Inc. (jclouds) under one or more * contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,86 +18,141 @@ */ package org.jclouds.openstack.swift.domain.internal; +import static com.google.common.base.Preconditions.checkNotNull; + +import java.beans.ConstructorProperties; import java.net.URI; import java.util.Arrays; import java.util.Date; +import javax.inject.Named; + +import org.jclouds.javax.annotation.Nullable; import org.jclouds.openstack.swift.domain.ObjectInfo; -import com.google.gson.annotations.SerializedName; +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; +import com.google.common.collect.ComparisonChain; +/** + * Class ObjectInfoImpl + */ public class ObjectInfoImpl implements ObjectInfo { - public static Builder builder() { - return new Builder(); + + public static Builder builder() { + return new ConcreteBuilder(); } - public static class Builder { - private String name; - private String container; - private URI uri; - private byte[] hash; - private Long bytes; - private String contentType; - private Date lastModified; + public Builder toBuilder() { + return new ConcreteBuilder().fromObjectInfoImpl(this); + } - public Builder name(String name) { + public static abstract class Builder> { + protected abstract T self(); + + protected String name; + protected String container; + protected URI uri; + protected byte[] hash; + protected Long bytes; + protected String contentType; + protected Date lastModified; + + /** + * @see ObjectInfoImpl#getName() + */ + public T name(String name) { this.name = name; - return this; + return self(); } - public Builder container(String container) { + /** + * @see ObjectInfoImpl#getContainer() + */ + public T container(String container) { this.container = container; - return this; + return self(); } - public Builder uri(URI uri) { + /** + * @see ObjectInfoImpl#getUri() + */ + public T uri(URI uri) { this.uri = uri; - return this; + return self(); } - public Builder hash(byte[] hash) { + /** + * @see ObjectInfoImpl#getHash() + */ + public T hash(byte[] hash) { this.hash = hash; - return this; + return self(); } - public Builder bytes(Long bytes) { + /** + * @see ObjectInfoImpl#getBytes() + */ + public T bytes(Long bytes) { this.bytes = bytes; - return this; + return self(); } - public Builder contentType(String contentType) { + /** + * @see ObjectInfoImpl#getContentType() + */ + public T contentType(String contentType) { this.contentType = contentType; - return this; + return self(); } - public Builder lastModified(Date lastModified) { + /** + * @see ObjectInfoImpl#getLastModified() + */ + public T lastModified(Date lastModified) { this.lastModified = lastModified; - return this; + return self(); } public ObjectInfoImpl build() { - return new ObjectInfoImpl(name, uri, container, hash, bytes, contentType, lastModified); + return new ObjectInfoImpl(name, container, uri, hash, bytes, contentType, lastModified); } - public Builder fromObjectInfo(ObjectInfo in) { - return name(in.getName()).container(in.getContainer()).uri(uri).hash(in.getHash()).bytes(in.getBytes()) - .contentType(in.getContentType()).lastModified(in.getLastModified()); + public T fromObjectInfoImpl(ObjectInfoImpl in) { + return this + .name(in.getName()) + .container(in.getContainer()) + .uri(in.getUri()) + .hash(in.getHash()) + .bytes(in.getBytes()) + .contentType(in.getContentType()) + .lastModified(in.getLastModified()); } } - private String name; - private String container; - private URI uri; - private byte[] hash; - private Long bytes; - @SerializedName("content_type") - private String contentType; - @SerializedName("last_modified") - private Date lastModified; + private static class ConcreteBuilder extends Builder { + @Override + protected ConcreteBuilder self() { + return this; + } + } - public ObjectInfoImpl(String name, URI uri, String container, byte[] hash, Long bytes, String contentType, - Date lastModified) { - this.name = name; + private final String name; + private final String container; + private final URI uri; + private final byte[] hash; + private final Long bytes; + @Named("content_type") + private final String contentType; + @Named("last_modified") + private final Date lastModified; + + @ConstructorProperties({ + "name", "container", "uri", "hash", "bytes", "content_type", "last_modified" + }) + protected ObjectInfoImpl(String name, @Nullable String container, @Nullable URI uri, @Nullable byte[] hash, @Nullable Long bytes, + @Nullable String contentType, @Nullable Date lastModified) { + this.name = checkNotNull(name, "name"); this.container = container; this.uri = uri; this.hash = hash; @@ -106,110 +161,88 @@ public class ObjectInfoImpl implements ObjectInfo { this.lastModified = lastModified; } - ObjectInfoImpl() { - - } - /** * {@inheritDoc} */ - @Override public String getName() { - return name; + return this.name; } /** * {@inheritDoc} */ - @Override + @Nullable public String getContainer() { - return container; + return this.container; } /** * {@inheritDoc} */ - @Override + @Nullable public URI getUri() { - return uri; + return this.uri; } /** * {@inheritDoc} */ - @Override + @Nullable public byte[] getHash() { - return hash; + return this.hash; } /** * {@inheritDoc} */ - @Override + @Nullable public Long getBytes() { - return bytes; + return this.bytes; } /** * {@inheritDoc} */ - @Override + @Nullable public String getContentType() { - return contentType; + return this.contentType; } /** * {@inheritDoc} */ - @Override + @Nullable public Date getLastModified() { - return lastModified; + return this.lastModified; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((container == null) ? 0 : container.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; + return Objects.hashCode(name, container); } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - ObjectInfoImpl other = (ObjectInfoImpl) obj; - if (container == null) { - if (other.container != null) - return false; - } else if (!container.equals(other.container)) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + ObjectInfoImpl that = ObjectInfoImpl.class.cast(obj); + return Objects.equal(this.name, that.name) + && Objects.equal(this.container, that.container); + } + + protected ToStringHelper string() { + return Objects.toStringHelper(this) + .add("name", name).add("container", container).add("uri", uri).add("hash", Arrays.toString(hash)) + .add("bytes", bytes).add("contentType", contentType).add("lastModified", lastModified); } @Override public String toString() { - return String.format("[name=%s, container=%s, uri=%s, bytes=%s, contentType=%s, lastModified=%s, hash=%s]", name, - container, uri, bytes, contentType, lastModified, Arrays.toString(hash)); - } - - public Builder toBuilder() { - return builder().fromObjectInfo(this); + return string().toString(); } @Override - public int compareTo(ObjectInfo o) { - return name.compareTo(o.getName()); + public int compareTo(ObjectInfo other) { + return ComparisonChain.start().compare(name, other.getName()).compare(container, other.getContainer()).result(); } - } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseAccountMetadataResponseFromHeaders.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseAccountMetadataResponseFromHeaders.java index d1227d622b..55fed697c7 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseAccountMetadataResponseFromHeaders.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/functions/ParseAccountMetadataResponseFromHeaders.java @@ -28,7 +28,7 @@ import com.google.common.base.Function; /** * This parses {@link AccountMetadata} from HTTP headers. - * + * * @author James Murty */ public class ParseAccountMetadataResponseFromHeaders implements Function { @@ -38,9 +38,10 @@ public class ParseAccountMetadataResponseFromHeaders implements Function meta = new HashMap(); - List expects = ImmutableList.of(new ContainerMetadata("test_container_1", 2, 78, null, meta), - new ContainerMetadata("test_container_2", 1, 17, null, meta)); + List expects = ImmutableList.of(ContainerMetadata.builder().name("test_container_1").count( 2).bytes(78).metadata(meta).build(), + ContainerMetadata.builder().name("test_container_2").count(1).bytes(17).metadata(meta).build()); ParseJson> parser = i.getInstance(Key .get(new TypeLiteral>>() { })); diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/StubSwiftAsyncClient.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/StubSwiftAsyncClient.java index 354ffee1c4..8c11b7c698 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/StubSwiftAsyncClient.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/internal/StubSwiftAsyncClient.java @@ -155,7 +155,7 @@ public class StubSwiftAsyncClient implements CommonSwiftAsyncClient { return immediateFuture(Sets.newHashSet(Iterables.transform(listing, new Function() { public ContainerMetadata apply(StorageMetadata md) { - return new ContainerMetadata(md.getName(), -1, -1, null, new HashMap()); + return ContainerMetadata.builder().name(md.getName()).count(-1).bytes(-1).metadata(new HashMap()).build(); } }))); }