Add equals() and hashCode() implementation.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 8 Nov 2013 06:56:32 +0000 (07:56 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:59 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/freenet/wot/Trust.java
src/test/java/net/pterodactylus/sone/freenet/wot/TrustTest.java

index 0a31606..0438200 100644 (file)
 
 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 + "]";
        }
index 88ad04f..dfc2543 100644 (file)
@@ -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()));
+       }
+
 }