Consistently implement domain object methods

Including equals, hashCode, and toString.
This commit is contained in:
Andrew Gaul 2016-01-24 21:17:27 -08:00
parent b50c518f7e
commit a3376d4efe
6 changed files with 67 additions and 39 deletions

View File

@ -23,6 +23,8 @@ import java.util.Date;
import java.util.Map;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.domain.Location;
@ -91,4 +93,12 @@ public class BlobMetadataImpl extends StorageMetadataImpl implements BlobMetadat
public int hashCode() {
return Objects.hashCode(super.hashCode(), publicUri, container, contentMetadata);
}
@Override
protected ToStringHelper string() {
return super.string()
.add("publicUri", publicUri)
.add("container", container)
.add("contentMetadata", contentMetadata);
}
}

View File

@ -19,6 +19,8 @@ package org.jclouds.blobstore.domain.internal;
import java.net.URI;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.StorageType;
@ -115,4 +117,12 @@ public class MutableBlobMetadataImpl extends MutableStorageMetadataImpl implemen
public int hashCode() {
return Objects.hashCode(super.hashCode(), contentMetadata, publicUri, container);
}
@Override
protected ToStringHelper string() {
return super.string()
.add("publicUri", publicUri)
.add("container", container)
.add("contentMetadata", contentMetadata);
}
}

View File

@ -19,6 +19,8 @@ package org.jclouds.blobstore.domain.internal;
import java.util.Date;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import org.jclouds.blobstore.domain.MutableStorageMetadata;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
@ -118,4 +120,13 @@ public class MutableStorageMetadataImpl extends MutableResourceMetadataImpl<Stor
public int hashCode() {
return Objects.hashCode(super.hashCode(), eTag, creationDate, lastModified, size);
}
@Override
protected ToStringHelper string() {
return super.string()
.add("eTag", eTag)
.add("creationDate", creationDate)
.add("lastModified", lastModified)
.add("size", size);
}
}

View File

@ -23,6 +23,7 @@ import java.util.Date;
import java.util.Map;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
@ -77,7 +78,7 @@ public class StorageMetadataImpl extends ResourceMetadataImpl<StorageType> imple
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), eTag, creationDate,
lastModified, type);
lastModified, type, size);
}
@Override
@ -93,9 +94,20 @@ public class StorageMetadataImpl extends ResourceMetadataImpl<StorageType> imple
if (!Objects.equal(creationDate, other.creationDate)) { return false; }
if (!Objects.equal(lastModified, other.lastModified)) { return false; }
if (!Objects.equal(type, other.type)) { return false; }
if (!Objects.equal(size, other.size)) { return false; }
return true;
}
@Override
protected ToStringHelper string() {
return super.string()
.add("eTag", eTag)
.add("creationDate", creationDate)
.add("lastModified", lastModified)
.add("type", type)
.add("size", size);
}
/**
* {@inheritDoc}
*/

View File

@ -23,6 +23,8 @@ import org.jclouds.domain.Location;
import org.jclouds.domain.MutableResourceMetadata;
import org.jclouds.domain.ResourceMetadata;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.Maps;
/**
@ -158,20 +160,22 @@ public class MutableResourceMetadataImpl<T extends Enum<T>> implements MutableRe
@Override
public String toString() {
return "[type=" + type + ", id=" + id + ", name=" + name + ", location=" + location
+ ", uri=" + uri + ", userMetadata=" + userMetadata + "]";
return string().toString();
}
protected ToStringHelper string() {
return Objects.toStringHelper("").omitNullValues()
.add("id", id)
.add("location", location)
.add("name", name)
.add("type", getType())
.add("uri", uri)
.add("userMetadata", userMetadata);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((location == null) ? 0 : location.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
result = prime * result + ((uri == null) ? 0 : uri.hashCode());
return result;
return Objects.hashCode(id, location, name, type, uri, userMetadata);
}
@Override
@ -183,32 +187,12 @@ public class MutableResourceMetadataImpl<T extends Enum<T>> implements MutableRe
if (!(obj instanceof MutableResourceMetadata<?>))
return false;
MutableResourceMetadata<?> other = (MutableResourceMetadata<?>) obj;
if (id == null) {
if (other.getProviderId() != null)
return false;
} else if (!id.equals(other.getProviderId()))
return false;
if (location == null) {
if (other.getLocation() != null)
return false;
} else if (!location.equals(other.getLocation()))
return false;
if (name == null) {
if (other.getName() != null)
return false;
} else if (!name.equals(other.getName()))
return false;
if (type == null) {
if (other.getType() != null)
return false;
} else if (!type.equals(other.getType()))
return false;
if (uri == null) {
if (other.getUri() != null)
return false;
} else if (!uri.equals(other.getUri()))
return false;
return true;
return Objects.equal(id, other.getProviderId())
&& Objects.equal(location, other.getLocation())
&& Objects.equal(name, other.getName())
&& Objects.equal(type, other.getType())
&& Objects.equal(uri, other.getUri())
&& Objects.equal(userMetadata, other.getUserMetadata());
}
}

View File

@ -114,12 +114,13 @@ public abstract class ResourceMetadataImpl<T extends Enum<T>> implements Resourc
return false;
ResourceMetadataImpl<?> that = ResourceMetadataImpl.class.cast(o);
return equal(this.getType(), that.getType()) && equal(this.providerId, that.providerId)
&& equal(this.name, that.name) && equal(this.location, that.location) && equal(this.uri, that.uri);
&& equal(this.name, that.name) && equal(this.location, that.location) && equal(this.uri, that.uri)
&& equal(this.userMetadata, that.userMetadata);
}
@Override
public int hashCode() {
return Objects.hashCode(getType(), providerId, name, location, uri);
return Objects.hashCode(getType(), providerId, name, location, uri, userMetadata);
}
@Override