Add equals and hashcode methods to IPv4Net, Ipv6 and IPv6Net
Change-Id: I1f2e7923e7d3a72b226c4c0122d851291b89c6c3
diff --git a/src/main/java/net/onrc/onos/core/util/IPv6Net.java b/src/main/java/net/onrc/onos/core/util/IPv6Net.java
index 064e22e..22e5e22 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv6Net.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv6Net.java
@@ -1,5 +1,7 @@
package net.onrc.onos.core.util;
+import java.util.Objects;
+
import net.onrc.onos.core.util.serializers.IPv6NetDeserializer;
import net.onrc.onos.core.util.serializers.IPv6NetSerializer;
@@ -91,4 +93,39 @@
public String toString() {
return this.address.toString() + "/" + this.prefixLen;
}
+
+ /**
+ * Compares the value of two IPv6Net objects.
+ * <p/>
+ * Note the value of the IPv6 address is compared directly between the
+ * objects, and must match exactly for the objects to be considered equal.
+ * This may result in objects which represent the same IP prefix being
+ * classified as unequal, because the unsignificant bits of the address
+ * field don't match (the bits to the right of the prefix length).
+ * <p/>
+ * TODO Change this behavior so that objects that represent the same prefix
+ * are classified as equal according to this equals method.
+ *
+ * @see Object#equals(Object)
+ */
+ @Override
+ public boolean equals(Object other) {
+ if (other == this) {
+ return true;
+ }
+
+ if (!(other instanceof IPv6Net)) {
+ return false;
+ }
+
+ IPv6Net otherIpv6Net = (IPv6Net) other;
+
+ return Objects.equals(this.address, otherIpv6Net.address)
+ && this.prefixLen == otherIpv6Net.prefixLen;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(address, prefixLen);
+ }
}