Return optionals of fields.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 23 Oct 2013 05:49:12 +0000 (07:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:34 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
src/test/java/net/pterodactylus/sone/data/ProfileTest.java

index 3e446fd..6201276 100644 (file)
@@ -17,7 +17,9 @@
 
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Optional.absent;
 import static com.google.common.base.Optional.fromNullable;
+import static com.google.common.base.Optional.of;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
@@ -184,39 +186,23 @@ public class Profile implements Fingerprintable {
                return fields.contains(field);
        }
 
-       /**
-        * Returns the field with the given ID.
-        *
-        * @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 Field getFieldById(String fieldId) {
+       public Optional<Field> getFieldById(String fieldId) {
                checkNotNull(fieldId, "fieldId must not be null");
                for (Field field : fields) {
                        if (field.getId().equals(fieldId)) {
-                               return field;
+                               return of(field);
                        }
                }
-               return null;
+               return absent();
        }
 
-       /**
-        * Returns the field with the given name.
-        *
-        * @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 Field getFieldByName(String fieldName) {
+       public Optional<Field> getFieldByName(String fieldName) {
                for (Field field : fields) {
                        if (field.getName().equals(fieldName)) {
-                               return field;
+                               return of(field);
                        }
                }
-               return null;
+               return absent();
        }
 
        /**
index b6f3eeb..93b90d1 100644 (file)
@@ -25,6 +25,8 @@ import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.web.Method;
 
+import com.google.common.base.Optional;
+
 /**
  * Page that lets the user confirm the deletion of a profile field.
  *
@@ -56,8 +58,8 @@ public class DeleteProfileFieldPage extends SoneTemplatePage {
 
                /* get parameters from request. */
                String fieldId = request.getHttpRequest().getParam("field");
-               Field field = profile.getFieldById(fieldId);
-               if (field == null) {
+               Optional<Field> field = profile.getFieldById(fieldId);
+               if (!field.isPresent()) {
                        throw new RedirectException("invalid.html");
                }
 
@@ -66,17 +68,17 @@ public class DeleteProfileFieldPage extends SoneTemplatePage {
                        if (request.getHttpRequest().getPartAsStringFailsafe("confirm", 4).equals("true")) {
                                fieldId = request.getHttpRequest().getParam("field");
                                field = profile.getFieldById(fieldId);
-                               if (field == null) {
+                               if (!field.isPresent()) {
                                        throw new RedirectException("invalid.html");
                                }
-                               profile.removeField(field);
+                               profile.removeField(field.get());
                                currentSone.setProfile(profile);
                        }
                        throw new RedirectException("editProfile.html#profile-fields");
                }
 
                /* set current values in template. */
-               templateContext.set("field", field);
+               templateContext.set("field", field.get());
        }
 
 }
index d4510e7..a71abdc 100644 (file)
@@ -25,6 +25,8 @@ import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.web.Method;
 
+import com.google.common.base.Optional;
+
 /**
  * Page that lets the user edit the name of a profile field.
  *
@@ -56,8 +58,8 @@ public class EditProfileFieldPage extends SoneTemplatePage {
 
                /* get parameters from request. */
                String fieldId = request.getHttpRequest().getParam("field");
-               Field field = profile.getFieldById(fieldId);
-               if (field == null) {
+               Optional<Field> field = profile.getFieldById(fieldId);
+               if (!field.isPresent()) {
                        throw new RedirectException("invalid.html");
                }
 
@@ -68,13 +70,13 @@ public class EditProfileFieldPage extends SoneTemplatePage {
                        }
                        fieldId = request.getHttpRequest().getPartAsStringFailsafe("field", 36);
                        field = profile.getFieldById(fieldId);
-                       if (field == null) {
+                       if (!field.isPresent()) {
                                throw new RedirectException("invalid.html");
                        }
                        String name = request.getHttpRequest().getPartAsStringFailsafe("name", 256);
-                       Field existingField = profile.getFieldByName(name);
-                       if ((existingField == null) || (existingField.equals(field))) {
-                               profile.renameField(field, name);
+                       Optional<Field> existingField = profile.getFieldByName(name);
+                       if (!existingField.isPresent() || existingField.equals(field)) {
+                               profile.renameField(field.get(), name);
                                currentSone.setProfile(profile);
                                throw new RedirectException("editProfile.html#profile-fields");
                        }
@@ -82,7 +84,7 @@ public class EditProfileFieldPage extends SoneTemplatePage {
                }
 
                /* store current values in template. */
-               templateContext.set("field", field);
+               templateContext.set("field", field.get());
        }
 
 }
index 38cc800..df5a352 100644 (file)
@@ -29,8 +29,11 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.web.Method;
+
 import freenet.clients.http.ToadletContext;
 
+import com.google.common.base.Optional;
+
 /**
  * This page lets the user edit her profile.
  *
@@ -106,21 +109,21 @@ public class EditProfilePage extends SoneTemplatePage {
                                }
                                id = getFieldId(request, "move-up-field-");
                                if (id != null) {
-                                       Field field = profile.getFieldById(id);
-                                       if (field == null) {
+                                       Optional<Field> field = profile.getFieldById(id);
+                                       if (!field.isPresent()) {
                                                throw new RedirectException("invalid.html");
                                        }
-                                       profile.moveFieldUp(field);
+                                       profile.moveFieldUp(field.get());
                                        currentSone.setProfile(profile);
                                        throw new RedirectException("editProfile.html#profile-fields");
                                }
                                id = getFieldId(request, "move-down-field-");
                                if (id != null) {
-                                       Field field = profile.getFieldById(id);
-                                       if (field == null) {
+                                       Optional<Field> field = profile.getFieldById(id);
+                                       if (!field.isPresent()) {
                                                throw new RedirectException("invalid.html");
                                        }
-                                       profile.moveFieldDown(field);
+                                       profile.moveFieldDown(field.get());
                                        currentSone.setProfile(profile);
                                        throw new RedirectException("editProfile.html#profile-fields");
                                }
index f037251..b787e8c 100644 (file)
@@ -27,6 +27,7 @@ import net.pterodactylus.sone.web.page.FreenetRequest;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.base.Optional;
 
 /**
  * AJAX page that lets the user delete a profile field.
@@ -50,14 +51,14 @@ public class DeleteProfileFieldAjaxPage extends JsonPage {
                String fieldId = request.getHttpRequest().getParam("field");
                Sone currentSone = getCurrentSone(request.getToadletContext());
                Profile profile = currentSone.getProfile();
-               Field field = profile.getFieldById(fieldId);
-               if (field == null) {
+               Optional<Field> field = profile.getFieldById(fieldId);
+               if (!field.isPresent()) {
                        return createErrorJsonObject("invalid-field-id");
                }
-               profile.removeField(field);
+               profile.removeField(field.get());
                currentSone.setProfile(profile);
                webInterface.getCore().touchConfiguration();
-               return createSuccessJsonObject().put("field", new ObjectNode(instance).put("id", new TextNode(field.getId())));
+               return createSuccessJsonObject().put("field", new ObjectNode(instance).put("id", new TextNode(field.get().getId())));
        }
 
 }
index 50aea6c..319aa79 100644 (file)
@@ -23,6 +23,8 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 
+import com.google.common.base.Optional;
+
 /**
  * AJAX page that lets the user rename a profile field.
  *
@@ -49,19 +51,19 @@ public class EditProfileFieldAjaxPage extends JsonPage {
                String fieldId = request.getHttpRequest().getParam("field");
                Sone currentSone = getCurrentSone(request.getToadletContext());
                Profile profile = currentSone.getProfile();
-               Field field = profile.getFieldById(fieldId);
-               if (field == null) {
+               Optional<Field> field = profile.getFieldById(fieldId);
+               if (!field.isPresent()) {
                        return createErrorJsonObject("invalid-field-id");
                }
                String name = request.getHttpRequest().getParam("name", "").trim();
                if (name.length() == 0) {
                        return createErrorJsonObject("invalid-parameter-name");
                }
-               Field existingField = profile.getFieldByName(name);
-               if ((existingField != null) && !existingField.equals(field)) {
+               Optional<Field> existingField = profile.getFieldByName(name);
+               if (existingField.isPresent() && !existingField.equals(field)) {
                        return createErrorJsonObject("duplicate-field-name");
                }
-               profile.renameField(field, name);
+               profile.renameField(field.get(), name);
                currentSone.setProfile(profile);
                return createSuccessJsonObject();
        }
index 301e59a..5e8a2bd 100644 (file)
@@ -23,6 +23,8 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 
+import com.google.common.base.Optional;
+
 /**
  * AJAX page that lets the user move a profile field up or down.
  *
@@ -51,16 +53,16 @@ public class MoveProfileFieldAjaxPage extends JsonPage {
                Sone currentSone = getCurrentSone(request.getToadletContext());
                Profile profile = currentSone.getProfile();
                String fieldId = request.getHttpRequest().getParam("field");
-               Field field = profile.getFieldById(fieldId);
-               if (field == null) {
+               Optional<Field> field = profile.getFieldById(fieldId);
+               if (!field.isPresent()) {
                        return createErrorJsonObject("invalid-field-id");
                }
                String direction = request.getHttpRequest().getParam("direction");
                try {
                        if ("up".equals(direction)) {
-                               profile.moveFieldUp(field);
+                               profile.moveFieldUp(field.get());
                        } else if ("down".equals(direction)) {
-                               profile.moveFieldDown(field);
+                               profile.moveFieldDown(field.get());
                        } else {
                                return createErrorJsonObject("invalid-direction");
                        }
index 0fcb80e..0801520 100644 (file)
@@ -19,10 +19,10 @@ package net.pterodactylus.sone.data;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.notNullValue;
 
 import net.pterodactylus.sone.data.Profile.Field;
 
+import com.google.common.base.Optional;
 import org.junit.Test;
 
 /**
@@ -37,27 +37,27 @@ public class ProfileTest {
        @Test
        public void testAddingAField() {
                profile.addField("TestField");
-               Field testField = profile.getFieldByName("TestField");
-               assertThat(testField, notNullValue());
-               assertThat(testField.getName(), is("TestField"));
+               Optional<Field> testField = profile.getFieldByName("TestField");
+               assertThat(testField.isPresent(), is(true));
+               assertThat(testField.get().getName(), is("TestField"));
        }
 
        @Test
        public void testRenamingAField() {
                profile.addField("TestField");
-               Field testField = profile.getFieldByName("TestField");
-               profile.renameField(testField, "RenamedField");
-               Field renamedField = profile.getFieldByName("RenamedField");
-               assertThat(testField.getId(), is(renamedField.getId()));
+               Optional<Field> testField = profile.getFieldByName("TestField");
+               profile.renameField(testField.get(), "RenamedField");
+               Optional<Field> renamedField = profile.getFieldByName("RenamedField");
+               assertThat(testField.get().getId(), is(renamedField.get().getId()));
        }
 
        @Test
        public void testChangingTheValueOfAField() {
                profile.addField("TestField");
-               Field testField = profile.getFieldByName("TestField");
-               profile.setField(testField, "Test");
+               Optional<Field> testField = profile.getFieldByName("TestField");
+               profile.setField(testField.get(), "Test");
                testField = profile.getFieldByName("TestField");
-               assertThat(testField.getValue(), is("Test"));
+               assertThat(testField.get().getValue(), is("Test"));
        }
 
 }