From: Ed Warnicke Date: Fri, 17 May 2013 15:04:23 +0000 (-0700) Subject: Five more Equals/HashCode/StringBuilder replacements X-Git-Tag: releasepom-0.1.0~431^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=04839638880d0ce748a588250256440154e2d8f4 Five more Equals/HashCode/StringBuilder replacements I'm intentionally breaking these up into small groups so they can be reasonably reviewed, rather than one giant group that can't. Contributes to fixing bug 20. Seventh set. Fixed to no longer use NodeIDString in Node or NodeConnectorIDString in NodeConnector when computing equality or hashcode. Change-Id: Ic433f705cb27c6454ab06e897ede6715fcf5a5a2 Signed-off-by: Ed Warnicke --- diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java index 2bfeb46248..b4d1045bac 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java @@ -9,13 +9,13 @@ package org.opendaylight.controller.sal.core; +import java.util.Arrays; + import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; /** @@ -79,12 +79,27 @@ public class MacAddress extends Property { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = super.hashCode(); + result = prime * result + Arrays.hashCode(controllerMacAddress); + result = prime * result + Arrays.hashCode(nodeMacAddress); + return result; } @Override public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + MacAddress other = (MacAddress) obj; + if (!Arrays.equals(controllerMacAddress, other.controllerMacAddress)) + return false; + if (!Arrays.equals(nodeMacAddress, other.nodeMacAddress)) + return false; + return true; } @Override diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Name.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Name.java index 6ac89952b3..ddb663f590 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Name.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Name.java @@ -12,9 +12,6 @@ package org.opendaylight.controller.sal.core; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; - /** * The class represents the Name property of an element. */ @@ -48,12 +45,28 @@ public class Name extends Property { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = super.hashCode(); + result = prime * result + + ((nameValue == null) ? 0 : nameValue.hashCode()); + return result; } @Override public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + Name other = (Name) obj; + if (nameValue == null) { + if (other.nameValue != null) + return false; + } else if (!nameValue.equals(other.nameValue)) + return false; + return true; } @Override diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Node.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Node.java index 5c21294f46..ea9d93bdb6 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Node.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Node.java @@ -29,8 +29,6 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.opendaylight.controller.sal.utils.HexEncode; /** @@ -233,10 +231,10 @@ public class Node implements Serializable { } } - /** + /** * Private setter for nodeType to be called by JAXB not by anyone * else, Node is immutable - * + * * @param type of node to be set */ private void setType(String type) { @@ -269,10 +267,10 @@ public class Node implements Serializable { return this.nodeID.toString(); } } - - /** + + /** * private setter to be used by JAXB - * + * * @param nodeIDString String representation for NodeID */ private void setNodeIDString(String nodeIDString) { @@ -284,24 +282,34 @@ public class Node implements Serializable { @Override public int hashCode() { - return new HashCodeBuilder(163841, 56473) - .append(nodeType) - .append(nodeID) - .hashCode(); + final int prime = 31; + int result = 1; + result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode()); + result = prime * result + + ((nodeType == null) ? 0 : nodeType.hashCode()); + return result; } @Override public boolean equals(Object obj) { - if (obj == null) { return false; } - if (obj == this) { return true; } - if (obj.getClass() != getClass()) { + if (this == obj) + return true; + if (obj == null) return false; - } - Node rhs = (Node)obj; - return new EqualsBuilder() - .append(this.getType(), rhs.getType()) - .append(this.getID(), rhs.getID()) - .isEquals(); + if (getClass() != obj.getClass()) + return false; + Node other = (Node) obj; + if (nodeID == null) { + if (other.nodeID != null) + return false; + } else if (!nodeID.equals(other.nodeID)) + return false; + if (nodeType == null) { + if (other.nodeType != null) + return false; + } else if (!nodeType.equals(other.nodeType)) + return false; + return true; } @Override diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/NodeConnector.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/NodeConnector.java index 6245c9be8a..495af7970c 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/NodeConnector.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/NodeConnector.java @@ -27,8 +27,6 @@ import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.tuple.ImmutablePair; /** @@ -53,14 +51,14 @@ public class NodeConnector implements Serializable { ConcurrentHashMap, String>> compatibleType = new ConcurrentHashMap, String>>(); /** - * Represents the OFPP_CONTROLLER reserved port to forward a - * packet to the controller, this is to send data packets - * to the controller from the data plane triggering + * Represents the OFPP_CONTROLLER reserved port to forward a + * packet to the controller, this is to send data packets + * to the controller from the data plane triggering * a packet_in event. */ public static String CONTROLLER = "CTRL"; /** - * Represents the OFPP_ALL reserved OF port + * Represents the OFPP_ALL reserved OF port * to forward to ALL the ports in the system , * should be used for flooding like mechanism to * be used cautiously to avoid excessive flooding. @@ -74,8 +72,8 @@ public class NodeConnector implements Serializable { */ public static String SWSTACK = "SW"; /** - * Describes OFPP_Normal reserved port destination that invokes - * the traditional native L2/L3 HW normal forwarding functionality + * Describes OFPP_Normal reserved port destination that invokes + * the traditional native L2/L3 HW normal forwarding functionality * if supported on the forwarding element. */ public static String HWPATH = "HW"; @@ -332,10 +330,10 @@ public class NodeConnector implements Serializable { } } - /** + /** * Private setter for nodeConnectorType to be called by JAXB not by anyone * else, NodeConnector is immutable - * + * * @param type of node to be set */ private void setType(String type) { @@ -366,9 +364,9 @@ public class NodeConnector implements Serializable { return this.nodeConnectorID.toString(); } - /** + /** * private setter to be used by JAXB - * + * * @param nodeConnectorIDString String representation for NodeConnectorID */ private void setNodeConnectorIDString(String IDStr) { @@ -390,26 +388,46 @@ public class NodeConnector implements Serializable { @Override public int hashCode() { - return new HashCodeBuilder(63389, 4951) - .append(nodeConnectorType) - .append(nodeConnectorID) - .append(nodeConnectorNode) - .hashCode(); + final int prime = 31; + int result = 1; + result = prime * result + + ((nodeConnectorID == null) ? 0 : nodeConnectorID.hashCode()); + result = prime + * result + + ((nodeConnectorNode == null) ? 0 : nodeConnectorNode + .hashCode()); + result = prime + * result + + ((nodeConnectorType == null) ? 0 : nodeConnectorType + .hashCode()); + return result; } @Override public boolean equals(Object obj) { - if (obj == null) { return false; } - if (obj == this) { return true; } - if (obj.getClass() != getClass()) { + if (this == obj) + return true; + if (obj == null) return false; - } - NodeConnector rhs = (NodeConnector)obj; - return new EqualsBuilder() - .append(this.getType(), rhs.getType()) - .append(this.getID(), rhs.getID()) - .append(this.getNode(), rhs.getNode()) - .isEquals(); + if (getClass() != obj.getClass()) + return false; + NodeConnector other = (NodeConnector) obj; + if (nodeConnectorID == null) { + if (other.nodeConnectorID != null) + return false; + } else if (!nodeConnectorID.equals(other.nodeConnectorID)) + return false; + if (nodeConnectorNode == null) { + if (other.nodeConnectorNode != null) + return false; + } else if (!nodeConnectorNode.equals(other.nodeConnectorNode)) + return false; + if (nodeConnectorType == null) { + if (other.nodeConnectorType != null) + return false; + } else if (!nodeConnectorType.equals(other.nodeConnectorType)) + return false; + return true; } @Override diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Path.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Path.java index 5f2dc92aee..e21f6f1472 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Path.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Path.java @@ -19,13 +19,11 @@ package org.opendaylight.controller.sal.core; import java.io.Serializable; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.apache.commons.lang3.builder.EqualsBuilder; -import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; /** * Describe a path as a sequence of Edge such that from @@ -129,12 +127,27 @@ public class Path implements Serializable { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = 1; + result = prime * result + ((edges == null) ? 0 : edges.hashCode()); + return result; } @Override public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Path other = (Path) obj; + if (edges == null) { + if (other.edges != null) + return false; + } else if (!edges.equals(other.edges)) + return false; + return true; } @Override