--- /dev/null
+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>()
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
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
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
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
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
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
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
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
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
+++ /dev/null
-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)
--- /dev/null
+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))
+ }
+
+}
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
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
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
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
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