Fixing equals method on domain objects

This commit is contained in:
Andrew Donald Kennedy 2012-02-08 13:32:25 +00:00
parent 78fb8bd919
commit 9153d887af
9 changed files with 52 additions and 32 deletions

View File

@ -147,7 +147,9 @@ public class Entity extends EntityType<Entity> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Entity that = Entity.class.cast(o); Entity that = Entity.class.cast(o);
return super.equals(that); return super.equals(that);

View File

@ -224,7 +224,9 @@ public class EntityType<T extends EntityType<T>> extends ResourceType<T> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
EntityType<?> that = EntityType.class.cast(o); EntityType<?> that = EntityType.class.cast(o);
return super.equals(that) && return super.equals(that) &&

View File

@ -18,9 +18,14 @@
*/ */
package org.jclouds.vcloud.director.v1_5.domain; package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.equal; import static com.google.common.base.Objects.*;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.*;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.NS; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
@ -107,17 +112,17 @@ public class Error {
public Builder fromError(Error in) { public Builder fromError(Error in) {
return message(in.getMessage()) return message(in.getMessage())
.majorErrorCode(in.getMajorErrorCode()) .majorErrorCode(in.getMajorErrorCode())
.minorErrorCode(in.getMinorErrorCode()) .minorErrorCode(in.getMinorErrorCode())
.vendorSpecificErrorCode(in.getVendorSpecificErrorCode()) .vendorSpecificErrorCode(in.getVendorSpecificErrorCode())
.stackTrace(in.getStackTrace()); .stackTrace(in.getStackTrace());
} }
} }
@XmlAttribute @XmlAttribute
private String message; private String message;
@XmlAttribute @XmlAttribute
private int majorErrorCode; private Integer majorErrorCode;
@XmlAttribute @XmlAttribute
private String minorErrorCode; private String minorErrorCode;
@XmlAttribute @XmlAttribute
@ -125,9 +130,9 @@ public class Error {
@XmlAttribute @XmlAttribute
private String stackTrace; private String stackTrace;
private Error(String message, int majorErrorCode, String minorErrorCode) { private Error(String message, Integer majorErrorCode, String minorErrorCode) {
this.message = checkNotNull(message, "message"); this.message = checkNotNull(message, "message");
this.majorErrorCode = majorErrorCode; this.majorErrorCode = checkNotNull(majorErrorCode, "majorErrorCode");
this.minorErrorCode = checkNotNull(minorErrorCode, "minorErrorCode"); this.minorErrorCode = checkNotNull(minorErrorCode, "minorErrorCode");
} }
@ -145,7 +150,7 @@ public class Error {
/** /**
* The class of the error. Matches the HTTP status code. * The class of the error. Matches the HTTP status code.
*/ */
public int getMajorErrorCode() { public Integer getMajorErrorCode() {
return majorErrorCode; return majorErrorCode;
} }
@ -182,10 +187,11 @@ public class Error {
this.stackTrace = stackTrace; this.stackTrace = stackTrace;
} }
@Override
@Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Error that = (Error) o; Error that = (Error) o;
return equal(this.message, that.message) && return equal(this.message, that.message) &&
@ -197,15 +203,14 @@ public class Error {
@Override @Override
public int hashCode() { public int hashCode() {
return super.hashCode() + Objects.hashCode(message, majorErrorCode, minorErrorCode, vendorSpecificErrorCode, stackTrace); return Objects.hashCode(message, majorErrorCode, minorErrorCode, vendorSpecificErrorCode, stackTrace);
} }
@Override @Override
public String toString() { public String toString() {
return Objects.toStringHelper("") return Objects.toStringHelper("")
.add("message", message) .add("message", message).add("majorErrorCode", majorErrorCode).add("minorErrorCode", minorErrorCode)
.add("majorErrorCode", majorErrorCode) .add("vendorSpecificErrorCode", vendorSpecificErrorCode).add("stackTrace", stackTrace)
.add("minorErrorCode", minorErrorCode)
.toString(); .toString();
} }
} }

View File

