swift: issue 971 adding builders and applying ConstructorProperties to domain objects

This commit is contained in:
Adam Lowe 2012-07-12 15:35:33 +01:00
parent 735da0a285
commit 2022bf3fbb
6 changed files with 333 additions and 218 deletions

View File

@ -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<T extends Builder<T>> {
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<ConcreteBuilder> {
@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;
}
}

View File

@ -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<ContainerMetadata> {
private String name;
private long count;
private long bytes;
@SerializedName("X-Container-Read")
private String readACL;
private Map<String, String> 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<String, String> metadata) {
this.name = name;
public Builder<?> toBuilder() {
return new ConcreteBuilder().fromContainerMetadata(this);
}
public static abstract class Builder<T extends Builder<T>> {
protected abstract T self();
protected String name;
protected long count;
protected long bytes;
protected String readACL;
protected Map<String, String> 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<String, String> 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<ConcreteBuilder> {
@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<String, String> metadata;
@ConstructorProperties({
"name", "count", "bytes", "X-Container-Read", "metadata"
})
protected ContainerMetadata(String name, long count, long bytes, @Nullable String readACL, @Nullable Map<String, String> metadata) {
this.name = checkNotNull(name, "name");
this.count = count;
this.bytes = bytes;
this.readACL = readACL;
this.metadata = metadata;
this.metadata = metadata == null ? ImmutableMap.<String, String>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<String, String> 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();
}
}

View File

@ -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<T extends Builder<T>> {
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<ConcreteBuilder> {
@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();
}
}

View File

@ -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<HttpResponse, AccountMetadata> {
@ -38,9 +38,10 @@ public class ParseAccountMetadataResponseFromHeaders implements Function<HttpRes
*/
public AccountMetadata apply(final HttpResponse from) {
String bytesString = checkNotNull(from.getFirstHeaderOrNull(SwiftHeaders.ACCOUNT_BYTES_USED),
SwiftHeaders.ACCOUNT_BYTES_USED);
SwiftHeaders.ACCOUNT_BYTES_USED);
String containersCountString = checkNotNull(from.getFirstHeaderOrNull(SwiftHeaders.ACCOUNT_CONTAINER_COUNT),
SwiftHeaders.ACCOUNT_CONTAINER_COUNT);
return new AccountMetadata(Long.parseLong(containersCountString), Long.parseLong(bytesString));
SwiftHeaders.ACCOUNT_CONTAINER_COUNT);
return AccountMetadata.builder().containerCount(Long.parseLong(containersCountString))
.bytes(Long.parseLong(bytesString)).build();
}
}

View File

@ -53,8 +53,8 @@ public class ParseContainerListFromJsonResponseTest {
.toInputStream("[ {\"name\":\"test_container_1\",\"count\":2,\"bytes\":78}, {\"name\":\"test_container_2\",\"count\":1,\"bytes\":17} ] ");
Map<String, String> meta = new HashMap<String, String>();
List<ContainerMetadata> expects = ImmutableList.of(new ContainerMetadata("test_container_1", 2, 78, null, meta),
new ContainerMetadata("test_container_2", 1, 17, null, meta));
List<ContainerMetadata> 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<List<ContainerMetadata>> parser = i.getInstance(Key
.get(new TypeLiteral<ParseJson<List<ContainerMetadata>>>() {
}));

View File

@ -155,7 +155,7 @@ public class StubSwiftAsyncClient implements CommonSwiftAsyncClient {
return immediateFuture(Sets.newHashSet(Iterables.transform(listing,
new Function<StorageMetadata, ContainerMetadata>() {
public ContainerMetadata apply(StorageMetadata md) {
return new ContainerMetadata(md.getName(), -1, -1, null, new HashMap<String,String>());
return ContainerMetadata.builder().name(md.getName()).count(-1).bytes(-1).metadata(new HashMap<String,String>()).build();
}
})));
}