From 63d43f236e15a881e0c30f4f0c82627f46624c69 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 27 Aug 2014 10:15:38 -0700 Subject: [PATCH] Correct unusual equals implementations Found with FindBugs. --- .../src/main/java/org/jclouds/byon/Node.java | 26 ++++++++++++++++--- .../elasticstack/domain/WellKnownImage.java | 16 +++++++++--- .../domain/AuthenticationResponse.java | 11 +++++--- .../dynect/v3/domain/CreateRecord.java | 2 +- .../org/jclouds/scriptbuilder/InitScript.java | 10 ++++--- .../org/jclouds/servermanager/Datacenter.java | 11 +++++--- .../org/jclouds/servermanager/Hardware.java | 14 +++++++--- .../java/org/jclouds/servermanager/Image.java | 11 +++++--- .../org/jclouds/servermanager/Server.java | 19 +++++++++++--- 9 files changed, 95 insertions(+), 25 deletions(-) diff --git a/apis/byon/src/main/java/org/jclouds/byon/Node.java b/apis/byon/src/main/java/org/jclouds/byon/Node.java index c930e96a59..61bf0b34b2 100644 --- a/apis/byon/src/main/java/org/jclouds/byon/Node.java +++ b/apis/byon/src/main/java/org/jclouds/byon/Node.java @@ -272,10 +272,30 @@ public class Node { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof Node)) { + return false; + } + Node that = (Node) obj; + return Objects.equal(this.id, that.id) + && Objects.equal(this.name, that.name) + && Objects.equal(this.description, that.description) + && Objects.equal(this.hostname, that.hostname) + && Objects.equal(this.locationId, that.locationId) + && Objects.equal(this.osArch, that.osArch) + && Objects.equal(this.osFamily, that.osFamily) + && Objects.equal(this.osDescription, that.osDescription) + && Objects.equal(this.osVersion, that.osVersion) + && Objects.equal(this.loginPort, that.loginPort) + && Objects.equal(this.os64Bit, that.os64Bit) + && Objects.equal(this.group, that.group) + && Objects.equal(this.tags, that.tags) + && Objects.equal(this.metadata, that.metadata) + && Objects.equal(this.username, that.username) + // not comparing credential and credentialUrl + && Objects.equal(this.sudoPassword, that.sudoPassword); } @Override diff --git a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/domain/WellKnownImage.java b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/domain/WellKnownImage.java index 29106dbd44..41b2683aa6 100644 --- a/apis/elasticstack/src/main/java/org/jclouds/elasticstack/domain/WellKnownImage.java +++ b/apis/elasticstack/src/main/java/org/jclouds/elasticstack/domain/WellKnownImage.java @@ -135,10 +135,20 @@ public class WellKnownImage { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof WellKnownImage)){ + return false; + } + WellKnownImage that = (WellKnownImage) obj; + return Objects.equal(this.loginUser, that.loginUser) + && Objects.equal(this.uuid, that.uuid) + && Objects.equal(this.description, that.description) + && Objects.equal(this.osFamily, that.osFamily) + && Objects.equal(this.osVersion, that.osVersion) + && Objects.equal(this.size, that.size) + && Objects.equal(this.is64bit, that.is64bit); } @Override diff --git a/common/openstack/src/main/java/org/jclouds/openstack/domain/AuthenticationResponse.java b/common/openstack/src/main/java/org/jclouds/openstack/domain/AuthenticationResponse.java index c76dca8865..d86673f844 100644 --- a/common/openstack/src/main/java/org/jclouds/openstack/domain/AuthenticationResponse.java +++ b/common/openstack/src/main/java/org/jclouds/openstack/domain/AuthenticationResponse.java @@ -48,10 +48,15 @@ public class AuthenticationResponse { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof AuthenticationResponse)) { + return false; + } + AuthenticationResponse that = (AuthenticationResponse) obj; + return Objects.equal(this.authToken, that.authToken) + && Objects.equal(this.services, that.services); } @Override diff --git a/providers/dynect/src/main/java/org/jclouds/dynect/v3/domain/CreateRecord.java b/providers/dynect/src/main/java/org/jclouds/dynect/v3/domain/CreateRecord.java index e80eb1caf3..dbb30d7f1c 100644 --- a/providers/dynect/src/main/java/org/jclouds/dynect/v3/domain/CreateRecord.java +++ b/providers/dynect/src/main/java/org/jclouds/dynect/v3/domain/CreateRecord.java @@ -74,7 +74,7 @@ public class CreateRecord> { public boolean equals(Object obj) { if (this == obj) return true; - if (obj == null || !obj.getClass().equals(CreateRecord.class)) + if (obj == null || !(obj instanceof CreateRecord)) return false; CreateRecord that = CreateRecord.class.cast(obj); return equal(this.fqdn, that.fqdn) && equal(this.type, that.type) && equal(this.ttl, that.ttl) diff --git a/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/InitScript.java b/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/InitScript.java index c8d7d2a416..a7fd4253b1 100644 --- a/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/InitScript.java +++ b/scriptbuilder/src/main/java/org/jclouds/scriptbuilder/InitScript.java @@ -299,10 +299,14 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return equal(this.toString(), that.toString()); + if (!(obj instanceof InitScript)) { + return false; + } + InitScript that = (InitScript) obj; + return equal(this.instanceName, that.instanceName); } @Override diff --git a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Datacenter.java b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Datacenter.java index b0ae2981e3..92feb7ba33 100644 --- a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Datacenter.java +++ b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Datacenter.java @@ -38,10 +38,15 @@ public class Datacenter { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof Datacenter)) { + return false; + } + Datacenter that = (Datacenter) obj; + return Objects.equal(this.id, that.id) + && Objects.equal(this.name, that.name); } @Override diff --git a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Hardware.java b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Hardware.java index c320961147..f0068bd1f8 100644 --- a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Hardware.java +++ b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Hardware.java @@ -44,10 +44,18 @@ public class Hardware { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof Hardware)) { + return false; + } + Hardware that = (Hardware) obj; + return Objects.equal(this.id, that.id) + && Objects.equal(this.name, that.name) + && Objects.equal(this.cores, that.cores) + && Objects.equal(this.ram, that.ram) + && Objects.equal(this.disk, that.disk); } @Override diff --git a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Image.java b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Image.java index 6b18e6c357..e187a031f6 100644 --- a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Image.java +++ b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Image.java @@ -38,10 +38,15 @@ public class Image { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof Image)) { + return false; + } + Image that = (Image) obj; + return Objects.equal(this.id, that.id) + && Objects.equal(this.name, that.name); } @Override diff --git a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Server.java b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Server.java index df37dba680..3c2a8ddda6 100644 --- a/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Server.java +++ b/skeletons/standalone-compute/src/main/java/org/jclouds/servermanager/Server.java @@ -46,10 +46,23 @@ public class Server { } @Override - public boolean equals(Object that) { - if (that == null) + public boolean equals(Object obj) { + if (obj == null) return false; - return Objects.equal(this.toString(), that.toString()); + if (!(obj instanceof Server)) { + return false; + } + Server that = (Server) obj; + return Objects.equal(this.id, that.id) + && Objects.equal(this.name, that.name) + && Objects.equal(this.status, that.status) + && Objects.equal(this.datacenter, that.datacenter) + && Objects.equal(this.imageId, that.imageId) + && Objects.equal(this.hardwareId, that.hardwareId) + && Objects.equal(this.publicAddress, that.publicAddress) + && Objects.equal(this.privateAddress, that.privateAddress) + && Objects.equal(this.loginUser, that.loginUser) + && Objects.equal(this.password, that.password); } @Override