♻️ Don’t use Optional as parameter type
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Jul 2019 12:50:37 +0000 (14:50 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Jul 2019 12:50:37 +0000 (14:50 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java
src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt
src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt

index f004cf2..2ff27c9 100644 (file)
@@ -98,7 +98,6 @@ import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
@@ -1063,7 +1062,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The text of the post
         * @return The created post
         */
-       public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
+       public Post createPost(Sone sone, @Nullable Sone recipient, String text) {
                checkNotNull(text, "text must not be null");
                checkArgument(text.trim().length() > 0, "text must not be empty");
                if (!sone.isLocal()) {
@@ -1072,8 +1071,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
                PostBuilder postBuilder = database.newPostBuilder();
                postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
-               if (recipient.isPresent()) {
-                       postBuilder.to(recipient.get().getId());
+               if (recipient != null) {
+                       postBuilder.to(recipient.getId());
                }
                final Post post = postBuilder.build();
                database.storePost(post);
index 2c8456d..221ea7a 100644 (file)
@@ -29,7 +29,7 @@ import freenet.support.SimpleFieldSet;
 /**
  * FCP command that creates a new {@link Post}.
  *
- * @see Core#createPost(Sone, Optional, String)
+ * @see Core#createPost(Sone, Sone, String)
  */
 public class CreatePostCommand extends AbstractSoneCommand {
 
@@ -57,7 +57,7 @@ public class CreatePostCommand extends AbstractSoneCommand {
                if (sone.equals(recipient)) {
                        return new ErrorResponse("Sone and Recipient must not be the same.");
                }
-               Post post = getCore().createPost(sone, Optional.fromNullable(recipient), text);
+               Post post = getCore().createPost(sone, recipient, text);
                return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get());
        }
 
index 79e66ad..28af3e3 100644 (file)
@@ -19,7 +19,7 @@ class CreatePostAjaxPage @Inject constructor(webInterface: WebInterface) : Logge
                                        ?.let { text ->
                                                val sender = request.parameters["sender"].emptyToNull?.let(core::getSone) ?: currentSone
                                                val recipient = request.parameters["recipient"]?.let(core::getSone)
-                                               core.createPost(sender, recipient.asOptional(), text).let { post ->
+                                               core.createPost(sender, recipient, text).let { post ->
                                                        createSuccessJsonObject().apply {
                                                                put("postId", post.id)
                                                                put("sone", sender.id)
index b47c2b1..2f8e054 100644 (file)
@@ -28,7 +28,7 @@ class CreatePostPage @Inject constructor(webInterface: WebInterface, loaders: Lo
                        }
                        val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
                        val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43))
-                       soneRequest.core.createPost(sender, recipient.asOptional(), TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
+                       soneRequest.core.createPost(sender, recipient, TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
                        throw RedirectException(returnPage)
                }
        }
index 29db41c..cfbda94 100644 (file)
@@ -56,7 +56,7 @@ class CreatePostCommandTest : SoneCommandTest() {
                parameters += "Text" to "Test"
                whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
                val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
-               whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post)
+               whenever(core.createPost(localSone, null, "Test")).thenReturn(post)
                val response = command.execute(parameters)
                assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
                assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
@@ -90,7 +90,7 @@ class CreatePostCommandTest : SoneCommandTest() {
                whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
                whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
                val post = mock<Post>().apply { whenever(id).thenReturn("PostId") }
-               whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post)
+               whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post)
                val response = command.execute(parameters)
                assertThat(response.replyParameters.get("Message"), equalTo("PostCreated"))
                assertThat(response.replyParameters.get("Post"), equalTo("PostId"))
index 821198f..b79e6df 100644 (file)
@@ -40,7 +40,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::
        fun `request with valid data creates post`() {
                addRequestParameter("text", "test")
                val post = createPost()
-               whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
+               whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
                assertThatJsonIsSuccessful()
                assertThat(json["postId"]?.asText(), equalTo("id"))
                assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
@@ -52,7 +52,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::
                addRequestParameter("text", "test")
                addRequestParameter("recipient", "invalid")
                val post = createPost()
-               whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
+               whenever(core.createPost(currentSone, null, "test")).thenReturn(post)
                assertThatJsonIsSuccessful()
                assertThat(json["postId"]?.asText(), equalTo("id"))
                assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
@@ -66,7 +66,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::
                val recipient = mock<Sone>().apply { whenever(id).thenReturn("valid") }
                addSone(recipient)
                val post = createPost("valid")
-               whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post)
+               whenever(core.createPost(currentSone, recipient, "test")).thenReturn(post)
                assertThatJsonIsSuccessful()
                assertThat(json["postId"]?.asText(), equalTo("id"))
                assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
@@ -78,7 +78,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = ::
                addRequestParameter("text", "Link http://freenet.test:8888/KSK@foo is filtered")
                addRequestHeader("Host", "freenet.test:8888")
                val post = createPost()
-               whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post)
+               whenever(core.createPost(currentSone, null, "Link KSK@foo is filtered")).thenReturn(post)
                assertThatJsonIsSuccessful()
                assertThat(json["postId"]?.asText(), equalTo("id"))
                assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
index 2bf0c05..6212c90 100644 (file)
@@ -40,7 +40,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) {
                addHttpRequestPart("returnPage", "return.html")
                addHttpRequestPart("text", "post text")
                verifyRedirect("return.html") {
-                       verify(core).createPost(currentSone, absent(), "post text")
+                       verify(core).createPost(currentSone, null, "post text")
                }
        }
 
@@ -62,7 +62,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) {
                val sender = mock<Sone>()
                addLocalSone("sender-id", sender)
                verifyRedirect("return.html") {
-                       verify(core).createPost(sender, absent(), "post text")
+                       verify(core).createPost(sender, null, "post text")
                }
        }
 
@@ -75,7 +75,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) {
                val recipient = mock<Sone>()
                addSone("recipient-id", recipient)
                verifyRedirect("return.html") {
-                       verify(core).createPost(currentSone, recipient.asOptional(), "post text")
+                       verify(core).createPost(currentSone, recipient, "post text")
                }
        }
 
@@ -86,7 +86,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) {
                addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text")
                addHttpRequestHeader("Host", "localhost:12345")
                verifyRedirect("return.html") {
-                       verify(core).createPost(currentSone, absent(), "post KSK@foo text")
+                       verify(core).createPost(currentSone, null, "post KSK@foo text")
                }
        }