Add method to rename a field.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Profile.java
index fcea737..1d7610e 100644 (file)
@@ -42,7 +42,7 @@ public class Profile implements Fingerprintable {
        /** The Sone this profile belongs to. */
        private final Sone sone;
 
-       private volatile Name name;
+       private volatile Name name = new Name();
        private volatile BirthDate birthDate = new BirthDate();
 
        /** The ID of the avatar image. */
@@ -155,18 +155,12 @@ public class Profile implements Fingerprintable {
        /**
         * Sets the avatar image.
         *
-        * @param avatar
-        *            The new avatar image, or {@code null} to not select an avatar
-        *            image.
-        * @return This Sone
+        * @param avatarId
+        *              The ID of the new avatar image
+        * @return This profile
         */
-       public Profile setAvatar(Image avatar) {
-               if (avatar == null) {
-                       this.avatar = null;
-                       return this;
-               }
-               checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
-               this.avatar = avatar.getId();
+       public Profile setAvatar(Optional<String> avatarId) {
+               this.avatar = avatarId.orNull();
                return this;
        }
 
@@ -244,6 +238,14 @@ public class Profile implements Fingerprintable {
                return field;
        }
 
+       public void renameField(Field field, String newName) {
+               int indexOfField = fields.indexOf(field);
+               if (indexOfField == -1) {
+                       return;
+               }
+               fields.set(indexOfField, new Field(field.getId(), newName, field.getValue()));
+       }
+
        /**
         * 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
@@ -431,6 +433,12 @@ public class Profile implements Fingerprintable {
                        this.id = checkNotNull(id, "id must not be null");
                }
 
+               public Field(String id, String name, String value) {
+                       this.id = checkNotNull(id, "id must not be null");
+                       this.name = name;
+                       this.value = value;
+               }
+
                /**
                 * Returns the ID of this field.
                 *
@@ -520,6 +528,10 @@ public class Profile implements Fingerprintable {
                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;