Fix issues found by FindBugs: EQ_COMPARETO_USE_OBJECT_EQUALS
http://findbugs.sourceforge.net/bugDescriptions.html#EQ_COMPARETO_USE_OBJECT_EQUALS
* For each Comparable class that has compareTo() method, add
the corresponding equals() and hashCode() methods.
* Added/updated some of the Javadoc for the compareTo() methods
* Added a TODO comment inside class ControllerRegistryEntry
Change-Id: Idda67721329a21227c2136a4a2b8ee3367e520bc
diff --git a/src/main/java/net/onrc/onos/core/registry/ControllerRegistryEntry.java b/src/main/java/net/onrc/onos/core/registry/ControllerRegistryEntry.java
index 7e13fdd..569a922 100644
--- a/src/main/java/net/onrc/onos/core/registry/ControllerRegistryEntry.java
+++ b/src/main/java/net/onrc/onos/core/registry/ControllerRegistryEntry.java
@@ -4,7 +4,11 @@
public class ControllerRegistryEntry implements Comparable<ControllerRegistryEntry> {
-
+ //
+ // TODO: Refactor the implementation and decide whether controllerId
+ // is needed. If "yes", we might need to consider it inside the
+ // compareTo(), equals() and hashCode() impmenetations.
+ //
private String controllerId;
private int sequenceNumber;
@@ -18,10 +22,47 @@
return controllerId;
}
+ /**
+ * Compares this object with the specified object for order.
+ * NOTE: the test is based on ControllerRegistryEntry sequence numbers,
+ * and doesn't include the controllerId.
+ *
+ * @param o the object to be compared.
+ * @return a negative integer, zero, or a positive integer as this object
+ * is less than, equal to, or greater than the specified object.
+ */
@Override
public int compareTo(ControllerRegistryEntry o) {
- return sequenceNumber - o.sequenceNumber;
- //return 0;
+ return this.sequenceNumber - o.sequenceNumber;
}
+ /**
+ * Test whether some other object is "equal to" this one.
+ * NOTE: the test is based on ControllerRegistryEntry sequence numbers,
+ * and doesn't include the controllerId.
+ *
+ * @param obj the reference object with which to compare.
+ * @return true if this object is the same as the obj argument; false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof ControllerRegistryEntry) {
+ ControllerRegistryEntry other = (ControllerRegistryEntry) obj;
+ return this.sequenceNumber == other.sequenceNumber;
+ }
+ return false;
+ }
+
+ /**
+ * Get the hash code for the object.
+ * NOTE: the computation is based on ControllerRegistryEntry sequence
+ * numbers, and doesn't include the controller ID.
+ *
+ * @return a hash code value for this object.
+ */
+ @Override
+ public int hashCode() {
+ return Integer.valueOf(this.sequenceNumber).hashCode();
+ }
}
diff --git a/src/main/java/net/onrc/onos/core/util/FlowId.java b/src/main/java/net/onrc/onos/core/util/FlowId.java
index f57701e..d397918 100644
--- a/src/main/java/net/onrc/onos/core/util/FlowId.java
+++ b/src/main/java/net/onrc/onos/core/util/FlowId.java
@@ -87,11 +87,38 @@
* Compare two FlowId objects numerically using their Flow IDs.
*
* @return the value 0 if the Flow ID is equal to the argument's Flow ID;
- * a value less than 0 if the Flow ID is numerically less than the argument's Flow ID;
- * and a value greater than 0 if the Flow ID is numerically greater than the argument's Flow ID.
+ * a value less than 0 if the Flow ID is numerically less than the
+ * argument's Flow ID; and a value greater than 0 if the Flow ID is
+ * numerically greater than the argument's Flow ID.
*/
@Override
public int compareTo(FlowId o) {
return Long.valueOf(this.value).compareTo(o.value());
}
+
+ /**
+ * Test whether some other object is "equal to" this one.
+ *
+ * @param obj the reference object with which to compare.
+ * @return true if this object is the same as the obj argument; false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof FlowId) {
+ FlowId other = (FlowId) obj;
+ return (this.value == other.value);
+ }
+ return false;
+ }
+
+ /**
+ * Get the hash code for the object.
+ *
+ * @return a hash code value for this object.
+ */
+ @Override
+ public int hashCode() {
+ return Long.valueOf(this.value).hashCode();
+ }
}
diff --git a/src/main/java/net/onrc/onos/core/util/FlowPath.java b/src/main/java/net/onrc/onos/core/util/FlowPath.java
index fe51f08..0286bd2 100644
--- a/src/main/java/net/onrc/onos/core/util/FlowPath.java
+++ b/src/main/java/net/onrc/onos/core/util/FlowPath.java
@@ -327,10 +327,43 @@
}
/**
- * CompareTo method to order flowPath by Id
+ * Compares this object with the specified object for order.
+ * NOTE: The test is based on the Flow ID.
+ *
+ * @param o the object to be compared.
+ * @return a negative integer, zero, or a positive integer as this object
+ * is less than, equal to, or greater than the specified object.
*/
@Override
public int compareTo(FlowPath f) {
- return (int) (this.flowId.value() - f.flowId.value());
+ return (this.flowId.compareTo(f.flowId()));
+ }
+
+ /**
+ * Test whether some other object is "equal to" this one.
+ * NOTE: The test is based on the Flow ID.
+ *
+ * @param obj the reference object with which to compare.
+ * @return true if this object is the same as the obj argument; false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof FlowPath) {
+ FlowPath other = (FlowPath) obj;
+ return (this.flowId.equals(other.flowId()));
+ }
+ return false;
+ }
+
+ /**
+ * Get the hash code for the object.
+ * NOTE: The computation is based on the Flow ID.
+ *
+ * @return a hash code value for this object.
+ */
+ @Override
+ public int hashCode() {
+ return this.flowId.hashCode();
}
}