From c34b9ee41bcc31359f581d04d38e0b5615eee21b Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Mon, 29 Apr 2002 11:02:49 +0000 Subject: [PATCH] Improve equals implementation git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272587 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/Manifest.java | 106 ++++++++++++------ 1 file changed, 70 insertions(+), 36 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Manifest.java b/src/main/org/apache/tools/ant/taskdefs/Manifest.java index 76ddbc64b..75b4254cb 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Manifest.java +++ b/src/main/org/apache/tools/ant/taskdefs/Manifest.java @@ -176,19 +176,42 @@ public class Manifest extends Task { setValue(value); } + /** + * @see java.lang.Object#hashCode + */ + public int hashCode() { + int hashCode = 0; + + if (name != null) { + hashCode += name.hashCode(); + } + + hashCode += values.hashCode(); + return hashCode; + } + /** * @see java.lang.Object#equals */ public boolean equals(Object rhs) { - if (!(rhs instanceof Attribute)) { + if (rhs == null || rhs.getClass() != getClass()) { return false; } + + if (rhs == this) { + return true; + } Attribute rhsAttribute = (Attribute) rhs; - return (name != null && rhsAttribute.name != null && - getKey().equals(rhsAttribute.getKey()) && - values != null && - CollectionUtils.equals(values, rhsAttribute.values)); + String lhsKey = getKey(); + String rhsKey = rhsAttribute.getKey(); + if ((lhsKey == null && rhsKey != null) + || (lhsKey != null && rhsKey == null) + || !lhsKey.equals(rhsKey)) { + return false; + } + + return CollectionUtils.equals(values, rhsAttribute.values); } /** @@ -642,29 +665,34 @@ public class Manifest extends Task { } /** - * @see java.lang.Object#equals + * @see java.lang.Object#hashCode */ - public boolean equals(Object rhs) { - if (!(rhs instanceof Section)) { - return false; + public int hashCode() { + int hashCode = 0; + + if (name != null) { + hashCode += name.hashCode(); } + + hashCode += attributes.hashCode(); + return hashCode; + } - Section rhsSection = (Section) rhs; - if (attributes.size() != rhsSection.attributes.size()) { + /** + * @see java.lang.Object#equals + */ + public boolean equals(Object rhs) { + if (rhs == null || rhs.getClass() != getClass()) { return false; } - - for (Enumeration e = attributes.keys(); e.hasMoreElements();) { - String attributeName = (String) e.nextElement(); - Object attributeValue = attributes.get(attributeName); - Object rhsAttributeValue - = rhsSection.attributes.get(attributeName); - if (!attributeValue.equals(rhsAttributeValue)) { - return false; - } + + if (rhs == this) { + return true; } - return true; + Section rhsSection = (Section) rhs; + + return attributes.equals(rhsSection.attributes); } } @@ -937,14 +965,33 @@ public class Manifest extends Task { return warnings.elements(); } + /** + * @see java.lang.Object#hashCode + */ + public int hashCode() { + int hashCode = 0; + + if (manifestVersion != null) { + hashCode += manifestVersion.hashCode(); + } + hashCode += mainSection.hashCode(); + hashCode += sections.hashCode(); + + return hashCode; + } + /** * @see java.lang.Object#equals */ public boolean equals(Object rhs) { - if (!(rhs instanceof Manifest)) { + if (rhs == null || rhs.getClass() != getClass()) { return false; } + if (rhs == this) { + return true; + } + Manifest rhsManifest = (Manifest) rhs; if (manifestVersion == null) { if (rhsManifest.manifestVersion != null) { @@ -953,25 +1000,12 @@ public class Manifest extends Task { } else if (!manifestVersion.equals(rhsManifest.manifestVersion)) { return false; } - if (sections.size() != rhsManifest.sections.size()) { - return false; - } if (!mainSection.equals(rhsManifest.mainSection)) { return false; } - Enumeration e = sections.elements(); - while (e.hasMoreElements()) { - Section section = (Section) e.nextElement(); - Section rhsSection - = (Section) rhsManifest.sections.get(section.getName()); - if (!section.equals(rhsSection)) { - return false; - } - } - - return true; + return sections.equals(rhsManifest.sections); } /**