X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FProfile.java;h=679196b14342f4df5338cf64f373e13f6483e7b8;hb=50999afc14ced32f8f15b7512a896bbac1e61562;hp=57b54c6858bce2e95ad71ec407107af9b7aeae3b;hpb=99888ce13cc17d49f5e217ab6f2c9ad5ef168792;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java
index 57b54c6..679196b 100644
--- a/src/main/java/net/pterodactylus/sone/data/Profile.java
+++ b/src/main/java/net/pterodactylus/sone/data/Profile.java
@@ -26,6 +26,9 @@ import java.util.Collections;
import java.util.List;
import java.util.UUID;
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
+
/**
* A profile stores personal information about a {@link Sone}. All information
* is optional and can be {@code null}.
@@ -240,7 +243,7 @@ public class Profile implements Fingerprintable {
return this;
}
checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
- this.avatar = avatar.getId();
+ this.avatar = avatar.getInternalId();
return this;
}
@@ -322,10 +325,14 @@ public class Profile implements Fingerprintable {
*/
public Field addField(String fieldName) throws IllegalArgumentException {
checkNotNull(fieldName, "fieldName must not be null");
- checkArgument(fieldName.length() > 0, "fieldName must not be empty");
- checkState(getFieldByName(fieldName) == null, "fieldName must be unique");
+ if (fieldName.length() == 0) {
+ throw new EmptyFieldName();
+ }
+ if (getFieldByName(fieldName) != null) {
+ throw new DuplicateField();
+ }
@SuppressWarnings("synthetic-access")
- Field field = new Field().setName(fieldName);
+ Field field = new Field().setName(fieldName).setValue("");
fields.add(field);
return field;
}
@@ -401,37 +408,37 @@ public class Profile implements Fingerprintable {
*/
@Override
public String getFingerprint() {
- StringBuilder fingerprint = new StringBuilder();
- fingerprint.append("Profile(");
+ Hasher hash = Hashing.sha256().newHasher();
+ hash.putString("Profile(");
if (firstName != null) {
- fingerprint.append("FirstName(").append(firstName).append(')');
+ hash.putString("FirstName(").putString(firstName).putString(")");
}
if (middleName != null) {
- fingerprint.append("MiddleName(").append(middleName).append(')');
+ hash.putString("MiddleName(").putString(middleName).putString(")");
}
if (lastName != null) {
- fingerprint.append("LastName(").append(lastName).append(')');
+ hash.putString("LastName(").putString(lastName).putString(")");
}
if (birthDay != null) {
- fingerprint.append("BirthDay(").append(birthDay).append(')');
+ hash.putString("BirthDay(").putInt(birthDay).putString(")");
}
if (birthMonth != null) {
- fingerprint.append("BirthMonth(").append(birthMonth).append(')');
+ hash.putString("BirthMonth(").putInt(birthMonth).putString(")");
}
if (birthYear != null) {
- fingerprint.append("BirthYear(").append(birthYear).append(')');
+ hash.putString("BirthYear(").putInt(birthYear).putString(")");
}
if (avatar != null) {
- fingerprint.append("Avatar(").append(avatar).append(')');
+ hash.putString("Avatar(").putString(avatar).putString(")");
}
- fingerprint.append("ContactInformation(");
+ hash.putString("ContactInformation(");
for (Field field : fields) {
- fingerprint.append(field.getName()).append('(').append(field.getValue()).append(')');
+ hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
}
- fingerprint.append(")");
- fingerprint.append(")");
+ hash.putString(")");
+ hash.putString(")");
- return fingerprint.toString();
+ return hash.hash().toString();
}
/**
@@ -550,4 +557,18 @@ public class Profile implements Fingerprintable {
}
+ /**
+ * Exception that signals the addition of a field with an empty name.
+ *
+ * @author David âBombeâ Roden
+ */
+ public static class EmptyFieldName extends IllegalArgumentException { }
+
+ /**
+ * Exception that signals the addition of a field that already exists.
+ *
+ * @author David âBombeâ Roden
+ */
+ public static class DuplicateField extends IllegalArgumentException { }
+
}