From 39efbde874d5caa9c12857cd76970b9cbcc17161 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 8 Nov 2013 07:56:32 +0100 Subject: [PATCH] Add equals() and hashCode() implementation. --- .../java/net/pterodactylus/sone/freenet/wot/Trust.java | 18 ++++++++++++++++++ .../net/pterodactylus/sone/freenet/wot/TrustTest.java | 8 ++++++++ 2 files changed, 26 insertions(+) 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())); + } + } -- 2.7.4