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;
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();
}
/**
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.
*
/* 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");
}
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());
}
}
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.
*
/* 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");
}
}
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");
}
}
/* store current values in template. */
- templateContext.set("field", field);
+ templateContext.set("field", field.get());
}
}
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.
*
}
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");
}
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.
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())));
}
}
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.
*
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();
}
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.
*
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");
}
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;
/**
@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"));
}
}