projects
/
Sone.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
64718e3
)
Return optionals of fields.
author
David ‘Bombe’ Roden
<bombe@pterodactylus.net>
Wed, 23 Oct 2013 05:49:12 +0000
(07:49 +0200)
committer
David ‘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
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
patch
|
blob
|
history
src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
patch
|
blob
|
history
src/test/java/net/pterodactylus/sone/data/ProfileTest.java
patch
|
blob
|
history
diff --git
a/src/main/java/net/pterodactylus/sone/data/Profile.java
b/src/main/java/net/pterodactylus/sone/data/Profile.java
index
3e446fd
..
6201276
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/data/Profile.java
+++ b/
src/main/java/net/pterodactylus/sone/data/Profile.java
@@
-17,7
+17,9
@@
package net.pterodactylus.sone.data;
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.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;
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);
}
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)) {
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)) {
for (Field field : fields) {
if (field.getName().equals(fieldName)) {
- return
field
;
+ return
of(field)
;
}
}
}
}
- return
null
;
+ return
absent()
;
}
/**
}
/**
diff --git
a/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
b/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
index
b6f3eeb
..
93b90d1
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
@@
-25,6
+25,8
@@
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
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.
*
/**
* 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");
/* 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");
}
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 (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");
}
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. */
currentSone.setProfile(profile);
}
throw new RedirectException("editProfile.html#profile-fields");
}
/* set current values in template. */
- templateContext.set("field", field);
+ templateContext.set("field", field
.get()
);
}
}
}
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
index
d4510e7
..
a71abdc
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
@@
-25,6
+25,8
@@
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
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.
*
/**
* 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");
/* 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");
}
throw new RedirectException("invalid.html");
}
@@
-68,13
+70,13
@@
public class EditProfileFieldPage extends SoneTemplatePage {
}
fieldId = request.getHttpRequest().getPartAsStringFailsafe("field", 36);
field = profile.getFieldById(fieldId);
}
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);
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");
}
currentSone.setProfile(profile);
throw new RedirectException("editProfile.html#profile-fields");
}
@@
-82,7
+84,7
@@
public class EditProfileFieldPage extends SoneTemplatePage {
}
/* store current values in template. */
}
/* store current values in template. */
- templateContext.set("field", field);
+ templateContext.set("field", field
.get()
);
}
}
}
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
index
38cc800
..
df5a352
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
@@
-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 net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+
import freenet.clients.http.ToadletContext;
import freenet.clients.http.ToadletContext;
+import com.google.common.base.Optional;
+
/**
* This page lets the user edit her profile.
*
/**
* 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) {
}
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");
}
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) {
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");
}
throw new RedirectException("invalid.html");
}
- profile.moveFieldDown(field);
+ profile.moveFieldDown(field
.get()
);
currentSone.setProfile(profile);
throw new RedirectException("editProfile.html#profile-fields");
}
currentSone.setProfile(profile);
throw new RedirectException("editProfile.html#profile-fields");
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
index
f037251
..
b787e8c
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
@@
-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.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.
/**
* 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();
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");
}
return createErrorJsonObject("invalid-field-id");
}
- profile.removeField(field);
+ profile.removeField(field
.get()
);
currentSone.setProfile(profile);
webInterface.getCore().touchConfiguration();
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
().get
Id())));
}
}
}
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
b/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
index
50aea6c
..
319aa79
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
@@
-23,6
+23,8
@@
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
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.
*
/**
* 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();
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");
}
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");
}
return createErrorJsonObject("duplicate-field-name");
}
- profile.renameField(field, name);
+ profile.renameField(field
.get()
, name);
currentSone.setProfile(profile);
return createSuccessJsonObject();
}
currentSone.setProfile(profile);
return createSuccessJsonObject();
}
diff --git
a/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
b/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
index
301e59a
..
5e8a2bd
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
+++ b/
src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
@@
-23,6
+23,8
@@
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
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.
*
/**
* 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");
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)) {
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)) {
} else if ("down".equals(direction)) {
- profile.moveFieldDown(field);
+ profile.moveFieldDown(field
.get()
);
} else {
return createErrorJsonObject("invalid-direction");
}
} else {
return createErrorJsonObject("invalid-direction");
}
diff --git
a/src/test/java/net/pterodactylus/sone/data/ProfileTest.java
b/src/test/java/net/pterodactylus/sone/data/ProfileTest.java
index
0fcb80e
..
0801520
100644
(file)
--- a/
src/test/java/net/pterodactylus/sone/data/ProfileTest.java
+++ b/
src/test/java/net/pterodactylus/sone/data/ProfileTest.java
@@
-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.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.notNullValue;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Profile.Field;
+import com.google.common.base.Optional;
import org.junit.Test;
/**
import org.junit.Test;
/**
@@
-37,27
+37,27
@@
public class ProfileTest {
@Test
public void testAddingAField() {
profile.addField("TestField");
@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
().get
Name(), is("TestField"));
}
@Test
public void testRenamingAField() {
profile.addField("TestField");
}
@Test
public void testRenamingAField() {
profile.addField("TestField");
-
Field
testField = profile.getFieldByName("TestField");
- profile.renameField(testField, "RenamedField");
-
Field
renamedField = profile.getFieldByName("RenamedField");
- assertThat(testField.get
Id(), 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");
}
@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");
testField = profile.getFieldByName("TestField");
- assertThat(testField.getValue(), is("Test"));
+ assertThat(testField.get
().get
Value(), is("Test"));
}
}
}
}