Moved to Object.equals and Object.hashCode in domain objects

This commit is contained in:
Mattias Holmqvist 2011-12-15 22:50:56 +01:00
parent 41a6bb4355
commit 9965d76e91
7 changed files with 152 additions and 64 deletions

View File

@ -19,8 +19,11 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import org.virtualbox_4_1.DeviceType; import org.virtualbox_4_1.DeviceType;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Represents a specification for a device attachment. * Represents a specification for a device attachment.
* <p/> * <p/>
@ -56,4 +59,60 @@ public class DeviceDetails {
return deviceType; return deviceType;
} }
public static Builder builder() {
return new Builder();
}
public static class Builder {
private int port;
private int deviceSlot;
private DeviceType deviceType;
public Builder port(int port) {
this.port = port;
return this;
}
public Builder deviceType(DeviceType deviceType) {
this.deviceType = deviceType;
return this;
}
public Builder deviceSlot(int slot) {
this.deviceSlot = slot;
return this;
}
public DeviceDetails build() {
checkNotNull(deviceType, "deviceType");
return new DeviceDetails(port, deviceSlot, deviceType);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof DeviceDetails) {
DeviceDetails other = (DeviceDetails) o;
return Objects.equal(port, other.port) &&
Objects.equal(deviceSlot, other.deviceSlot)
&& Objects.equal(deviceType, other.deviceType);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(port, deviceSlot, deviceType);
}
@Override
public String toString() {
return "DeviceDetails{" +
"port=" + port +
", deviceSlot=" + deviceSlot +
", deviceType=" + deviceType +
'}';
}
} }

View File

@ -19,14 +19,17 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* A representation of a hard disk in a VirtualBox VM. * A representation of a hard disk in a VirtualBox VM.
* * <p/>
* diskPath is an absolute path to the file that is the location of the storage for the hard disk. * diskPath is an absolute path to the file that is the location of the storage for the hard disk.
* diskFormat is any of the formats supported by ISystemProperties.getMediumFormats() in the VirtualBox API. * diskFormat is any of the formats supported by ISystemProperties.getMediumFormats() in the VirtualBox API.
* This call is platform-dependent so the supported formats differ from host to host. The default format used is VDI. * This call is platform-dependent so the supported formats differ from host to host. The default format used is VDI.
* deviceDetails contains information about how the HardDisk is attached to the StorageController. * deviceDetails contains information about how the HardDisk is attached to the StorageController.
*
*/ */
public class HardDisk { public class HardDisk {
@ -37,6 +40,9 @@ public class HardDisk {
private final DeviceDetails deviceDetails; private final DeviceDetails deviceDetails;
public HardDisk(DeviceDetails deviceDetails, String diskPath, String diskFormat) { public HardDisk(DeviceDetails deviceDetails, String diskPath, String diskFormat) {
checkNotNull(deviceDetails, "deviceDetails");
checkNotNull(diskPath, "diskPath");
checkNotNull(diskFormat, "diskFormat");
this.diskPath = diskPath; this.diskPath = diskPath;
this.diskFormat = diskFormat; this.diskFormat = diskFormat;
this.deviceDetails = deviceDetails; this.deviceDetails = deviceDetails;
@ -57,23 +63,26 @@ public class HardDisk {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o instanceof HardDisk) {
HardDisk hardDisk = (HardDisk) o; HardDisk hardDisk = (HardDisk) o;
return Objects.equal(deviceDetails, hardDisk.deviceDetails) &&
if (deviceDetails != null ? !deviceDetails.equals(hardDisk.deviceDetails) : hardDisk.deviceDetails != null) Objects.equal(diskFormat, hardDisk.diskFormat) &&
Objects.equal(diskPath, hardDisk.diskPath);
}
return false; return false;
if (diskFormat != null ? !diskFormat.equals(hardDisk.diskFormat) : hardDisk.diskFormat != null) return false;
if (diskPath != null ? !diskPath.equals(hardDisk.diskPath) : hardDisk.diskPath != null) return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = diskFormat != null ? diskFormat.hashCode() : 0; return Objects.hashCode(diskPath, diskFormat, deviceDetails);
result = 31 * result + (diskPath != null ? diskPath.hashCode() : 0); }
result = 31 * result + (deviceDetails != null ? deviceDetails.hashCode() : 0);
return result; @Override
public String toString() {
return "HardDisk{" +
"diskFormat='" + diskFormat + '\'' +
", diskPath='" + diskPath + '\'' +
", deviceDetails=" + deviceDetails +
'}';
} }
} }

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to jclouds, Inc. (jclouds) under one or more * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
@ -19,9 +19,11 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
/** /**
* Represents an optical medium (DVD) in a VirtualBox VM. * Represents an optical medium (DVD) in a VirtualBox VM.
* * <p/>
* sourcePath is the location of the .iso file to load the medium from. * sourcePath is the location of the .iso file to load the medium from.
* deviceDetails contains information about how the Dvd is attached to the StorageController. * deviceDetails contains information about how the Dvd is attached to the StorageController.
*/ */
@ -45,21 +47,17 @@ public class IsoImage {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o instanceof IsoImage) {
IsoImage other = (IsoImage) o;
IsoImage dvd = (IsoImage) o; return Objects.equal(deviceDetails, other.deviceDetails) &&
Objects.equal(sourcePath, other.sourcePath);
if (deviceDetails != null ? !deviceDetails.equals(dvd.deviceDetails) : dvd.deviceDetails != null) return false; }
if (sourcePath != null ? !sourcePath.equals(dvd.sourcePath) : dvd.sourcePath != null) return false; return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = deviceDetails != null ? deviceDetails.hashCode() : 0; return Objects.hashCode(deviceDetails, sourcePath);
result = 31 * result + (sourcePath != null ? sourcePath.hashCode() : 0);
return result;
} }
@Override @Override

