Added null checks in contructors and more use of Objects.equal/hashCode.

This commit is contained in:
Mattias Holmqvist 2011-12-15 23:22:36 +01:00
parent 6d435742e1
commit 1fd77eb348
6 changed files with 34 additions and 16 deletions

View File

@ -42,6 +42,7 @@ public class DeviceDetails {
private final DeviceType deviceType; private final DeviceType deviceType;
public DeviceDetails(int port, int deviceSlot, DeviceType deviceType) { public DeviceDetails(int port, int deviceSlot, DeviceType deviceType) {
checkNotNull(deviceType, "deviceType");
this.port = port; this.port = port;
this.deviceSlot = deviceSlot; this.deviceSlot = deviceSlot;
this.deviceType = deviceType; this.deviceType = deviceType;

View File

@ -21,6 +21,8 @@ package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Represents an optical medium (DVD) in a VirtualBox VM. * Represents an optical medium (DVD) in a VirtualBox VM.
* <p/> * <p/>
@ -32,6 +34,8 @@ public class IsoImage {
private String sourcePath; private String sourcePath;
public IsoImage(DeviceDetails deviceDetails, String sourcePath) { public IsoImage(DeviceDetails deviceDetails, String sourcePath) {
checkNotNull(deviceDetails, "deviceDetails");
checkNotNull(sourcePath, "sourcePath");
this.deviceDetails = deviceDetails; this.deviceDetails = deviceDetails;
this.sourcePath = sourcePath; this.sourcePath = sourcePath;
} }

View File

@ -26,6 +26,8 @@ import org.virtualbox_4_1.NATProtocol;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Represents a NAT network adapter in VirtualBox. * Represents a NAT network adapter in VirtualBox.
* <p/> * <p/>
@ -36,6 +38,7 @@ public class NatAdapter {
private final Set<RedirectRule> redirectRules = Sets.newLinkedHashSet(); private final Set<RedirectRule> redirectRules = Sets.newLinkedHashSet();
public NatAdapter(Set<RedirectRule> redirectRules) { public NatAdapter(Set<RedirectRule> redirectRules) {
checkNotNull(redirectRules);
this.redirectRules.addAll(redirectRules); this.redirectRules.addAll(redirectRules);
} }

View File

@ -22,6 +22,8 @@ package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import org.virtualbox_4_1.NATProtocol; import org.virtualbox_4_1.NATProtocol;
import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* @author Mattias Holmqvist * @author Mattias Holmqvist
*/ */
@ -34,6 +36,9 @@ public class RedirectRule {
private final int guestPort; private final int guestPort;
public RedirectRule(NATProtocol protocol, String host, int hostPort, String guest, int guestPort) { public RedirectRule(NATProtocol protocol, String host, int hostPort, String guest, int guestPort) {
checkNotNull(protocol);
checkNotNull(host);
checkNotNull(guest);
this.protocol = protocol; this.protocol = protocol;
this.host = host; this.host = host;
this.hostPort = hostPort; this.hostPort = hostPort;

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.DeviceType; import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.StorageBus; import org.virtualbox_4_1.StorageBus;
@ -30,7 +31,7 @@ import static org.jclouds.virtualbox.domain.HardDisk.DEFAULT_DISK_FORMAT;
/** /**
* Represents a storage controller in a VirtualBox VM. * Represents a storage controller in a VirtualBox VM.
* * <p/>
* name is the unique name of the controller. * name is the unique name of the controller.
* bus is the type of connection bus for the controller * bus is the type of connection bus for the controller
* hardDisks contains the hard disks that are attached (or should be attached) to this controller * hardDisks contains the hard disks that are attached (or should be attached) to this controller
@ -45,6 +46,10 @@ public class StorageController {
private Set<IsoImage> isoImages; private Set<IsoImage> isoImages;
public StorageController(String name, StorageBus bus, Set<HardDisk> hardDisks, Set<IsoImage> isoImages) { public StorageController(String name, StorageBus bus, Set<HardDisk> hardDisks, Set<IsoImage> isoImages) {
checkNotNull(name, "name");
checkNotNull(bus, "bus");
checkNotNull(hardDisks, "hardDisks");
checkNotNull(isoImages, "isoImages");
this.name = name; this.name = name;
this.bus = bus; this.bus = bus;
this.hardDisks = hardDisks; this.hardDisks = hardDisks;
@ -70,25 +75,19 @@ public class StorageController {
@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 StorageController) {
StorageController other = (StorageController) o;
StorageController that = (StorageController) o; return Objects.equal(name, other.name) &&
Objects.equal(bus, other.bus) &&
if (bus != that.bus) return false; Objects.equal(hardDisks, other.hardDisks) &&
if (isoImages != null ? !isoImages.equals(that.isoImages) : that.isoImages != null) return false; Objects.equal(isoImages, other.isoImages);
if (hardDisks != null ? !hardDisks.equals(that.hardDisks) : that.hardDisks != null) return false; }
if (name != null ? !name.equals(that.name) : that.name != null) return false; return false;
return true;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = name != null ? name.hashCode() : 0; return Objects.hashCode(name, bus, hardDisks, isoImages);
result = 31 * result + (bus != null ? bus.hashCode() : 0);
result = 31 * result + (hardDisks != null ? hardDisks.hashCode() : 0);
result = 31 * result + (isoImages != null ? isoImages.hashCode() : 0);
return result;
} }
@Override @Override

View File

@ -25,6 +25,7 @@ import java.util.*;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/** /**
* A description of a Virtual Machine in VirtualBox. * A description of a Virtual Machine in VirtualBox.
@ -40,6 +41,11 @@ public class VmSpec {
private final Set<StorageController> controllers; private final Set<StorageController> controllers;
public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) { public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) {
checkNotNull(vmId, "vmId");
checkNotNull(vmName, "vmName");
checkArgument(memory > 0, "memory must be > 0");
checkNotNull(controllers, "controllers");
checkNotNull(natNetworkAdapters, "natNetworkAdapters");
this.vmId = vmId; this.vmId = vmId;
this.vmName = vmName; this.vmName = vmName;
this.osTypeId = osTypeId; this.osTypeId = osTypeId;