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 * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -18,6 +18,11 @@
*/ */
package org.jclouds.openstack.swift.domain; 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 * @author James Murty
@ -25,64 +30,94 @@ package org.jclouds.openstack.swift.domain;
*/ */
public class AccountMetadata { 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.containerCount = containerCount;
this.bytes = bytes; this.bytes = bytes;
} }
public AccountMetadata() { public long getContainerCount() {
return this.containerCount;
} }
@Override public long getBytes() {
public String toString() { return this.bytes;
StringBuilder builder = new StringBuilder();
builder.append("ResourceMetadata [bytes=").append(bytes)
.append(", containerCount=").append(containerCount).append("]");
return builder.toString();
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return Objects.hashCode(containerCount, bytes);
int result = 1;
result = prime * result + (int) (bytes ^ (bytes >>> 32));
result = prime * result + (int) (containerCount ^ (containerCount >>> 32));
return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null || getClass() != obj.getClass()) return false;
if (obj == null) AccountMetadata that = AccountMetadata.class.cast(obj);
return false; return Objects.equal(this.containerCount, that.containerCount)
if (getClass() != obj.getClass()) && Objects.equal(this.bytes, that.bytes);
return false;
AccountMetadata other = (AccountMetadata) obj;
if (bytes != other.bytes)
return false;
if (containerCount != other.containerCount)
return false;
return true;
} }
private long containerCount; protected ToStringHelper string() {
private long bytes; return Objects.toStringHelper(this)
.add("containerCount", containerCount).add("bytes", bytes);
public void setContainerCount(long count) {
this.containerCount = count;
} }
public long getContainerCount() { @Override
return containerCount; 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 * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -18,117 +18,163 @@
*/ */
package org.jclouds.openstack.swift.domain; package org.jclouds.openstack.swift.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.beans.ConstructorProperties;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps; import org.jclouds.javax.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableMap;
/** /**
* * Class ContainerMetadata
*
* @author Adrian Cole * @author Adrian Cole
*
*/ */
public class ContainerMetadata implements Comparable<ContainerMetadata> { public class 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 static Builder<?> builder() {
public ContainerMetadata() { return new ConcreteBuilder();
} }
public ContainerMetadata(String name, long count, long bytes, String readACL, Map<String, String> metadata) { public Builder<?> toBuilder() {
this.name = name; 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.count = count;
this.bytes = bytes; this.bytes = bytes;
this.readACL = readACL; 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() { 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() { public long getBytes() {
return bytes; return this.bytes;
} }
public void setBytes(long bytes) { @Nullable
this.bytes = bytes; 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() { public Map<String, String> getMetadata() {
return metadata; return this.metadata;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return Objects.hashCode(name, count, bytes);
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;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null || getClass() != obj.getClass()) return false;
if (obj == null) ContainerMetadata that = ContainerMetadata.class.cast(obj);
return false; return Objects.equal(this.name, that.name)
if (getClass() != obj.getClass()) && Objects.equal(this.count, that.count)
return false; && Objects.equal(this.bytes, that.bytes);
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;
} }
public int compareTo(ContainerMetadata o) { protected ToStringHelper string() {
if (getName() == null) return Objects.toStringHelper(this)
return -1; .add("name", name).add("count", count).add("bytes", bytes).add("readACL", readACL).add("metadata", metadata);
return (this == o) ? 0 : getName().compareTo(o.getName());
} }
@Override @Override
public String toString() { public String toString() {
return "ContainerMetadata [name=" + name + ", count=" + count + ", bytes=" return string().toString();
+ bytes + ", isPublic=" + isPublic() + "]";
} }
} }

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to jclouds, Inc. (jclouds) under one or more * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -18,86 +18,141 @@
*/ */
package org.jclouds.openstack.swift.domain.internal; 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.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import javax.inject.Named;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.openstack.swift.domain.ObjectInfo; 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 class ObjectInfoImpl implements ObjectInfo {
public static Builder builder() {
return new Builder(); public static Builder<?> builder() {
return new ConcreteBuilder();
} }
public static class Builder { public Builder<?> toBuilder() {
private String name; return new ConcreteBuilder().fromObjectInfoImpl(this);
private String container; }
private URI uri;
private byte[] hash;
private Long bytes;
private String contentType;
private Date lastModified;
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; this.name = name;
return this; return self();
} }
public Builder container(String container) { /**
* @see ObjectInfoImpl#getContainer()
*/
public T container(String container) {
this.container = container; this.container = container;
return this; return self();
} }
public Builder uri(URI uri) { /**
* @see ObjectInfoImpl#getUri()
*/
public T uri(URI uri) {
this.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; this.hash = hash;
return this; return self();
} }
public Builder bytes(Long bytes) { /**
* @see ObjectInfoImpl#getBytes()
*/
public T bytes(Long bytes) {
this.bytes = bytes; this.bytes = bytes;
return this; return self();
} }
public Builder contentType(String contentType) { /**
* @see ObjectInfoImpl#getContentType()
*/
public T contentType(String contentType) {
this.contentType = contentType; this.contentType = contentType;
return this; return self();
} }
public Builder lastModified(Date lastModified) { /**
* @see ObjectInfoImpl#getLastModified()
*/
public T lastModified(Date lastModified) {
this.lastModified = lastModified; this.lastModified = lastModified;
return this; return self();
} }
public ObjectInfoImpl build() { 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) { public T fromObjectInfoImpl(ObjectInfoImpl in) {
return name(in.getName()).container(in.getContainer()).uri(uri).hash(in.getHash()).bytes(in.getBytes()) return this
.contentType(in.getContentType()).lastModified(in.getLastModified()); .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 static class ConcreteBuilder extends Builder<ConcreteBuilder> {
private String container; @Override
private URI uri; protected ConcreteBuilder self() {
private byte[] hash; return this;
private Long bytes; }
@SerializedName("content_type") }
private String contentType;
@SerializedName("last_modified")
private Date lastModified;
public ObjectInfoImpl(String name, URI uri, String container, byte[] hash, Long bytes, String contentType, private final String name;
Date lastModified) { private final String container;
this.name = name; 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.container = container;
this.uri = uri; this.uri = uri;
this.hash = hash; this.hash = hash;
@ -106,110 +161,88 @@ public class ObjectInfoImpl implements ObjectInfo {
this.lastModified = lastModified; this.lastModified = lastModified;
} }
ObjectInfoImpl() {
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public String getName() { public String getName() {
return name; return this.name;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public String getContainer() { public String getContainer() {
return container; return this.container;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public URI getUri() { public URI getUri() {
return uri; return this.uri;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public byte[] getHash() { public byte[] getHash() {
return hash; return this.hash;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public Long getBytes() { public Long getBytes() {
return bytes; return this.bytes;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public String getContentType() { public String getContentType() {
return contentType; return this.contentType;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Nullable
public Date getLastModified() { public Date getLastModified() {
return lastModified; return this.lastModified;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return Objects.hashCode(name, container);
int result = 1;
result = prime * result + ((container == null) ? 0 : container.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj) return true;
return true; if (obj == null || getClass() != obj.getClass()) return false;
if (obj == null) ObjectInfoImpl that = ObjectInfoImpl.class.cast(obj);
return false; return Objects.equal(this.name, that.name)
if (getClass() != obj.getClass()) && Objects.equal(this.container, that.container);
return false; }
ObjectInfoImpl other = (ObjectInfoImpl) obj;
if (container == null) { protected ToStringHelper string() {
if (other.container != null) return Objects.toStringHelper(this)
return false; .add("name", name).add("container", container).add("uri", uri).add("hash", Arrays.toString(hash))
} else if (!container.equals(other.container)) .add("bytes", bytes).add("contentType", contentType).add("lastModified", lastModified);
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} }
@Override @Override
public String toString() { public String toString() {
return String.format("[name=%s, container=%s, uri=%s, bytes=%s, contentType=%s, lastModified=%s, hash=%s]", name, return string().toString();
container, uri, bytes, contentType, lastModified, Arrays.toString(hash));
}
public Builder toBuilder() {
return builder().fromObjectInfo(this);
} }
@Override @Override
public int compareTo(ObjectInfo o) { public int compareTo(ObjectInfo other) {
return name.compareTo(o.getName()); 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. * This parses {@link AccountMetadata} from HTTP headers.
* *
* @author James Murty * @author James Murty
*/ */
public class ParseAccountMetadataResponseFromHeaders implements Function<HttpResponse, AccountMetadata> { public class ParseAccountMetadataResponseFromHeaders implements Function<HttpResponse, AccountMetadata> {
@ -38,9 +38,10 @@ public class ParseAccountMetadataResponseFromHeaders implements Function<HttpRes
*/ */
public AccountMetadata apply(final HttpResponse from) { public AccountMetadata apply(final HttpResponse from) {
String bytesString = checkNotNull(from.getFirstHeaderOrNull(SwiftHeaders.ACCOUNT_BYTES_USED), 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), String containersCountString = checkNotNull(from.getFirstHeaderOrNull(SwiftHeaders.ACCOUNT_CONTAINER_COUNT),
SwiftHeaders.ACCOUNT_CONTAINER_COUNT); SwiftHeaders.ACCOUNT_CONTAINER_COUNT);
return new AccountMetadata(Long.parseLong(containersCountString), Long.parseLong(bytesString)); 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} ] "); .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>(); Map<String, String> meta = new HashMap<String, String>();
List<ContainerMetadata> expects = ImmutableList.of(new ContainerMetadata("test_container_1", 2, 78, null, meta), List<ContainerMetadata> expects = ImmutableList.of(ContainerMetadata.builder().name("test_container_1").count( 2).bytes(78).metadata(meta).build(),
new ContainerMetadata("test_container_2", 1, 17, null, meta)); ContainerMetadata.builder().name("test_container_2").count(1).bytes(17).metadata(meta).build());
ParseJson<List<ContainerMetadata>> parser = i.getInstance(Key ParseJson<List<ContainerMetadata>> parser = i.getInstance(Key
.get(new TypeLiteral<ParseJson<List<ContainerMetadata>>>() { .get(new TypeLiteral<ParseJson<List<ContainerMetadata>>>() {
})); }));

View File

@ -155,7 +155,7 @@ public class StubSwiftAsyncClient implements CommonSwiftAsyncClient {
return immediateFuture(Sets.newHashSet(Iterables.transform(listing, return immediateFuture(Sets.newHashSet(Iterables.transform(listing,
new Function<StorageMetadata, ContainerMetadata>() { new Function<StorageMetadata, ContainerMetadata>() {
public ContainerMetadata apply(StorageMetadata md) { 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();
} }
}))); })));
} }