From 2373f89c78f4a66b3c817524d1df05f870471648 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 21 Mar 2012 18:44:03 -0700 Subject: [PATCH] cleaned up toString/equals/hashCode in base options objects --- .../options/VCloudTemplateOptions.java | 3 +- .../TerremarkVCloudTemplateOptions.java | 3 +- .../compute/options/RunScriptOptions.java | 91 +++++----------- .../compute/options/TemplateOptions.java | 103 ++++++------------ 4 files changed, 65 insertions(+), 135 deletions(-) diff --git a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java index 1e5404f11e..4b11c21b96 100644 --- a/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java +++ b/apis/vcloud/src/main/java/org/jclouds/vcloud/compute/options/VCloudTemplateOptions.java @@ -18,7 +18,6 @@ */ package org.jclouds.vcloud.compute.options; -import java.util.Arrays; import java.util.Map; import org.jclouds.compute.options.TemplateOptions; @@ -290,7 +289,7 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable public String toString() { return "[customizationScript=" + (customizationScript != null) + ", description=" + description + ", ipAddressAllocationMode=" + ipAddressAllocationMode + ", inboundPorts=" - + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" + + inboundPorts + ", privateKey=" + (privateKey != null) + ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds + ", userMetadata: " + userMetadata + "]"; } diff --git a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java index 3ff3904f06..09841ef8e4 100644 --- a/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java +++ b/common/trmk/src/main/java/org/jclouds/trmk/vcloud_0_8/compute/options/TerremarkVCloudTemplateOptions.java @@ -21,7 +21,6 @@ package org.jclouds.trmk.vcloud_0_8.compute.options; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import java.util.Arrays; import java.util.Map; import org.jclouds.compute.options.TemplateOptions; @@ -278,7 +277,7 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions implements C @Override public String toString() { return "TerremarkVCloudTemplateOptions [keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", inboundPorts=" - + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" + + inboundPorts + ", privateKey=" + (privateKey != null) + ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds + ", userMetadata: " + userMetadata + "]"; } diff --git a/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java b/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java index d185c39372..09f281e73f 100644 --- a/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java +++ b/compute/src/main/java/org/jclouds/compute/options/RunScriptOptions.java @@ -18,16 +18,20 @@ */ package org.jclouds.compute.options; +import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import com.google.common.base.Optional; import org.jclouds.compute.functions.DefaultCredentialsFromImageOrOverridingCredentials; import org.jclouds.domain.Credentials; import org.jclouds.domain.LoginCredentials; import org.jclouds.javax.annotation.Nullable; import org.jclouds.util.CredentialUtils; +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; +import com.google.common.base.Optional; + /** * Enables additional options for running a script. * @@ -509,75 +513,36 @@ public class RunScriptOptions { } @Override - public String toString() { - return "[loginUser=" + loginUser + ", loginPasswordPresent=" + (loginPassword != null) - + ", loginPrivateKeyPresent=" + (loginPrivateKey != null) + ", shouldAuthenticateSudo=" + authenticateSudo - + ", port:seconds=" + port + ":" + seconds + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" - + blockOnComplete + ", wrapInInitScript=" + wrapInInitScript + "]"; + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + RunScriptOptions that = RunScriptOptions.class.cast(o); + return equal(this.loginUser, that.loginUser) && equal(this.loginPassword, that.loginPassword) + && equal(this.loginPrivateKey, that.loginPrivateKey) && equal(this.authenticateSudo, that.authenticateSudo) + && equal(this.port, that.port) && equal(this.seconds, that.seconds) && equal(this.taskName, that.taskName) + && equal(this.runAsRoot, that.runAsRoot) && equal(this.blockOnComplete, that.blockOnComplete) + && equal(this.wrapInInitScript, that.wrapInInitScript); } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((authenticateSudo == null) ? 0 : authenticateSudo.hashCode()); - result = prime * result + (blockOnComplete ? 1231 : 1237); - result = prime * result + ((loginPassword == null) ? 0 : loginPassword.hashCode()); - result = prime * result + ((loginPrivateKey == null) ? 0 : loginPrivateKey.hashCode()); - result = prime * result + ((loginUser == null) ? 0 : loginUser.hashCode()); - result = prime * result + port; - result = prime * result + (runAsRoot ? 1231 : 1237); - result = prime * result + seconds; - result = prime * result + ((taskName == null) ? 0 : taskName.hashCode()); - result = prime * result + (wrapInInitScript ? 1231 : 1237); - return result; + return Objects.hashCode(loginUser, loginPassword, loginPrivateKey, authenticateSudo, port, seconds, taskName, + taskName, blockOnComplete, wrapInInitScript); } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - RunScriptOptions other = (RunScriptOptions) obj; - if (authenticateSudo == null) { - if (other.authenticateSudo != null) - return false; - } else if (!authenticateSudo.equals(other.authenticateSudo)) - return false; - if (blockOnComplete != other.blockOnComplete) - return false; - if (loginPassword == null) { - if (other.loginPassword != null) - return false; - } else if (!loginPassword.equals(other.loginPassword)) - return false; - if (loginPrivateKey == null) { - if (other.loginPrivateKey != null) - return false; - } else if (!loginPrivateKey.equals(other.loginPrivateKey)) - return false; - if (loginUser == null) { - if (other.loginUser != null) - return false; - } else if (!loginUser.equals(other.loginUser)) - return false; - if (port != other.port) - return false; - if (runAsRoot != other.runAsRoot) - return false; - if (seconds != other.seconds) - return false; - if (taskName == null) { - if (other.taskName != null) - return false; - } else if (!taskName.equals(other.taskName)) - return false; - if (wrapInInitScript != other.wrapInInitScript) - return false; - return true; + public String toString() { + return string().toString(); + } + + protected ToStringHelper string() { + return Objects.toStringHelper("").add("loginUser", loginUser) + .add("loginPasswordPresent", (loginPassword != null)) + .add("loginPrivateKeyPresent", (loginPrivateKey != null)).add("authenticateSudo", authenticateSudo) + .add("port:seconds", port + ":" + seconds).add("taskName", taskName).add("runAsRoot", runAsRoot) + .add("blockOnComplete", blockOnComplete).add("wrapInInitScript", wrapInInitScript); } } diff --git a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java index 1902dfa441..ead24aff09 100644 --- a/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java +++ b/compute/src/main/java/org/jclouds/compute/options/TemplateOptions.java @@ -18,6 +18,7 @@ */ package org.jclouds.compute.options; +import static com.google.common.base.Objects.equal; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -34,9 +35,12 @@ import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statements; import org.jclouds.util.Strings2; +import com.google.common.base.Objects; +import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; +import com.google.common.primitives.Ints; /** * Contains options supported in the {@code ComputeService#createNodesInGroup} @@ -334,7 +338,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable { } - protected int[] inboundPorts = new int[] { 22 }; + protected Set inboundPorts = ImmutableSet.of(22); protected Statement script; @@ -348,8 +352,34 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable { protected Map userMetadata = Maps.newLinkedHashMap(); + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + TemplateOptions that = TemplateOptions.class.cast(o); + return super.equals(that) && equal(this.inboundPorts, that.inboundPorts) && equal(this.script, that.script) + && equal(this.publicKey, that.publicKey) && equal(this.privateKey, that.privateKey) + && equal(this.blockUntilRunning, that.blockUntilRunning) && equal(this.tags, that.tags) + && equal(this.userMetadata, that.userMetadata); + } + + @Override + public int hashCode() { + return Objects.hashCode(super.hashCode(), inboundPorts, script, publicKey, privateKey, blockUntilRunning, tags, + userMetadata); + } + + @Override + public ToStringHelper string() { + return super.string().add("inboundPorts", inboundPorts).add("scriptPresent", script != null) + .add("publicKeyPresent", publicKey != null).add("privateKeyPresent", privateKey != null).add("tags", tags) + .add("blockUntilRunning", blockUntilRunning).add("tags", tags).add("userMetadata", userMetadata); + } + public int[] getInboundPorts() { - return inboundPorts; + return Ints.toArray(inboundPorts); } public Statement getRunScript() { @@ -453,7 +483,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable { public TemplateOptions inboundPorts(int... ports) { for (int port : ports) checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535"); - this.inboundPorts = ports; + this.inboundPorts = ImmutableSet.copyOf(Ints.asList(ports)); return this; } @@ -615,15 +645,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable { } } - - @Override - public String toString() { - return "[inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" - + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + blockUntilRunning - + ", blockOnComplete=" + blockOnComplete + ", port:seconds=" + port + ":" + seconds + ", userMetadata: " - + userMetadata + "]"; - } - + /** *

Note

As of version 1.1.0, this option is incompatible with * {@link TemplateOptions#runScript(Statement)} and @@ -669,62 +691,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable { public Map getUserMetadata() { return userMetadata; } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (blockUntilRunning ? 1231 : 1237); - result = prime * result + Arrays.hashCode(inboundPorts); - result = prime * result + port; - result = prime * result + ((userMetadata == null) ? 0 : userMetadata.hashCode()); - result = prime * result + ((privateKey == null) ? 0 : privateKey.hashCode()); - result = prime * result + ((publicKey == null) ? 0 : publicKey.hashCode()); - result = prime * result + ((script == null) ? 0 : script.hashCode()); - result = prime * result + seconds; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - TemplateOptions other = (TemplateOptions) obj; - if (blockUntilRunning != other.blockUntilRunning) - return false; - if (!Arrays.equals(inboundPorts, other.inboundPorts)) - return false; - if (userMetadata == null) { - if (other.userMetadata != null) - return false; - } else if (!userMetadata.equals(other.userMetadata)) - return false; - if (port != other.port) - return false; - if (privateKey == null) { - if (other.privateKey != null) - return false; - } else if (!privateKey.equals(other.privateKey)) - return false; - if (publicKey == null) { - if (other.publicKey != null) - return false; - } else if (!publicKey.equals(other.publicKey)) - return false; - if (script == null) { - if (other.script != null) - return false; - } else if (!script.equals(other.script)) - return false; - if (seconds != other.seconds) - return false; - return true; - } - + @Override public TemplateOptions blockOnPort(int port, int seconds) { return TemplateOptions.class.cast(super.blockOnPort(port, seconds));