Add combined ID class
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Oct 2015 17:15:44 +0000 (19:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Oct 2015 17:15:44 +0000 (19:15 +0200)
src/main/java/net/pterodactylus/sone/data/Id.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/data/IdTest.java [new file with mode: 0644]

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 (file)
index 0000000..bc875b9
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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 (file)
index 0000000..082f65b
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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));
+       }
+
+}