From 96fab6bc9e850ab89e1b45dd8ed958c98ba9e561 Mon Sep 17 00:00:00 2001 From: Ed Warnicke Date: Fri, 17 May 2013 08:12:09 -0700 Subject: [PATCH] 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. Tenth set. Change-Id: I62a1b9724ed235c6b406fbbf2a005606cfd9d87e Signed-off-by: Ed Warnicke Signed-off-by: Giovanni Meo --- .../controller/switchmanager/SpanConfig.java | 30 +++++++++--- .../controller/switchmanager/Subnet.java | 49 ++++++++++--------- .../controller/switchmanager/Switch.java | 39 +++++++++++++-- .../switchmanager/SwitchConfig.java | 41 ++++++++++++++-- .../usermanager/internal/UserConfig.java | 34 +++++++++++-- 5 files changed, 152 insertions(+), 41 deletions(-) diff --git a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SpanConfig.java b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SpanConfig.java index f701553811..80ed209819 100644 --- a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SpanConfig.java +++ b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SpanConfig.java @@ -14,15 +14,11 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.opendaylight.controller.sal.core.ConstructionException; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType; import org.opendaylight.controller.sal.utils.GUIField; - -import org.opendaylight.controller.switchmanager.SpanConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,12 +65,34 @@ public class SpanConfig implements Serializable { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = 1; + result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode()); + result = prime * result + + ((spanPort == null) ? 0 : spanPort.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; + SpanConfig other = (SpanConfig) obj; + if (nodeId == null) { + if (other.nodeId != null) + return false; + } else if (!nodeId.equals(other.nodeId)) + return false; + if (spanPort == null) { + if (other.spanPort != null) + return false; + } else if (!spanPort.equals(other.spanPort)) + return false; + return true; } public static ArrayList getFieldsNames() { diff --git a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java index 551f0722db..e15303581f 100644 --- a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java +++ b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Subnet.java @@ -15,13 +15,8 @@ import java.net.UnknownHostException; import java.util.HashSet; import java.util.Set; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.opendaylight.controller.sal.core.NodeConnector; -import org.opendaylight.controller.switchmanager.Subnet; -import org.opendaylight.controller.switchmanager.SubnetConfig; - /** * The class describes subnet information including L3 address, vlan and set of * ports associated with the subnet. @@ -179,33 +174,43 @@ public class Subnet implements Serializable { return this; } - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { - return new HashCodeBuilder().append(networkAddress).append( - subnetMaskLength).toHashCode(); + final int prime = 31; + int result = 1; + result = prime * result + + ((networkAddress == null) ? 0 : networkAddress.hashCode()); + result = prime * result + + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode()); + result = prime * result + subnetMaskLength; + result = prime * result + vlan; + return result; } - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object obj) { - if (this == obj) { + if (this == obj) return true; - } - if (obj == null) { + if (obj == null) return false; - } - if (obj.getClass() != getClass()) { + if (getClass() != obj.getClass()) return false; - } Subnet other = (Subnet) obj; - // Check only equality for the key fields - return new EqualsBuilder().append(networkAddress, other.networkAddress) - .append(subnetMaskLength, other.subnetMaskLength).isEquals(); + if (networkAddress == null) { + if (other.networkAddress != null) + return false; + } else if (!networkAddress.equals(other.networkAddress)) + return false; + if (nodeConnectors == null) { + if (other.nodeConnectors != null) + return false; + } else if (!nodeConnectors.equals(other.nodeConnectors)) + return false; + if (subnetMaskLength != other.subnetMaskLength) + return false; + if (vlan != other.vlan) + return false; + return true; } /* (non-Javadoc) diff --git a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Switch.java b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Switch.java index afca30c3a1..6ba78deade 100644 --- a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Switch.java +++ b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/Switch.java @@ -11,12 +11,11 @@ package org.opendaylight.controller.switchmanager; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; @@ -125,12 +124,44 @@ public class Switch implements Serializable { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(dataLayerAddress); + result = prime * result + ((node == null) ? 0 : node.hashCode()); + result = prime * result + + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode()); + result = prime * result + + ((spanPorts == null) ? 0 : spanPorts.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; + Switch other = (Switch) obj; + if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress)) + return false; + if (node == null) { + if (other.node != null) + return false; + } else if (!node.equals(other.node)) + return false; + if (nodeConnectors == null) { + if (other.nodeConnectors != null) + return false; + } else if (!nodeConnectors.equals(other.nodeConnectors)) + return false; + if (spanPorts == null) { + if (other.spanPorts != null) + return false; + } else if (!spanPorts.equals(other.spanPorts)) + return false; + return true; } @Override diff --git a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SwitchConfig.java b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SwitchConfig.java index ffb8ec042c..778310e07f 100644 --- a/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SwitchConfig.java +++ b/opendaylight/switchmanager/api/src/main/java/org/opendaylight/controller/switchmanager/SwitchConfig.java @@ -11,9 +11,6 @@ package org.opendaylight.controller.switchmanager; import java.io.Serializable; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; - /** * The class describes a switch configuration including node identifier, node * name, tier number and proactive/reactive mode. @@ -59,11 +56,45 @@ public class SwitchConfig implements Serializable { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = 1; + result = prime * result + + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((mode == null) ? 0 : mode.hashCode()); + result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode()); + result = prime * result + ((tier == null) ? 0 : tier.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; + SwitchConfig other = (SwitchConfig) obj; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + if (mode == null) { + if (other.mode != null) + return false; + } else if (!mode.equals(other.mode)) + return false; + if (nodeId == null) { + if (other.nodeId != null) + return false; + } else if (!nodeId.equals(other.nodeId)) + return false; + if (tier == null) { + if (other.tier != null) + return false; + } else if (!tier.equals(other.tier)) + return false; + return true; } } diff --git a/opendaylight/usermanager/src/main/java/org/opendaylight/controller/usermanager/internal/UserConfig.java b/opendaylight/usermanager/src/main/java/org/opendaylight/controller/usermanager/internal/UserConfig.java index fd491fe880..176af3a3e4 100644 --- a/opendaylight/usermanager/src/main/java/org/opendaylight/controller/usermanager/internal/UserConfig.java +++ b/opendaylight/usermanager/src/main/java/org/opendaylight/controller/usermanager/internal/UserConfig.java @@ -15,8 +15,6 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; import org.opendaylight.controller.sal.authorization.AuthResultEnum; import org.opendaylight.controller.sal.utils.Status; import org.opendaylight.controller.sal.utils.StatusCode; @@ -66,12 +64,40 @@ public class UserConfig implements Serializable { @Override public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); + final int prime = 31; + int result = 1; + result = prime * result + + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((roles == null) ? 0 : roles.hashCode()); + result = prime * result + ((user == null) ? 0 : user.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; + UserConfig other = (UserConfig) obj; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; + if (roles == null) { + if (other.roles != null) + return false; + } else if (!roles.equals(other.roles)) + return false; + if (user == null) { + if (other.user != null) + return false; + } else if (!user.equals(other.user)) + return false; + return true; } @Override -- 2.36.6