[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [libvirt-java] [PATCH 01/10] Implement equals and hashCode methods for Connect and Domain



---
 src/main/java/org/libvirt/Connect.java          | 35 +++++++++++++++++++
 src/main/java/org/libvirt/Domain.java           | 46 ++++++++++++++++++++++++-
 src/test/java/org/libvirt/TestJavaBindings.java |  4 +++
 3 files changed, 84 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index fedc60e..d92976e 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -89,6 +89,41 @@ public class Connect {
      */
     protected ConnectionPointer VCP;
 
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((VCP == null) ? 0 : VCP.hashCode());
+        return result;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (!(obj instanceof Connect))
+            return false;
+        Connect other = (Connect) obj;
+        if (VCP == null)
+            return (other.VCP == null);
+        else if (VCP.equals(other.VCP))
+            return true;
+
+        try {
+            return getURI().equals(other.getURI());
+        } catch (LibvirtException e) {
+            throw new RuntimeException("libvirt error testing connect equality", e);
+        }
+    }
+
     /**
      * Protected constructor to return a Connection with ConnectionPointer
      */
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index 4860770..5184abc 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -134,10 +134,52 @@ public class Domain {
      */
     DomainPointer VDP;
 
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((VDP == null) ? 0 : VDP.hashCode());
+        result = prime * result
+            + ((virConnect == null) ? 0 : virConnect.hashCode());
+        return result;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (!(obj instanceof Domain))
+            return false;
+        Domain other = (Domain) obj;
+
+        // return false when this domain belongs to
+        // a different hypervisor than the other
+        if (!this.virConnect.equals(other.virConnect))
+            return false;
+
+        if (VDP == null) return (other.VDP == null);
+
+        if (VDP.equals(other.VDP)) return true;
+
+        try {
+            return getUUID().equals(other.getUUID());
+        } catch (LibvirtException e) {
+            throw new RuntimeException("libvirt error testing domain equality", e);
+        }
+    }
+
     /**
      * The Connect Object that represents the Hypervisor of this Domain
      */
-    private Connect virConnect;
+    private final Connect virConnect;
 
     /**
      * Constructs a Domain object from a known native DomainPointer, and a
@@ -149,6 +191,8 @@ public class Domain {
      *            the native virDomainPtr
      */
     Domain(Connect virConnect, DomainPointer VDP) {
+    	assert virConnect != null;
+
         this.virConnect = virConnect;
         this.VDP = VDP;
     }
diff --git a/src/test/java/org/libvirt/TestJavaBindings.java b/src/test/java/org/libvirt/TestJavaBindings.java
index 3600844..fdd9c33 100644
--- a/src/test/java/org/libvirt/TestJavaBindings.java
+++ b/src/test/java/org/libvirt/TestJavaBindings.java
@@ -130,6 +130,7 @@ public final class TestJavaBindings extends TestCase {
                 + "  <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e67</uuid>" + "  <memory>8388608</memory>"
                 + "  <vcpu>2</vcpu>" + "  <os><type arch='i686'>hvm</type></os>" + "  <on_reboot>restart</on_reboot>"
                 + "  <on_poweroff>destroy</on_poweroff>" + "  <on_crash>restart</on_crash>" + "</domain>", 0);
+        UUID dom2UUID = UUID.fromString("004b96e1-2d78-c30f-5aa5-f03c87d21e67");
 
         assertEquals("Number of domains", 2, conn.numOfDomains());
         assertEquals("Number of listed domains", 2, conn.listDomains().length);
@@ -143,6 +144,9 @@ public final class TestJavaBindings extends TestCase {
         this.validateDomainData(conn.domainLookupByUUID(UUIDArray));
         this.validateDomainData(conn.domainLookupByUUIDString("004b96e1-2d78-c30f-5aa5-f03c87d21e67"));
         this.validateDomainData(conn.domainLookupByUUID(UUID.fromString("004b96e1-2d78-c30f-5aa5-f03c87d21e67")));
+        assertEquals("Domain is not equal to Domain retrieved by lookup",
+                     dom2,
+                     conn.domainLookupByUUID(dom2.getUUID()));
     }
 
     private void validateDomainData(Domain dom) throws Exception {
-- 
1.8.5.2.msysgit.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]