Five more Equals/HashCode/StringBuilder replacements 69/369/4
authorEd Warnicke <eaw@cisco.com>
Fri, 17 May 2013 15:04:23 +0000 (08:04 -0700)
committerEd Warnicke <eaw@cisco.com>
Wed, 22 May 2013 21:27:29 +0000 (16:27 -0500)
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 <eaw@cisco.com>
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/MacAddress.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Name.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Node.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/NodeConnector.java
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/core/Path.java

index 2bfeb46248416b58327a503a97fa5212c22076ac..b4d1045bac2e461c4c408b6ae04e5531f07e4b53 100644 (file)
@@ -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
index 6ac89952b30fbb73e6b09d08a4d542eaf818bc34..ddb663f5901fe8aa45ab2318a8e86615ae3de05d 100644 (file)
@@ -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
index 5c21294f46da0438da325b263039a895e3f08371..ea9d93bdb6f0774b2d5c72128648f4431c72b2a8 100644 (file)
@@ -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
index 6245c9be8a79d8c17d256f1dd274b6b386a309bf..495af7970c7f0597ad50fc1e39863aafd3753fd6 100644 (file)
@@ -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, ImmutablePair<Class<? extends Object>, String>> compatibleType =
             new ConcurrentHashMap<String, ImmutablePair<Class<? extends Object>, 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
index 5f2dc92aee8c06dd8e204a10af04910cf426bafb..e21f6f14720d9d510d72cf65ccc7ed1159acae14 100644 (file)
@@ -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