From cf0b725b26cb29cbeb9eb346922e1ac964b7e56b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 7 Mar 2012 12:59:58 -0800 Subject: [PATCH] refactored hashCode, etc. as in NL sometimes date fields are not present --- .../org/jclouds/gogrid/domain/Server.java | 99 ++++------------- .../jclouds/gogrid/domain/ServerImage.java | 101 +++++------------- 2 files changed, 50 insertions(+), 150 deletions(-) diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/Server.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/Server.java index 1804864f80..d9b872754e 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/Server.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/Server.java @@ -18,7 +18,10 @@ */ package org.jclouds.gogrid.domain; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; +import com.google.common.base.Objects; import com.google.common.primitives.Longs; /** @@ -46,7 +49,7 @@ public class Server implements Comparable { } public Server(long id, Option datacenter, boolean sandbox, String name, String description, ServerState state, - Option type, Option ram, Option os, Ip ip, ServerImage image) { + Option type, Option ram, Option os, Ip ip, ServerImage image) { this.id = id; this.isSandbox = sandbox; this.name = name; @@ -105,93 +108,37 @@ public class Server implements Comparable { } @Override - public boolean equals(Object obj) { - if (this == obj) + public boolean equals(Object object) { + if (this == object) { return true; - if (obj == null) + } + if (object instanceof Server) { + final Server other = Server.class.cast(object); + return equal(id, other.id) && equal(name, other.name); + } else { return false; - if (getClass() != obj.getClass()) - return false; - Server other = (Server) obj; - if (datacenter == null) { - if (other.datacenter != null) - return false; - } else if (!datacenter.equals(other.datacenter)) - return false; - if (description == null) { - if (other.description != null) - return false; - } else if (!description.equals(other.description)) - return false; - if (id != other.id) - return false; - if (image == null) { - if (other.image != null) - return false; - } else if (!image.equals(other.image)) - return false; - if (ip == null) { - if (other.ip != null) - return false; - } else if (!ip.equals(other.ip)) - return false; - if (isSandbox != other.isSandbox) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (os == null) { - if (other.os != null) - return false; - } else if (!os.equals(other.os)) - return false; - if (ram == null) { - if (other.ram != null) - return false; - } else if (!ram.equals(other.ram)) - return false; - if (state == null) { - if (other.state != null) - return false; - } else if (!state.equals(other.state)) - return false; - if (type == null) { - if (other.type != null) - return false; - } else if (!type.equals(other.type)) - return false; - return true; + } } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((datacenter == null) ? 0 : datacenter.hashCode()); - result = prime * result + ((description == null) ? 0 : description.hashCode()); - result = prime * result + (int) (id ^ (id >>> 32)); - result = prime * result + ((image == null) ? 0 : image.hashCode()); - result = prime * result + ((ip == null) ? 0 : ip.hashCode()); - result = prime * result + (isSandbox ? 1231 : 1237); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((os == null) ? 0 : os.hashCode()); - result = prime * result + ((ram == null) ? 0 : ram.hashCode()); - result = prime * result + ((state == null) ? 0 : state.hashCode()); - result = prime * result + ((type == null) ? 0 : type.hashCode()); - return result; + return Objects.hashCode(id, name); } @Override - public int compareTo(Server o) { - return Longs.compare(id, o.getId()); + public int compareTo(Server that) { + if (that == null) + return 1; + if (this == that) + return 0; + return Longs.compare(id, that.getId()); } @Override public String toString() { - return "Server [datacenter=" + datacenter + ", description=" + description + ", id=" + id + ", image=" + image - + ", ip=" + ip + ", isSandbox=" + isSandbox + ", name=" + name + ", os=" + os + ", ram=" + ram + ", state=" - + state + ", type=" + type + "]"; + return toStringHelper("").add("id", id).add("name", name).add("description", description).add("os", os).add( + "image", image).add("datacenter", datacenter).add("state", state).add("ip", ip).add("isSandbox", + isSandbox).add("ram", ram).add("type", type).toString(); } + } diff --git a/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/ServerImage.java b/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/ServerImage.java index d965ea94fd..718a648550 100644 --- a/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/ServerImage.java +++ b/providers/gogrid/src/main/java/org/jclouds/gogrid/domain/ServerImage.java @@ -18,9 +18,13 @@ */ package org.jclouds.gogrid.domain; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Objects.toStringHelper; + import java.util.Date; import java.util.Set; +import com.google.common.base.Objects; import com.google.common.primitives.Longs; import com.google.gson.annotations.SerializedName; @@ -53,10 +57,9 @@ public class ServerImage implements Comparable { public ServerImage() { } - public ServerImage(long id, String name, String friendlyName, String description, Option os, - Option architecture, ServerImageType type, ServerImageState state, double price, - String location, boolean active, boolean aPublic, Date createdTime, Date updatedTime, - Set billingTokens, Customer owner) { + public ServerImage(long id, String name, String friendlyName, String description, Option os, Option architecture, + ServerImageType type, ServerImageState state, double price, String location, boolean active, + boolean aPublic, Date createdTime, Date updatedTime, Set billingTokens, Customer owner) { this.id = id; this.name = name; this.friendlyName = friendlyName; @@ -142,88 +145,38 @@ public class ServerImage implements Comparable { } @Override - public boolean equals(Object o) { - if (this == o) + public boolean equals(Object object) { + if (this == object) { return true; - if (o == null || getClass() != o.getClass()) + } + if (object instanceof ServerImage) { + final ServerImage other = ServerImage.class.cast(object); + return equal(id, other.id) && equal(name, other.name); + } else { return false; - - ServerImage that = (ServerImage) o; - - if (id != that.id) - return false; - if (isActive != that.isActive) - return false; - if (isPublic != that.isPublic) - return false; - if (Double.compare(that.price, price) != 0) - return false; - if (architecture != null ? !architecture.equals(that.architecture) - : that.architecture != null) - return false; - if (billingTokens != null ? !billingTokens.equals(that.billingTokens) - : that.billingTokens != null) - return false; - if (!createdTime.equals(that.createdTime)) - return false; - if (description != null ? !description.equals(that.description) : that.description != null) - return false; - if (friendlyName != null ? !friendlyName.equals(that.friendlyName) - : that.friendlyName != null) - return false; - if (location != null ? !location.equals(that.location) : that.location != null) - return false; - if (!name.equals(that.name)) - return false; - if (!os.equals(that.os)) - return false; - if (owner != null ? !owner.equals(that.owner) : that.owner != null) - return false; - if (!state.equals(that.state)) - return false; - if (!type.equals(that.type)) - return false; - if (updatedTime != null ? !updatedTime.equals(that.updatedTime) : that.updatedTime != null) - return false; - - return true; + } } @Override public int hashCode() { - int result; - long temp; - result = (int) (id ^ (id >>> 32)); - result = 31 * result + name.hashCode(); - result = 31 * result + (friendlyName != null ? friendlyName.hashCode() : 0); - result = 31 * result + (description != null ? description.hashCode() : 0); - result = 31 * result + os.hashCode(); - result = 31 * result + type.hashCode(); - result = 31 * result + state.hashCode(); - temp = price != +0.0d ? Double.doubleToLongBits(price) : 0L; - result = 31 * result + (int) (temp ^ (temp >>> 32)); - result = 31 * result + (location != null ? location.hashCode() : 0); - result = 31 * result + (isActive ? 1 : 0); - result = 31 * result + (isPublic ? 1 : 0); - result = 31 * result + createdTime.hashCode(); - result = 31 * result + (updatedTime != null ? updatedTime.hashCode() : 0); - result = 31 * result + (billingTokens != null ? billingTokens.hashCode() : 0); - result = 31 * result + (owner != null ? owner.hashCode() : 0); - return result; + return Objects.hashCode(id, name); } @Override - public int compareTo(ServerImage o) { - return Longs.compare(id, o.getId()); + public int compareTo(ServerImage that) { + if (that == null) + return 1; + if (this == that) + return 0; + return Longs.compare(id, that.getId()); } @Override public String toString() { - return "ServerImage{" + "id=" + id + ", name='" + name + '\'' + ", friendlyName='" - + friendlyName + '\'' + ", description='" + description + '\'' + ", os=" + os - + ", architecture=" + architecture + ", type=" + type + ", state=" + state - + ", price=" + price + ", location='" + location + '\'' + ", isActive=" + isActive - + ", isPublic=" + isPublic + ", createdTime=" + createdTime + ", updatedTime=" - + updatedTime + ", billingTokens=" + billingTokens + ", owner=" + owner + '}'; + return toStringHelper("").add("id", id).add("name", name).add("friendlyName", friendlyName).add("friendlyName", + friendlyName).add("description", description).add("os", os).add("architecture", architecture).add( + "type", type).add("state", state).add("price", price).add("location", location) + .add("isActive", isActive).add("isPublic", isPublic).add("createdTime", createdTime).add("updatedTime", + updatedTime).add("billingTokens", billingTokens).add("owner", owner).toString(); } }