Signed-off-by: Claudio Bley <cbley@xxxxxxxxxx> --- src/main/java/org/libvirt/Connect.java | 35 +++++++++++++++++ src/main/java/org/libvirt/Domain.java | 48 ++++++++++++++++++++++- src/test/java/org/libvirt/TestJavaBindings.java | 4 ++ 3 files changed, 86 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 047ac33..ab6f9f0 100644 --- a/src/main/java/org/libvirt/Domain.java +++ b/src/main/java/org/libvirt/Domain.java @@ -21,6 +21,8 @@ import com.sun.jna.NativeLong; import com.sun.jna.Pointer; import com.sun.jna.ptr.IntByReference; +import java.util.Arrays; + /** * A virtual machine defined within libvirt. */ @@ -135,10 +137,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 Arrays.equals(getUUID(), 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 @@ -150,6 +194,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 0123e6a..91f8df4 100644 --- a/src/test/java/org/libvirt/TestJavaBindings.java +++ b/src/test/java/org/libvirt/TestJavaBindings.java @@ -132,6 +132,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); @@ -145,6 +146,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.7.9.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list