Make Sone and element ID optional
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Oct 2015 18:51:32 +0000 (20:51 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Oct 2015 18:51:32 +0000 (20:51 +0200)
src/main/java/net/pterodactylus/sone/data/Id.java
src/test/java/net/pterodactylus/sone/data/IdTest.java

index 75f9bc6..2283447 100644 (file)
@@ -1,7 +1,10 @@
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import com.google.common.base.Charsets;
 import com.google.common.base.Objects;
+import com.google.common.base.Optional;
 import com.google.common.hash.HashFunction;
 import com.google.common.hash.Hashing;
 
@@ -16,27 +19,45 @@ public class Id {
 
        private final String soneId;
        private final String elementId;
+       private final String externalId;
+
+       private Id(String externalId) {
+               this(null, null, checkNotNull(externalId));
+       }
 
        private Id(String soneId, String elementId) {
+               this(checkNotNull(soneId), checkNotNull(elementId), calculateExternalId(soneId, elementId));
+       }
+
+       private Id(String soneId, String elementId, String externalId) {
                this.soneId = soneId;
                this.elementId = elementId;
+               this.externalId = externalId;
        }
 
-       public String getSoneId() {
-               return soneId;
+       public Optional<String> getSoneId() {
+               return Optional.fromNullable(soneId);
        }
 
-       public String getElementId() {
-               return elementId;
+       public Optional<String> getElementId() {
+               return Optional.fromNullable(elementId);
        }
 
        public String getExternal() {
+               return externalId;
+       }
+
+       private static String calculateExternalId(String soneId, String elementId) {
                return sha256.newHasher()
                                .putBytes(soneId.getBytes(Charsets.UTF_8))
                                .putBytes(elementId.getBytes(Charsets.UTF_8))
                                .hash().toString();
        }
 
+       public static Id from(String externalId) {
+               return new Id(externalId);
+       }
+
        public static Id from(String soneId, String elementId) {
                return new Id(soneId, elementId);
        }
@@ -47,13 +68,12 @@ public class Id {
                        return false;
                }
                Id id = (Id) o;
-               return Objects.equal(soneId, id.soneId)
-                               && Objects.equal(elementId, id.elementId);
+               return Objects.equal(externalId, id.externalId);
        }
 
        @Override
        public int hashCode() {
-               return Objects.hashCode(soneId, elementId);
+               return Objects.hashCode(externalId);
        }
 
        @Override
index 082f65b..46964d0 100644 (file)
@@ -17,55 +17,79 @@ 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);
+
+       private final Id completeId = Id.from(SONE_ID, ELEMENT_ID);
+       private final Id parsedId = Id.from(EXPECTED_ID);
 
        @Test
        public void idBuilderBuildsCorrectIds() {
-               assertThat(id.getSoneId(), is(SONE_ID));
-               assertThat(id.getElementId(), is(ELEMENT_ID));
-               assertThat(id.getExternal(), is(EXPECTED_ID));
+               assertThat(completeId.getSoneId().get(), is(SONE_ID));
+               assertThat(completeId.getElementId().get(), is(ELEMENT_ID));
+               assertThat(completeId.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)));
+       public void parsedIdIsBuiltCorrectly() {
+               assertThat(parsedId.getSoneId().isPresent(), is(false));
+               assertThat(parsedId.getElementId().isPresent(), is(false));
+               assertThat(parsedId.getExternal(), is(EXPECTED_ID));
        }
 
        @Test
-       public void twoIdsAreNotEqualIfElementIdIsDifferent() {
-               Id id2 = Id.from(SONE_ID, ELEMENT_ID + "1");
-               assertThat(id2, not(equalTo(id)));
-               assertThat(id, not(equalTo(id2)));
+       public void twoIdsAreNotEqualIfExternalIdIsDifferent() {
+               Id id2 = Id.from(EXPECTED_ID + "1");
+               assertThat(id2, not(equalTo(parsedId)));
+               assertThat(parsedId, not(equalTo(id2)));
        }
 
        @Test
        public void twoIdsAreEqualIfSoneAndElementIdIsEqual() {
                Id id2 = Id.from(SONE_ID, ELEMENT_ID);
-               assertThat(id2, equalTo(id));
-               assertThat(id, equalTo(id2));
+               assertThat(id2, equalTo(completeId));
+               assertThat(completeId, equalTo(id2));
+       }
+
+       @Test
+       public void twoIdsAreEqualIfExternalIdIsEqual() {
+               Id id2 = Id.from(EXPECTED_ID);
+               assertThat(id2, equalTo(parsedId));
+               assertThat(parsedId, equalTo(id2));
        }
 
        @Test
        public void twoIdsHaveTheSameHashCodeIfTheyAreEqual() {
-               Id id2 = Id.from(SONE_ID, ELEMENT_ID);
-               assertThat(id2.hashCode(), equalTo(id.hashCode()));
+               Id id2 = Id.from(EXPECTED_ID);
+               assertThat(id2.hashCode(), equalTo(completeId.hashCode()));
        }
 
        @Test
        public void nullIsNotEqualToId() {
-               assertThat(id, not(equalTo(null)));
+               assertThat(completeId, not(equalTo(null)));
        }
 
        @Test
        public void objectOfDifferentClassIsNotEqualToId() {
-               assertThat(id, not(equalTo(new Object())));
+               assertThat(completeId, not(equalTo(new Object())));
        }
 
        @Test
        public void stringRepresentationMatchesTheExternalId() {
-               assertThat(id.toString(), is(EXPECTED_ID));
+               assertThat(completeId.toString(), is(EXPECTED_ID));
+       }
+
+       @Test(expected = NullPointerException.class)
+       public void canNotParseNullAsExternalId() {
+               Id.from(null);
+       }
+
+       @Test(expected = NullPointerException.class)
+       public void canNotParseNullAsSoneId() {
+               Id.from(null, ELEMENT_ID);
+       }
+
+       @Test(expected = NullPointerException.class)
+       public void canNotParseNullAsElementId() {
+               Id.from(SONE_ID, null);
        }
 
 }