Make profile fields immutable.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Profile.java
index 059c33e..3e446fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * FreenetSone - Profile.java - Copyright © 2010 David Roden
+ * Sone - Profile.java - Copyright © 2010–2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.UUID.randomUUID;
+
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
-import net.pterodactylus.util.validation.Validation;
+import com.google.common.base.Optional;
+import com.google.common.hash.Hasher;
+import com.google.common.hash.Hashing;
 
 /**
  * A profile stores personal information about a {@link Sone}. All information
@@ -33,38 +39,26 @@ import net.pterodactylus.util.validation.Validation;
  */
 public class Profile implements Fingerprintable {
 
-       /** Whether the profile was modified. */
-       private volatile boolean modified;
-
-       /** The first name. */
-       private volatile String firstName;
-
-       /** The middle name(s). */
-       private volatile String middleName;
-
-       /** The last name. */
-       private volatile String lastName;
+       /** The Sone this profile belongs to. */
+       private final Sone sone;
 
-       /** The day of the birth date. */
-       private volatile Integer birthDay;
+       private volatile Name name = new Name();
+       private volatile BirthDate birthDate = new BirthDate();
 
-       /** The month of the birth date. */
-       private volatile Integer birthMonth;
-
-       /** The year of the birth date. */
-       private volatile Integer birthYear;
+       /** The ID of the avatar image. */
+       private volatile String avatar;
 
        /** Additional fields in the profile. */
-       private final List<String> fields = Collections.synchronizedList(new ArrayList<String>());
-
-       /** The field values. */
-       private final Map<String, String> fieldValues = Collections.synchronizedMap(new HashMap<String, String>());
+       private final List<Field> fields = Collections.synchronizedList(new ArrayList<Field>());
 
        /**
         * Creates a new empty profile.
+        *
+        * @param sone
+        *            The Sone this profile belongs to
         */
-       public Profile() {
-               /* do nothing. */
+       public Profile(Sone sone) {
+               this.sone = sone;
        }
 
        /**
@@ -74,16 +68,11 @@ public class Profile implements Fingerprintable {
         *            The profile to copy
         */
        public Profile(Profile profile) {
-               if (profile == null) {
-                       return;
-               }
-               this.firstName = profile.firstName;
-               this.middleName = profile.middleName;
-               this.lastName = profile.lastName;
-               this.birthDay = profile.birthDay;
-               this.birthMonth = profile.birthMonth;
-               this.birthYear = profile.birthYear;
-               this.fieldValues.putAll(profile.fieldValues);
+               this.sone = profile.sone;
+               this.name = profile.name;
+               this.birthDate = profile.birthDate;
+               this.avatar = profile.avatar;
+               this.fields.addAll(profile.fields);
        }
 
        //
@@ -91,15 +80,12 @@ public class Profile implements Fingerprintable {
        //
 
        /**
-        * Returns whether this profile was modified after creation. To clear the
-        * “is modified” flag you need to create a new profile from this one using
-        * the {@link #Profile(Profile)} constructor.
+        * Returns the Sone this profile belongs to.
         *
-        * @return {@code true} if this profile was modified after creation,
-        *         {@code false} otherwise
+        * @return The Sone this profile belongs to
         */
-       public boolean isModified() {
-               return modified;
+       public Sone getSone() {
+               return sone;
        }
 
        /**
@@ -108,20 +94,7 @@ public class Profile implements Fingerprintable {
         * @return The first name
         */
        public String getFirstName() {
-               return firstName;
-       }
-
-       /**
-        * Sets the first name.
-        *
-        * @param firstName
-        *            The first name to set
-        * @return This profile (for method chaining)
-        */
-       public Profile setFirstName(String firstName) {
-               modified |= ((firstName != null) && (!firstName.equals(this.firstName))) || (this.firstName != null);
-               this.firstName = firstName;
-               return this;
+               return name.getFirst().orNull();
        }
 
        /**
@@ -130,20 +103,7 @@ public class Profile implements Fingerprintable {
         * @return The middle name
         */
        public String getMiddleName() {
-               return middleName;
-       }
-
-       /**
-        * Sets the middle name.
-        *
-        * @param middleName
-        *            The middle name to set
-        * @return This profile (for method chaining)
-        */
-       public Profile setMiddleName(String middleName) {
-               modified |= ((middleName != null) && (!middleName.equals(this.middleName))) || (this.middleName != null);
-               this.middleName = middleName;
-               return this;
+               return name.getMiddle().orNull();
        }
 
        /**
@@ -152,20 +112,7 @@ public class Profile implements Fingerprintable {
         * @return The last name
         */
        public String getLastName() {
-               return lastName;
-       }
-
-       /**
-        * Sets the last name.
-        *
-        * @param lastName
-        *            The last name to set
-        * @return This profile (for method chaining)
-        */
-       public Profile setLastName(String lastName) {
-               modified |= ((lastName != null) && (!lastName.equals(this.lastName))) || (this.lastName != null);
-               this.lastName = lastName;
-               return this;
+               return name.getLast().orNull();
        }
 
        /**
@@ -174,20 +121,7 @@ public class Profile implements Fingerprintable {
         * @return The day of the birth date (from 1 to 31)
         */
        public Integer getBirthDay() {
-               return birthDay;
-       }
-
-       /**
-        * Sets the day of the birth date.
-        *
-        * @param birthDay
-        *            The day of the birth date (from 1 to 31)
-        * @return This profile (for method chaining)
-        */
-       public Profile setBirthDay(Integer birthDay) {
-               modified |= ((birthDay != null) && (!birthDay.equals(this.birthDay))) || (this.birthDay != null);
-               this.birthDay = birthDay;
-               return this;
+               return birthDate.getDay().orNull();
        }
 
        /**
@@ -196,20 +130,7 @@ public class Profile implements Fingerprintable {
         * @return The month of the birth date (from 1 to 12)
         */
        public Integer getBirthMonth() {
-               return birthMonth;
-       }
-
-       /**
-        * Sets the month of the birth date.
-        *
-        * @param birthMonth
-        *            The month of the birth date (from 1 to 12)
-        * @return This profile (for method chaining)
-        */
-       public Profile setBirthMonth(Integer birthMonth) {
-               modified |= ((birthMonth != null) && (!birthMonth.equals(this.birthMonth))) || (this.birthMonth != null);
-               this.birthMonth = birthMonth;
-               return this;
+               return birthDate.getMonth().orNull();
        }
 
        /**
@@ -218,143 +139,231 @@ public class Profile implements Fingerprintable {
         * @return The year of the birth date
         */
        public Integer getBirthYear() {
-               return birthYear;
+               return birthDate.getYear().orNull();
        }
 
        /**
-        * Sets the year of the birth date.
+        * Returns the ID of the currently selected avatar image.
         *
-        * @param birthYear
-        *            The year of the birth date
-        * @return This profile (for method chaining)
+        * @return The ID of the currently selected avatar image, or {@code null} if
+        *         no avatar is selected.
         */
-       public Profile setBirthYear(Integer birthYear) {
-               modified |= ((birthYear != null) && (!birthYear.equals(this.birthYear))) || (this.birthYear != null);
-               this.birthYear = birthYear;
-               return this;
+       public String getAvatar() {
+               return avatar;
        }
 
        /**
-        * Appends a new field to the list of fields.
+        * Sets the avatar image.
         *
-        * @param field
-        *            The field to add
-        * @throws IllegalArgumentException
-        *             if the name is not valid
+        * @param avatarId
+        *              The ID of the new avatar image
+        * @return This profile
         */
-       public void addField(String field) throws IllegalArgumentException {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Unique", !fields.contains(field), true).check();
-               fields.add(field);
+       public Profile setAvatar(Optional<String> avatarId) {
+               this.avatar = avatarId.orNull();
+               return this;
        }
 
        /**
-        * Moves the field with the given index up one position in the field list.
-        * The index of the field to move must be greater than {@code 0} (because
-        * you obviously can not move the first field further up).
+        * Returns the fields of this profile.
         *
-        * @param fieldIndex
-        *            The index of the field to move
+        * @return The fields of this profile
         */
-       public void moveFieldUp(int fieldIndex) {
-               Validation.begin().isGreater("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
-               String field = fields.remove(fieldIndex);
-               fields.add(fieldIndex - 1, field);
+       public List<Field> getFields() {
+               return new ArrayList<Field>(fields);
        }
 
        /**
-        * Moves the field with the given name up one position in the field list.
-        * The field must not be the first field (because you obviously can not move
-        * the first field further up).
+        * Returns whether this profile contains the given field.
         *
         * @param field
-        *            The name of the field to move
+        *            The field to check for
+        * @return {@code true} if this profile contains the field, false otherwise
         */
-       public void moveFieldUp(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               moveFieldUp(getFieldIndex(field));
+       public boolean hasField(Field field) {
+               return fields.contains(field);
        }
 
        /**
-        * Moves the field with the given index down one position in the field list.
-        * The index of the field to move must be less than the index of the last
-        * field (because you obviously can not move the last field further down).
+        * Returns the field with the given ID.
         *
-        * @param fieldIndex
-        *            The index of the field to move
+        * @param fieldId
+        *            The ID of the field to get
+        * @return The field, or {@code null} if this profile does not contain a
+        *         field with the given ID
         */
-       public void moveFieldDown(int fieldIndex) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size() - 1).check();
-               String field = fields.remove(fieldIndex);
-               fields.add(fieldIndex + 1, field);
+       public Field getFieldById(String fieldId) {
+               checkNotNull(fieldId, "fieldId must not be null");
+               for (Field field : fields) {
+                       if (field.getId().equals(fieldId)) {
+                               return field;
+                       }
+               }
+               return null;
        }
 
        /**
-        * Moves the field with the given name down one position in the field list.
-        * The field must not be the last field (because you obviously can not move
-        * the last field further down).
+        * Returns the field with the given name.
         *
-        * @param field
-        *            The name of the field to move
+        * @param fieldName
+        *            The name of the field to get
+        * @return The field, or {@code null} if this profile does not contain a
+        *         field with the given name
         */
-       public void moveFieldDown(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               moveFieldDown(getFieldIndex(field));
+       public Field getFieldByName(String fieldName) {
+               for (Field field : fields) {
+                       if (field.getName().equals(fieldName)) {
+                               return field;
+                       }
+               }
+               return null;
        }
 
        /**
-        * Removes the field at the given index.
+        * Appends a new field to the list of fields.
         *
-        * @param fieldIndex
-        *            The index of the field to remove
+        * @param fieldName
+        *            The name of the new field
+        * @return The new field
+        * @throws IllegalArgumentException
+        *             if the name is not valid
         */
-       public void removeField(int fieldIndex) {
-               Validation.begin().isGreaterOrEqual("Field Index", fieldIndex, 0).isLess("Field Index", fieldIndex, fields.size()).check();
-               String field = fields.remove(fieldIndex);
-               fieldValues.remove(field);
+       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");
+               @SuppressWarnings("synthetic-access")
+               Field field = new Field(fieldName);
+               fields.add(field);
+               return field;
+       }
+
+       public void renameField(Field field, String newName) {
+               int indexOfField = getFieldIndex(field);
+               if (indexOfField == -1) {
+                       return;
+               }
+               fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
+       }
+
+       public void setField(Field field, String newValue) {
+               int indexOfField = getFieldIndex(field);
+               if (indexOfField == -1) {
+                       return;
+               }
+               fields.set(indexOfField, new Field(field.getId(), field.getName(), newValue));
        }
 
        /**
-        * Removes the field with the given name.
+        * Moves the given field up one position in the field list. The index of the
+        * field to move must be greater than {@code 0} (because you obviously can
+        * not move the first field further up).
         *
         * @param field
-        *            The name of the field
+        *            The field to move up
         */
-       public void removeField(String field) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               removeField(getFieldIndex(field));
+       public void moveFieldUp(Field field) {
+               checkNotNull(field, "field must not be null");
+               checkArgument(hasField(field), "field must belong to this profile");
+               checkArgument(getFieldIndex(field) > 0, "field index must be > 0");
+               int fieldIndex = getFieldIndex(field);
+               fields.remove(field);
+               fields.add(fieldIndex - 1, field);
        }
 
        /**
-        * Returns the value of the field with the given name.
+        * Moves the given field down one position in the field list. The index of
+        * the field to move must be less than the index of the last field (because
+        * you obviously can not move the last field further down).
         *
         * @param field
-        *            The name of the field
-        * @return The value of the field, or {@code null} if there is no such field
+        *            The field to move down
         */
-       public String getField(String field) {
-               return fieldValues.get(field);
+       public void moveFieldDown(Field field) {
+               checkNotNull(field, "field must not be null");
+               checkArgument(hasField(field), "field must belong to this profile");
+               checkArgument(getFieldIndex(field) < fields.size() - 1, "field index must be < " + (fields.size() - 1));
+               int fieldIndex = getFieldIndex(field);
+               fields.remove(field);
+               fields.add(fieldIndex + 1, field);
        }
 
        /**
-        * Sets the value of the field with the given name.
+        * Removes the given field.
         *
         * @param field
-        *            The name of the field
-        * @param value
-        *            The value of the field
+        *            The field to remove
         */
-       public void setField(String field, String value) {
-               Validation.begin().isNotNull("Field Name", field).check().isGreater("Field Name Length", field.length(), 0).isEqual("Field Name Existing", fields.contains(field), true).check();
-               fieldValues.put(field, value);
+       public void removeField(Field field) {
+               checkNotNull(field, "field must not be null");
+               checkArgument(hasField(field), "field must belong to this profile");
+               fields.remove(field);
        }
 
-       /**
-        * Returns a list of all fields stored in this profile.
-        *
-        * @return The fields of this profile
-        */
-       public List<String> getFields() {
-               return Collections.unmodifiableList(fields);
+       public Modifier modify() {
+               return new Modifier() {
+                       private Optional<String> firstName = name.getFirst();
+                       private Optional<String> middleName = name.getMiddle();
+                       private Optional<String> lastName = name.getLast();
+                       private Optional<Integer> birthYear = birthDate.getYear();
+                       private Optional<Integer> birthMonth = birthDate.getMonth();
+                       private Optional<Integer> birthDay = birthDate.getDay();
+
+                       @Override
+                       public Modifier setFirstName(String firstName) {
+                               this.firstName = fromNullable(firstName);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setMiddleName(String middleName) {
+                               this.middleName = fromNullable(middleName);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setLastName(String lastName) {
+                               this.lastName = fromNullable(lastName);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setBirthYear(Integer birthYear) {
+                               this.birthYear = fromNullable(birthYear);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setBirthMonth(Integer birthMonth) {
+                               this.birthMonth = fromNullable(birthMonth);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setBirthDay(Integer birthDay) {
+                               this.birthDay = fromNullable(birthDay);
+                               return this;
+                       }
+
+                       @Override
+                       public Profile update() {
+                               Profile.this.name = new Name(firstName, middleName, lastName);
+                               Profile.this.birthDate = new BirthDate(birthYear, birthMonth, birthDay);
+                               return Profile.this;
+                       }
+               };
+       }
+
+       public interface Modifier {
+
+               Modifier setFirstName(String firstName);
+               Modifier setMiddleName(String middleName);
+               Modifier setLastName(String lastName);
+               Modifier setBirthYear(Integer birthYear);
+               Modifier setBirthMonth(Integer birthMonth);
+               Modifier setBirthDay(Integer birthDay);
+               Profile update();
+
        }
 
        //
@@ -369,7 +378,7 @@ public class Profile implements Fingerprintable {
         * @return The index of the field, or {@code -1} if there is no field with
         *         the given name
         */
-       private int getFieldIndex(String field) {
+       private int getFieldIndex(Field field) {
                return fields.indexOf(field);
        }
 
@@ -382,34 +391,168 @@ public class Profile implements Fingerprintable {
         */
        @Override
        public String getFingerprint() {
-               StringBuilder fingerprint = new StringBuilder();
-               fingerprint.append("Profile(");
-               if (firstName != null) {
-                       fingerprint.append("FirstName(").append(firstName).append(')');
+               Hasher hash = Hashing.sha256().newHasher();
+               hash.putString("Profile(");
+               hash.putString(name.getFingerprint());
+               hash.putString(birthDate.getFingerprint());
+               if (avatar != null) {
+                       hash.putString("Avatar(").putString(avatar).putString(")");
+               }
+               hash.putString("ContactInformation(");
+               for (Field field : fields) {
+                       hash.putString(field.getName()).putString("(").putString(field.getValue()).putString(")");
+               }
+               hash.putString(")");
+               hash.putString(")");
+
+               return hash.hash().toString();
+       }
+
+       /**
+        * Container for a profile field.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public static class Field {
+
+               private final String id;
+               private final String name;
+               private final String value;
+
+               public Field(String name) {
+                       this(name, null);
+               }
+
+               public Field(String name, String value) {
+                       this(randomUUID().toString(), name, value);
+               }
+
+               public Field(String id, String name, String value) {
+                       this.id = checkNotNull(id, "id must not be null");
+                       this.name = name;
+                       this.value = value;
                }
-               if (middleName != null) {
-                       fingerprint.append("MiddleName(").append(middleName).append(')');
+
+               public String getId() {
+                       return id;
                }
-               if (lastName != null) {
-                       fingerprint.append("LastName(").append(lastName).append(')');
+
+               public String getName() {
+                       return name;
                }
-               if (birthDay != null) {
-                       fingerprint.append("BirthDay(").append(birthDay).append(')');
+
+               public String getValue() {
+                       return value;
                }
-               if (birthMonth != null) {
-                       fingerprint.append("BirthMonth(").append(birthMonth).append(')');
+
+               @Override
+               public boolean equals(Object object) {
+                       if (!(object instanceof Field)) {
+                               return false;
+                       }
+                       Field field = (Field) object;
+                       return id.equals(field.id);
                }
-               if (birthYear != null) {
-                       fingerprint.append("BirthYear(").append(birthYear).append(')');
+
+               @Override
+               public int hashCode() {
+                       return id.hashCode();
                }
-               fingerprint.append("ContactInformation(");
-               for (String field : fields) {
-                       fingerprint.append(field).append('(').append(fieldValues.get(field)).append(')');
+
+       }
+
+       public static class Name implements Fingerprintable {
+
+               private final Optional<String> first;
+               private final Optional<String> middle;
+               private final Optional<String> last;
+
+               public Name() {
+                       this(Optional.<String>absent(), Optional.<String>absent(), Optional.<String>absent());
+               }
+
+               public Name(Optional<String> first, Optional<String> middle, Optional<String> last) {
+                       this.first = first;
+                       this.middle = middle;
+                       this.last = last;
+               }
+
+               public Optional<String> getFirst() {
+                       return first;
+               }
+
+               public Optional<String> getMiddle() {
+                       return middle;
+               }
+
+               public Optional<String> getLast() {
+                       return last;
+               }
+
+               @Override
+               public String getFingerprint() {
+                       Hasher hash = Hashing.sha256().newHasher();
+                       hash.putString("Name(");
+                       if (first.isPresent()) {
+                               hash.putString("First(").putString(first.get()).putString(")");
+                       }
+                       if (middle.isPresent()) {
+                               hash.putString("Middle(").putString(middle.get()).putString(")");
+                       }
+                       if (last.isPresent()) {
+                               hash.putString("Last(").putString(last.get()).putString(")");
+                       }
+                       hash.putString(")");
+                       return hash.hash().toString();
+               }
+
+       }
+
+       public static class BirthDate implements Fingerprintable {
+
+               private final Optional<Integer> year;
+               private final Optional<Integer> month;
+               private final Optional<Integer> day;
+
+               public BirthDate() {
+                       this(Optional.<Integer>absent(), Optional.<Integer>absent(), Optional.<Integer>absent());
+               }
+
+               public BirthDate(Optional<Integer> year, Optional<Integer> month, Optional<Integer> day) {
+                       this.year = year;
+                       this.month = month;
+                       this.day = day;
+               }
+
+               public Optional<Integer> getYear() {
+                       return year;
+               }
+
+               public Optional<Integer> getMonth() {
+                       return month;
+               }
+
+               public Optional<Integer> getDay() {
+                       return day;
+               }
+
+               @Override
+               public String getFingerprint() {
+                       Hasher hash = Hashing.sha256().newHasher();
+                       hash.putString("Birthdate(");
+                       if (year.isPresent()) {
+                               hash.putString("Year(").putInt(year.get()).putString(")");
+                       }
+                       if (month.isPresent()) {
+                               hash.putString("Month(").putInt(month.get()).putString(")");
+                       }
+                       if (day.isPresent()) {
+                               hash.putString("Day(").putInt(day.get()).putString(")");
+                       }
+                       hash.putString(")");
+                       return hash.hash().toString();
                }
-               fingerprint.append(")");
-               fingerprint.append(")");
 
-               return fingerprint.toString();
        }
 
 }