Add custom exception for adding duplicate fields
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 15 Mar 2015 10:28:34 +0000 (11:28 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 15 Mar 2015 10:28:34 +0000 (11:28 +0100)
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 5851b18..e234965 100644 (file)
@@ -20,6 +20,7 @@ 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.DuplicateField;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.PostBuilder;
 import net.pterodactylus.sone.database.PostReplyBuilder;
@@ -150,8 +151,8 @@ public class SoneParser {
                                }
                                try {
                                        profile.addField(fieldName.trim()).setValue(fieldValue);
-                               } catch (IllegalArgumentException iae1) {
-                                       logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1);
+                               } catch (DuplicateField df1) {
+                                       logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), df1);
                                        return null;
                                }
                        }
index 4bf26cc..1f80248 100644 (file)
@@ -326,7 +326,9 @@ 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");
-               checkArgument(getFieldByName(fieldName) == null, "fieldName must be unique");
+               if (getFieldByName(fieldName) != null) {
+                       throw new DuplicateField();
+               }
                @SuppressWarnings("synthetic-access")
                Field field = new Field().setName(fieldName);
                fields.add(field);
@@ -553,4 +555,11 @@ public class Profile implements Fingerprintable {
 
        }
 
+       /**
+        * Exception that signals the addition of a field that already exists.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public static class DuplicateField extends IllegalArgumentException { }
+
 }
index 39c2561..d7d729f 100644 (file)
@@ -23,6 +23,7 @@ import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
 import java.util.List;
 
 import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.DuplicateField;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -101,7 +102,7 @@ public class EditProfilePage extends SoneTemplatePage {
                                        fields = profile.getFields();
                                        webInterface.getCore().touchConfiguration();
                                        throw new RedirectException("editProfile.html#profile-fields");
-                               } catch (IllegalArgumentException iae1) {
+                               } catch (DuplicateField df1) {
                                        templateContext.set("fieldName", fieldName);
                                        templateContext.set("duplicateFieldName", true);
                                }