From 7c202189a23742b6f32d95128e9318972b8c39dc Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Tue, 30 Oct 2018 15:02:15 -0300 Subject: [PATCH] [bramble] Implement equals() and hashCode() for AuthorView --- .../bramble/api/identity/AuthorInfo.java | 14 +++++++ .../bramble/api/identity/AuthorInfoTest.java | 42 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 bramble-api/src/test/java/org/briarproject/bramble/api/identity/AuthorInfoTest.java diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/identity/AuthorInfo.java b/bramble-api/src/main/java/org/briarproject/bramble/api/identity/AuthorInfo.java index a93b414052..8c90d9aff0 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/identity/AuthorInfo.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/identity/AuthorInfo.java @@ -35,4 +35,18 @@ public class AuthorInfo { return alias; } + @Override + public int hashCode() { + int hashCode = status.ordinal(); + if (alias != null) hashCode += alias.hashCode(); + return hashCode; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof AuthorInfo)) return false; + AuthorInfo info = (AuthorInfo) o; + return status == info.status && + (alias == null ? info.alias == null : alias.equals(info.alias)); + } } diff --git a/bramble-api/src/test/java/org/briarproject/bramble/api/identity/AuthorInfoTest.java b/bramble-api/src/test/java/org/briarproject/bramble/api/identity/AuthorInfoTest.java new file mode 100644 index 0000000000..9c059c84ec --- /dev/null +++ b/bramble-api/src/test/java/org/briarproject/bramble/api/identity/AuthorInfoTest.java @@ -0,0 +1,42 @@ +package org.briarproject.bramble.api.identity; + +import org.briarproject.bramble.test.BrambleTestCase; +import org.junit.Test; + +import static org.briarproject.bramble.api.identity.AuthorInfo.Status.NONE; +import static org.briarproject.bramble.api.identity.AuthorInfo.Status.VERIFIED; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class AuthorInfoTest extends BrambleTestCase { + + @Test + public void testEquals() { + assertEquals( + new AuthorInfo(NONE), + new AuthorInfo(NONE, null) + ); + assertEquals( + new AuthorInfo(NONE, "test"), + new AuthorInfo(NONE, "test") + ); + + assertNotEquals( + new AuthorInfo(NONE), + new AuthorInfo(VERIFIED) + ); + assertNotEquals( + new AuthorInfo(NONE, "test"), + new AuthorInfo(NONE) + ); + assertNotEquals( + new AuthorInfo(NONE), + new AuthorInfo(NONE, "test") + ); + assertNotEquals( + new AuthorInfo(NONE, "a"), + new AuthorInfo(NONE, "b") + ); + } + +} -- GitLab