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;
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);
}
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
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);
}
}