Store a Sone’s name in its own class, use a modifier to modify a Sone’s name.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 17 Oct 2013 19:37:15 +0000 (21:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:29 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java

index fbcc8b3..20983e3 100644 (file)
@@ -62,6 +62,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.Field;
+import net.pterodactylus.sone.data.Profile.Name;
 import net.pterodactylus.sone.data.Reply.Modifier.ReplyUpdated;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
@@ -952,9 +953,10 @@ public class Core extends AbstractService implements SoneProvider {
 
                /* load profile. */
                Profile profile = new Profile(sone);
-               profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
-               profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
-               profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
+               String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
+               String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
+               String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
+               profile.modify().setFirstName(firstName).setMiddleName(middleName).setLastName(lastName).update();
                profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
                profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
                profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
index 384bbdc..2e8299f 100644 (file)
@@ -31,7 +31,6 @@ import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
-import net.pterodactylus.sone.data.Profile.Name;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.DefaultSone;
 import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
@@ -177,7 +176,7 @@ public class SoneParser {
                Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
                Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
                Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
-               Profile profile = new Profile(sone).modify().setName(new Name(profileFirstName, profileMiddleName, profileLastName)).update();
+               Profile profile = new Profile(sone).modify().setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName).update();
                profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
                /* avatar is processed after images are loaded. */
                String avatarId = profileXml.getValue("avatar", null);
index 5a4fde0..2a1c416 100644 (file)
@@ -17,6 +17,7 @@
 
 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;
@@ -26,6 +27,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
 
+import com.google.common.base.Optional;
 import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
 
@@ -40,14 +42,7 @@ public class Profile implements Fingerprintable {
        /** The Sone this profile belongs to. */
        private final Sone sone;
 
-       /** The first name. */
-       private volatile String firstName;
-
-       /** The middle name(s). */
-       private volatile String middleName;
-
-       /** The last name. */
-       private volatile String lastName;
+       private volatile Name name;
 
        /** The day of the birth date. */
        private volatile Integer birthDay;
@@ -82,9 +77,7 @@ public class Profile implements Fingerprintable {
         */
        public Profile(Profile profile) {
                this.sone = profile.sone;
-               this.firstName = profile.firstName;
-               this.middleName = profile.middleName;
-               this.lastName = profile.lastName;
+               this.name = profile.name;
                this.birthDay = profile.birthDay;
                this.birthMonth = profile.birthMonth;
                this.birthYear = profile.birthYear;
@@ -111,19 +104,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) {
-               this.firstName = firstName;
-               return this;
+               return name.getFirst().orNull();
        }
 
        /**
@@ -132,19 +113,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) {
-               this.middleName = middleName;
-               return this;
+               return name.getMiddle().orNull();
        }
 
        /**
@@ -153,19 +122,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) {
-               this.lastName = lastName;
-               return this;
+               return name.getLast().orNull();
        }
 
        /**
@@ -379,6 +336,47 @@ public class Profile implements Fingerprintable {
                fields.remove(field);
        }
 
+       public Modifier modify() {
+               return new Modifier() {
+                       private Optional<String> firstName = name.getFirst();
+                       private Optional<String> middleName = name.getMiddle();
+                       private Optional<String> lastName = name.getLast();
+
+                       @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 Profile update() {
+                               Profile.this.name = new Name(firstName, middleName, lastName);
+                               return Profile.this;
+                       }
+               };
+       }
+
+       public interface Modifier {
+
+               Modifier setFirstName(String firstName);
+               Modifier setMiddleName(String middleName);
+               Modifier setLastName(String lastName);
+               Profile update();
+
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -406,15 +404,7 @@ public class Profile implements Fingerprintable {
        public String getFingerprint() {
                Hasher hash = Hashing.sha256().newHasher();
                hash.putString("Profile(");
-               if (firstName != null) {
-                       hash.putString("FirstName(").putString(firstName).putString(")");
-               }
-               if (middleName != null) {
-                       hash.putString("MiddleName(").putString(middleName).putString(")");
-               }
-               if (lastName != null) {
-                       hash.putString("LastName(").putString(lastName).putString(")");
-               }
+               hash.putString(name.getFingerprint());
                if (birthDay != null) {
                        hash.putString("BirthDay(").putInt(birthDay).putString(")");
                }
@@ -553,4 +543,47 @@ public class Profile implements Fingerprintable {
 
        }
 
+       public static class Name implements Fingerprintable {
+
+               private final Optional<String> first;
+               private final Optional<String> middle;
+               private final Optional<String> last;
+
+               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();
+               }
+
+       }
+
 }
index 5e5b919..ecab3da 100644 (file)
@@ -75,9 +75,7 @@ public class EditProfilePage extends SoneTemplatePage {
                                birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim());
                                birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim());
                                avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
-                               profile.setFirstName(firstName.length() > 0 ? firstName : null);
-                               profile.setMiddleName(middleName.length() > 0 ? middleName : null);
-                               profile.setLastName(lastName.length() > 0 ? lastName : null);
+                               profile.modify().setFirstName(getNameFromFormField(firstName)).setMiddleName(getNameFromFormField(middleName)).setLastName(getNameFromFormField(lastName)).update();
                                profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
                                profile.setAvatar(webInterface.getCore().getImage(avatarId).orNull());
                                for (Field field : fields) {
@@ -140,6 +138,10 @@ public class EditProfilePage extends SoneTemplatePage {
                templateContext.set("fields", fields);
        }
 
+       private String getNameFromFormField(String name) {
+               return name.length() > 0 ? name : null;
+       }
+
        //
        // PRIVATE METHODS
        //