From: David ‘Bombe’ Roden Date: Fri, 8 Nov 2013 06:56:32 +0000 (+0100) Subject: Add equals() and hashCode() implementation. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=39efbde874d5caa9c12857cd76970b9cbcc17161;p=Sone.git Add equals() and hashCode() implementation. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java index 0a31606..0438200 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java @@ -17,6 +17,10 @@ package net.pterodactylus.sone.freenet.wot; +import static com.google.common.base.Objects.equal; + +import com.google.common.base.Objects; + /** * Container class for trust in the web of trust. * @@ -80,6 +84,20 @@ public class Trust { } @Override + public int hashCode() { + return Objects.hashCode(getExplicit(), getImplicit(), getDistance()); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof Trust)) { + return false; + } + Trust trust = (Trust) object; + return equal(getExplicit(), trust.getExplicit()) && equal(getImplicit(), trust.getImplicit()) && equal(getDistance(), trust.getDistance()); + } + + @Override public String toString() { return getClass().getName() + "[explicit=" + explicit + ",implicit=" + implicit + ",distance=" + distance + "]"; } diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/TrustTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/TrustTest.java index 88ad04f..dfc2543 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/TrustTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/wot/TrustTest.java @@ -46,4 +46,12 @@ public class TrustTest { assertThat(trust.getDistance(), nullValue()); } + @Test + public void equalTrustsHaveTheSameHashCode() { + Trust trust1 = new Trust(5, 17, 2); + Trust trust2 = new Trust(5, 17, 2); + assertThat(trust1, is(trust2)); + assertThat(trust1.hashCode(), is(trust2.hashCode())); + } + }