Add equals and hashcode methods to IPv4Net, Ipv6 and IPv6Net
Change-Id: I1f2e7923e7d3a72b226c4c0122d851291b89c6c3
diff --git a/src/main/java/net/onrc/onos/core/util/IPv4Net.java b/src/main/java/net/onrc/onos/core/util/IPv4Net.java
index 3b8fad6..66b98a3 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv4Net.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv4Net.java
@@ -1,5 +1,7 @@
package net.onrc.onos.core.util;
+import java.util.Objects;
+
import net.onrc.onos.core.util.serializers.IPv4NetDeserializer;
import net.onrc.onos.core.util.serializers.IPv4NetSerializer;
@@ -91,4 +93,39 @@
public String toString() {
return this.address.toString() + "/" + this.prefixLen;
}
+
+ /**
+ * Compares the value of two IPv4Net objects.
+ * <p/>
+ * Note the value of the IPv4 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 IPv4Net)) {
+ return false;
+ }
+
+ IPv4Net otherIpv4Net = (IPv4Net) other;
+
+ return Objects.equals(this.address, otherIpv4Net.address)
+ && this.prefixLen == otherIpv4Net.prefixLen;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(address, prefixLen);
+ }
}
diff --git a/src/main/java/net/onrc/onos/core/util/IPv6.java b/src/main/java/net/onrc/onos/core/util/IPv6.java
index cd1a192..346d49b 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv6.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv6.java
@@ -1,5 +1,7 @@
package net.onrc.onos.core.util;
+import java.util.Objects;
+
import net.onrc.onos.core.util.serializers.IPv6Deserializer;
import net.onrc.onos.core.util.serializers.IPv6Serializer;
@@ -87,4 +89,19 @@
return HexString.toHexString(this.valueHigh) + ":" +
HexString.toHexString(this.valueLow);
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof IPv6)) {
+ return false;
+ }
+ IPv6 other = (IPv6) o;
+ return this.valueHigh == other.valueHigh
+ && this.valueLow == other.valueLow;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(valueHigh, valueLow);
+ }
}
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);
+ }
}