View File

@ -19,20 +19,21 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import org.virtualbox_4_1.NATProtocol; import org.virtualbox_4_1.NATProtocol;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* Represents a NAT network adapter in VirtualBox. * Represents a NAT network adapter in VirtualBox.
* * <p/>
* redirectRules are the redirect rules that are applied to the network adapter. * redirectRules are the redirect rules that are applied to the network adapter.
*/ */
public class NatAdapter { public class NatAdapter {
private final Set<RedirectRule> redirectRules = new HashSet<RedirectRule>(); private final Set<RedirectRule> redirectRules = Sets.newLinkedHashSet();
public NatAdapter(Set<RedirectRule> redirectRules) { public NatAdapter(Set<RedirectRule> redirectRules) {
this.redirectRules.addAll(redirectRules); this.redirectRules.addAll(redirectRules);
@ -44,8 +45,7 @@ public class NatAdapter {
public static class Builder { public static class Builder {
private Set<RedirectRule> redirectRules = new HashSet<RedirectRule>(); private Set<RedirectRule> redirectRules = Sets.newLinkedHashSet();
private long adapterSlot;
public Builder tcpRedirectRule(String host, int hostPort, String guest, int guestPort) { public Builder tcpRedirectRule(String host, int hostPort, String guest, int guestPort) {
redirectRules.add(new RedirectRule(NATProtocol.TCP, host, hostPort, guest, guestPort)); redirectRules.add(new RedirectRule(NATProtocol.TCP, host, hostPort, guest, guestPort));
@ -70,18 +70,16 @@ public class NatAdapter {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o instanceof NatAdapter) {
NatAdapter other = (NatAdapter) o;
NatAdapter that = (NatAdapter) o; return Objects.equal(redirectRules, other.redirectRules);
}
if (redirectRules != null ? !redirectRules.equals(that.redirectRules) : that.redirectRules != null) return false; return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return redirectRules != null ? redirectRules.hashCode() : 0; return Objects.hashCode(redirectRules);
} }
@Override @Override

View File

@ -19,6 +19,7 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import org.virtualbox_4_1.NATProtocol; import org.virtualbox_4_1.NATProtocol;
/** /**
@ -59,4 +60,34 @@ public class RedirectRule {
public int getGuestPort() { public int getGuestPort() {
return guestPort; return guestPort;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof RedirectRule) {
RedirectRule other = (RedirectRule) o;
return Objects.equal(protocol, other.protocol) &&
Objects.equal(host, other.host) &&
Objects.equal(hostPort, other.hostPort) &&
Objects.equal(guest, other.guest) &&
Objects.equal(guestPort, other.guestPort);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(protocol, host, hostPort, guest, guestPort);
}
@Override
public String toString() {
return "RedirectRule{" +
"protocol=" + protocol +
", host='" + host + '\'' +
", hostPort=" + hostPort +
", guest='" + guest + '\'' +
", guestPort=" + guestPort +
'}';
}
} }

View File

@ -19,6 +19,8 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import java.util.*; import java.util.*;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -122,30 +124,21 @@ public class VmSpec {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o instanceof VmSpec) {
VmSpec other = (VmSpec) o;
VmSpec that = (VmSpec) o; return Objects.equal(vmId, other.vmId) &&
Objects.equal(vmName, other.vmName) &&
if (forceOverwrite != that.forceOverwrite) return false; Objects.equal(osTypeId, other.osTypeId) &&
if (controllers != null ? !controllers.equals(that.controllers) : that.controllers != null) return false; Objects.equal(forceOverwrite, other.forceOverwrite) &&
if (natNetworkAdapters != null ? !natNetworkAdapters.equals(that.natNetworkAdapters) : that.natNetworkAdapters != null) Objects.equal(natNetworkAdapters, other.natNetworkAdapters) &&
Objects.equal(controllers, other.controllers);
}
return false; return false;
if (osTypeId != null ? !osTypeId.equals(that.osTypeId) : that.osTypeId != null) return false;
if (vmId != null ? !vmId.equals(that.vmId) : that.vmId != null) return false;
if (vmName != null ? !vmName.equals(that.vmName) : that.vmName != null) return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = vmName != null ? vmName.hashCode() : 0; return Objects.hashCode(vmId, vmName, osTypeId, forceOverwrite, natNetworkAdapters, controllers);
result = 31 * result + (osTypeId != null ? osTypeId.hashCode() : 0);
result = 31 * result + (vmId != null ? vmId.hashCode() : 0);
result = 31 * result + (forceOverwrite ? 1 : 0);
result = 31 * result + (natNetworkAdapters != null ? natNetworkAdapters.hashCode() : 0);
result = 31 * result + (controllers != null ? controllers.hashCode() : 0);
return result;
} }
@Override @Override

View File

@ -1,4 +1,4 @@
/** /*
* Licensed to jclouds, Inc. (jclouds) under one or more * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information