Create Guava optional utility collection
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 3 Jun 2017 16:08:04 +0000 (18:08 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 09:55:28 +0000 (11:55 +0200)
17 files changed:
src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/fcp/GetPostCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt
src/test/kotlin/net/pterodactylus/sone/test/Guava.kt [deleted file]
src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt

diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt
new file mode 100644 (file)
index 0000000..35487ed
--- /dev/null
@@ -0,0 +1,7 @@
+package net.pterodactylus.sone.utils
+
+import com.google.common.base.Optional
+
+fun <T, R> Optional<T>.let(block: (T) -> R): R? = if (isPresent) block(get()) else null
+
+fun <T> T?.asOptional(): Optional<T> = this?.let { Optional.of(it) } ?: Optional.absent<T>()
index 1b4fa2e..b7f911c 100644 (file)
@@ -3,9 +3,9 @@ package net.pterodactylus.sone.fcp
 import freenet.support.SimpleFieldSet
 import net.pterodactylus.sone.core.Core
 import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.equalTo
index b972a53..72daa07 100644 (file)
@@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp
 
 import freenet.support.SimpleFieldSet
 import net.pterodactylus.sone.core.Core
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.equalTo
index d90ec1c..6483085 100644 (file)
@@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp
 
 import freenet.support.SimpleFieldSet
 import net.pterodactylus.sone.core.Core
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.equalTo
index 710a5df..e73160b 100644 (file)
@@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp
 
 import net.pterodactylus.sone.core.Core
 import net.pterodactylus.sone.freenet.fcp.FcpException
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.nullValue
index 86fb586..54f784f 100644 (file)
@@ -3,9 +3,9 @@ package net.pterodactylus.sone.fcp
 import net.pterodactylus.sone.core.Core
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.freenet.fcp.FcpException
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.junit.Before
index d222c27..69ace0a 100644 (file)
@@ -4,9 +4,9 @@ import net.pterodactylus.sone.core.Core
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.freenet.fcp.FcpException
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.junit.Before
index 1588bfe..1caea96 100644 (file)
@@ -1,8 +1,8 @@
 package net.pterodactylus.sone.fcp
 
 import net.pterodactylus.sone.core.Core
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.junit.Before
index f3994c8..dfed710 100644 (file)
@@ -11,9 +11,9 @@ import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.freenet.fcp.FcpException
 import net.pterodactylus.sone.template.SoneAccessor
 import net.pterodactylus.sone.test.OneByOneMatcher
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.junit.Before
 import org.junit.Rule
 import org.junit.rules.ExpectedException
index 5edc03e..c002db3 100644 (file)
@@ -1,8 +1,8 @@
 package net.pterodactylus.sone.fcp
 
 import net.pterodactylus.sone.core.Core
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
 import org.junit.Before
diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guava.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guava.kt
deleted file mode 100644 (file)
index 076962b..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package net.pterodactylus.sone.test
-
-import com.google.common.base.Optional
-import com.google.common.base.Optional.absent
-import com.google.common.base.Optional.of
-
-fun <T> T?.asOptional(): Optional<T> = if (this == null) absent<T>() else of(this)
diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt
new file mode 100644 (file)
index 0000000..2a49db0
--- /dev/null
@@ -0,0 +1,38 @@
+package net.pterodactylus.sone.utils
+
+import com.google.common.base.Optional
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Test
+
+/**
+ * Test for [Optional] utils.
+ */
+class OptionalsTest {
+
+       @Test
+       fun `present optional can be transformed with let`() {
+               val optional = Optional.of(1)
+               assertThat(optional.let { it + 1 }, equalTo(2))
+       }
+
+       @Test
+       fun `empty optional is transform to null with let`() {
+               val optional = Optional.absent<Int>()
+               assertThat(optional.let { it + 1 }, nullValue())
+       }
+
+       @Test
+       fun `1 as optional is correct optional`() {
+               val optional = 1.asOptional()
+               assertThat(optional.get(), equalTo(1))
+       }
+
+       @Test
+       fun `null as optional is asent optional`() {
+               val optional = null.asOptional()
+               assertThat(optional.isPresent, equalTo(false))
+       }
+
+}
index e719c06..11d5291 100644 (file)
@@ -11,10 +11,10 @@ import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.data.Sone.SoneStatus
 import net.pterodactylus.sone.data.Sone.SoneStatus.idle
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.deepMock
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.FreenetRequest
 import net.pterodactylus.util.notify.Notification
index 657c0ce..4cb6a8c 100644 (file)
@@ -2,8 +2,8 @@ package net.pterodactylus.sone.web.pages
 
 import com.google.common.base.Optional.absent
 import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.utils.asOptional
 import net.pterodactylus.util.web.Method.POST
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
index d47560b..67f7811 100644 (file)
@@ -2,10 +2,10 @@ package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
 import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.contains
 import org.hamcrest.Matchers.containsInAnyOrder
index a77ffcc..540b8b5 100644 (file)
@@ -8,10 +8,10 @@ import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Profile
 import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.isOnPage
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.contains
 import org.hamcrest.Matchers.equalTo
index 7161684..6850cf1 100644 (file)
@@ -4,9 +4,9 @@ import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Profile
 import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.asOptional
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.contains
 import org.hamcrest.Matchers.equalTo