cleaned up toString/equals/hashCode in base options objects

This commit is contained in:
Adrian Cole 2012-03-21 18:44:03 -07:00
parent 6fda2580f5
commit 2373f89c78
4 changed files with 65 additions and 135 deletions

View File

@ -18,7 +18,6 @@
*/ */
package org.jclouds.vcloud.compute.options; package org.jclouds.vcloud.compute.options;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
@ -290,7 +289,7 @@ public class VCloudTemplateOptions extends TemplateOptions implements Cloneable
public String toString() { public String toString() {
return "[customizationScript=" + (customizationScript != null) + ", description=" + description return "[customizationScript=" + (customizationScript != null) + ", description=" + description
+ ", ipAddressAllocationMode=" + ipAddressAllocationMode + ", inboundPorts=" + ", ipAddressAllocationMode=" + ipAddressAllocationMode + ", inboundPorts="
+ Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey=" + inboundPorts + ", privateKey=" + (privateKey != null) + ", publicKey="
+ (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds
+ ", userMetadata: " + userMetadata + "]"; + ", userMetadata: " + userMetadata + "]";
} }

View File

@ -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.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
@ -278,7 +277,7 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions implements C
@Override @Override
public String toString() { public String toString() {
return "TerremarkVCloudTemplateOptions [keyPair=" + keyPair + ", noKeyPair=" + noKeyPair + ", inboundPorts=" 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 + (publicKey != null) + ", runScript=" + (script != null) + ", port:seconds=" + port + ":" + seconds
+ ", userMetadata: " + userMetadata + "]"; + ", userMetadata: " + userMetadata + "]";
} }

View File

@ -18,16 +18,20 @@
*/ */
package org.jclouds.compute.options; 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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Optional;
import org.jclouds.compute.functions.DefaultCredentialsFromImageOrOverridingCredentials; import org.jclouds.compute.functions.DefaultCredentialsFromImageOrOverridingCredentials;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.domain.LoginCredentials; import org.jclouds.domain.LoginCredentials;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.util.CredentialUtils; 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. * Enables additional options for running a script.
* *
@ -509,75 +513,36 @@ public class RunScriptOptions {
} }
@Override @Override
public String toString() { public boolean equals(Object o) {
return "[loginUser=" + loginUser + ", loginPasswordPresent=" + (loginPassword != null) if (this == o)
+ ", loginPrivateKeyPresent=" + (loginPrivateKey != null) + ", shouldAuthenticateSudo=" + authenticateSudo return true;
+ ", port:seconds=" + port + ":" + seconds + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" if (o == null || getClass() != o.getClass())
+ blockOnComplete + ", wrapInInitScript=" + wrapInInitScript + "]"; 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 @Override
public int hashCode() { public int hashCode() {
final int prime = 31; return Objects.hashCode(loginUser, loginPassword, loginPrivateKey, authenticateSudo, port, seconds, taskName,
int result = 1; taskName, blockOnComplete, wrapInInitScript);
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;
} }
@Override @Override
public boolean equals(Object obj) { public String toString() {
if (this == obj) return string().toString();
return true; }
if (obj == null)
return false; protected ToStringHelper string() {
if (getClass() != obj.getClass()) return Objects.toStringHelper("").add("loginUser", loginUser)
return false; .add("loginPasswordPresent", (loginPassword != null))
RunScriptOptions other = (RunScriptOptions) obj; .add("loginPrivateKeyPresent", (loginPrivateKey != null)).add("authenticateSudo", authenticateSudo)
if (authenticateSudo == null) { .add("port:seconds", port + ":" + seconds).add("taskName", taskName).add("runAsRoot", runAsRoot)
if (other.authenticateSudo != null) .add("blockOnComplete", blockOnComplete).add("wrapInInitScript", wrapInInitScript);
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;
} }
} }

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.compute.options; 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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; 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.scriptbuilder.domain.Statements;
import org.jclouds.util.Strings2; 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.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
/** /**
* Contains options supported in the {@code ComputeService#createNodesInGroup} * 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<Integer> inboundPorts = ImmutableSet.of(22);
protected Statement script; protected Statement script;
@ -348,8 +352,34 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
protected Map<String, String> userMetadata = Maps.newLinkedHashMap(); protected Map<String, String> 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() { public int[] getInboundPorts() {
return inboundPorts; return Ints.toArray(inboundPorts);
} }
public Statement getRunScript() { public Statement getRunScript() {
@ -453,7 +483,7 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
public TemplateOptions inboundPorts(int... ports) { public TemplateOptions inboundPorts(int... ports) {
for (int port : ports) for (int port : ports)
checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535"); checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535");
this.inboundPorts = ports; this.inboundPorts = ImmutableSet.copyOf(Ints.asList(ports));
return this; return this;
} }
@ -616,14 +646,6 @@ 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 + "]";
}
/** /**
* <h4>Note</h4> As of version 1.1.0, this option is incompatible with * <h4>Note</h4> As of version 1.1.0, this option is incompatible with
* {@link TemplateOptions#runScript(Statement)} and * {@link TemplateOptions#runScript(Statement)} and
@ -670,61 +692,6 @@ public class TemplateOptions extends RunScriptOptions implements Cloneable {
return userMetadata; 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 @Override
public TemplateOptions blockOnPort(int port, int seconds) { public TemplateOptions blockOnPort(int port, int seconds) {
return TemplateOptions.class.cast(super.blockOnPort(port, seconds)); return TemplateOptions.class.cast(super.blockOnPort(port, seconds));