@ -159,7 +159,9 @@ public class Link extends ReferenceType<Link> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Link that = (Link) o; Link that = (Link) o;
return super.equals(that) && equal(this.rel, that.rel); return super.equals(that) && equal(this.rel, that.rel);

View File

@ -50,6 +50,7 @@ public class Metadata extends ResourceType<Metadata>{
return new Builder(); return new Builder();
} }
@Override
public Builder toBuilder() { public Builder toBuilder() {
return new Builder().fromMetadataList(this); return new Builder().fromMetadataList(this);
} }
@ -74,6 +75,7 @@ public class Metadata extends ResourceType<Metadata>{
return this; return this;
} }
@Override
public Metadata build() { public Metadata build() {
Metadata metadata = new Metadata(href, metadataEntries); Metadata metadata = new Metadata(href, metadataEntries);
metadata.setType(type); metadata.setType(type);
@ -140,7 +142,9 @@ public class Metadata extends ResourceType<Metadata>{
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Metadata that = Metadata.class.cast(o); Metadata that = Metadata.class.cast(o);
return super.equals(that) && equal(metadata, that.metadata); return super.equals(that) && equal(metadata, that.metadata);

View File

@ -49,6 +49,7 @@ public class MetadataEntry extends ResourceType<MetadataEntry> {
return new Builder(); return new Builder();
} }
@Override
public Builder toBuilder() { public Builder toBuilder() {
return new Builder().fromMetadata(this); return new Builder().fromMetadata(this);
} }
@ -73,6 +74,7 @@ public class MetadataEntry extends ResourceType<MetadataEntry> {
return this; return this;
} }
@Override
public MetadataEntry build() { public MetadataEntry build() {
MetadataEntry metadataEntry = new MetadataEntry(href, key, value); MetadataEntry metadataEntry = new MetadataEntry(href, key, value);
metadataEntry.setType(type); metadataEntry.setType(type);
@ -132,13 +134,12 @@ public class MetadataEntry extends ResourceType<MetadataEntry> {
this.value = checkNotNull(value, "value"); this.value = checkNotNull(value, "value");
} }
@XmlElement(namespace = NS, name = "K") @XmlElement(namespace = NS, name = "Key")
private String key; private String key;
@XmlElement(namespace = NS, name = "Value") @XmlElement(namespace = NS, name = "Value")
private String value; private String value;
/** /**
*
* @return key of the entry * @return key of the entry
*/ */
public String getKey() { public String getKey() {
@ -146,17 +147,17 @@ public class MetadataEntry extends ResourceType<MetadataEntry> {
} }
/** /**
*
* @return value of the entry * @return value of the entry
*/ */
public String getValue() { public String getValue() {
return value; return value;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
MetadataEntry that = MetadataEntry.class.cast(o); MetadataEntry that = MetadataEntry.class.cast(o);
return super.equals(that) && equal(key, that.key); return super.equals(that) && equal(key, that.key);

View File

@ -183,7 +183,9 @@ public class Org extends EntityType<Org> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Org that = Org.class.cast(o); Org that = Org.class.cast(o);
return super.equals(that) && equal(fullName, that.fullName); return super.equals(that) && equal(fullName, that.fullName);

View File

@ -461,10 +461,13 @@ public class Task extends EntityType<Task> {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!super.equals(o)) if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false; return false;
Task that = Task.class.cast(o); Task that = Task.class.cast(o);
return super.equals(that) && equal(this.error, that.error) && equal(this.org, that.org) && return super.equals(that) &&
equal(this.error, that.error) && equal(this.org, that.org) &&
equal(this.progress, that.progress) && equal(this.status, that.status) && equal(this.progress, that.progress) && equal(this.status, that.status) &&
equal(this.operation, that.operation) && equal(this.operationName, that.operationName) && equal(this.operation, that.operation) && equal(this.operationName, that.operationName) &&
equal(this.startTime, that.startTime) && equal(this.endTime, that.endTime) && equal(this.startTime, that.startTime) && equal(this.endTime, that.endTime) &&

View File

@ -28,9 +28,8 @@ import java.util.Set;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.jclouds.vcloud.director.v1_5.domain.Org.Builder;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;