From 25eccb52bf670342982c1f87c85b191cf4ae56cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 7 Oct 2015 19:15:44 +0200 Subject: [PATCH] Add combined ID class --- src/main/java/net/pterodactylus/sone/data/Id.java | 66 ++++++++++++++++++++ .../java/net/pterodactylus/sone/data/IdTest.java | 71 ++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/data/Id.java create mode 100644 src/test/java/net/pterodactylus/sone/data/IdTest.java diff --git a/src/main/java/net/pterodactylus/sone/data/Id.java b/src/main/java/net/pterodactylus/sone/data/Id.java new file mode 100644 index 0000000..bc875b9 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/Id.java @@ -0,0 +1,66 @@ +package net.pterodactylus.sone.data; + +import com.google.common.base.Charsets; +import com.google.common.base.Objects; +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; + +/** + * Container for a new, combined ID, originally computed from a {@link Sone}’s ID and an element’s ID. + * + * @author David ‘Bombe’ Roden + */ +public class Id { + + private static final HashFunction HASH_FUNCTION = Hashing.sha256(); + public static final int LENGTH = HASH_FUNCTION.bits() / 4; + + private final HashFunction sha256 = HASH_FUNCTION; + private final String soneId; + private final String elementId; + + private Id(String soneId, String elementId) { + this.soneId = soneId; + this.elementId = elementId; + } + + public String getSoneId() { + return soneId; + } + + public String getElementId() { + return elementId; + } + + public String getExternal() { + return sha256.newHasher() + .putBytes(soneId.getBytes(Charsets.UTF_8)) + .putBytes(elementId.getBytes(Charsets.UTF_8)) + .hash().toString(); + } + + public static Id from(String soneId, String elementId) { + return new Id(soneId, elementId); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) { + return false; + } + Id id = (Id) o; + return Objects.equal(soneId, id.soneId) + && Objects.equal(elementId, id.elementId); + } + + @Override + public int hashCode() { + return Objects.hashCode(soneId, elementId); + } + + @Override + public String toString() { + return getExternal(); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/data/IdTest.java b/src/test/java/net/pterodactylus/sone/data/IdTest.java new file mode 100644 index 0000000..082f65b --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/data/IdTest.java @@ -0,0 +1,71 @@ +package net.pterodactylus.sone.data; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; + +import org.junit.Test; + +/** + * Unit test for {@link Id}. + * + * @author David ‘Bombe’ Roden + */ +public class IdTest { + + private static final String SONE_ID = "~Yp72VX0c6FLDvgIzip5wIvaGIIrjKcKvnX~pTaMKXs"; + private static final String ELEMENT_ID = "88CC70AE-E853-4EEE-B245-E4C55F40DDDF"; + private static final String EXPECTED_ID = "139a629a13f6a2c4191fb19ecead7e57335ea3deb2a971b88d5e004378c4daad"; + private final Id id = Id.from(SONE_ID, ELEMENT_ID); + + @Test + public void idBuilderBuildsCorrectIds() { + assertThat(id.getSoneId(), is(SONE_ID)); + assertThat(id.getElementId(), is(ELEMENT_ID)); + assertThat(id.getExternal(), is(EXPECTED_ID)); + } + + @Test + public void twoIdsAreNotEqualIfSoneIdIsDifferent() { + Id id2 = Id.from(SONE_ID + "1", ELEMENT_ID); + assertThat(id2, not(equalTo(id))); + assertThat(id, not(equalTo(id2))); + } + + @Test + public void twoIdsAreNotEqualIfElementIdIsDifferent() { + Id id2 = Id.from(SONE_ID, ELEMENT_ID + "1"); + assertThat(id2, not(equalTo(id))); + assertThat(id, not(equalTo(id2))); + } + + @Test + public void twoIdsAreEqualIfSoneAndElementIdIsEqual() { + Id id2 = Id.from(SONE_ID, ELEMENT_ID); + assertThat(id2, equalTo(id)); + assertThat(id, equalTo(id2)); + } + + @Test + public void twoIdsHaveTheSameHashCodeIfTheyAreEqual() { + Id id2 = Id.from(SONE_ID, ELEMENT_ID); + assertThat(id2.hashCode(), equalTo(id.hashCode())); + } + + @Test + public void nullIsNotEqualToId() { + assertThat(id, not(equalTo(null))); + } + + @Test + public void objectOfDifferentClassIsNotEqualToId() { + assertThat(id, not(equalTo(new Object()))); + } + + @Test + public void stringRepresentationMatchesTheExternalId() { + assertThat(id.toString(), is(EXPECTED_ID)); + } + +} -- 2.7.4