Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save korotkevics/eb7571df642da911af31a7707773e7d1 to your computer and use it in GitHub Desktop.
Save korotkevics/eb7571df642da911af31a7707773e7d1 to your computer and use it in GitHub Desktop.
Example of JDK7+ Objects equals, hash and Guava 18+ toString implementations
import com.google.common.base.MoreObjects;
import java.util.Objects;
public class Address {
...
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final Address other = (Address) obj;
return Objects.equals(this.houseNumber, other.houseNumber)
&& Objects.equals(this.street, other.street)
&& Objects.equals(this.city, other.city)
&& Objects.equals(this.stateOrProvince, other.stateOrProvince)
&& Objects.equals(this.country, other.country);
}
@Override
public int hashCode() {
return Objects.hash(
this.houseNumber, this.street, this.city, this.stateOrProvince, this.country);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("houseNumber", houseNumber)
.add("street", street)
.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment