From: David ‘Bombe’ Roden Date: Mon, 19 Jun 2017 08:14:42 +0000 (+0200) Subject: Move function that deals with optionals to optional utils X-Git-Tag: 0.9.7^2~151 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=505e16e44f6b5d6ece6845a50f3f561214cc5709;p=Sone.git Move function that deals with optionals to optional utils --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Iterables.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Iterables.kt deleted file mode 100644 index 5651c72..0000000 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Iterables.kt +++ /dev/null @@ -1,6 +0,0 @@ -package net.pterodactylus.sone.utils - -import com.google.common.base.Optional - -fun Iterable.mapPresent(transform: (T) -> Optional): List = - map(transform).filter { it.isPresent }.map { it.get() } diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt index 5f53ed0..c8167b9 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt @@ -6,3 +6,6 @@ fun Optional.let(block: (T) -> R): R? = if (isPresent) block(get()) el fun Optional.also(block: (T) -> Unit) = if (isPresent) block(get()) else Unit fun T?.asOptional(): Optional = this?.let { Optional.of(it) } ?: Optional.absent() + +fun Iterable.mapPresent(transform: (T) -> Optional): List = + map(transform).filter { it.isPresent }.map { it.get() } diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/IterablesTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/IterablesTest.kt deleted file mode 100644 index 639dc90..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/utils/IterablesTest.kt +++ /dev/null @@ -1,16 +0,0 @@ -package net.pterodactylus.sone.utils - -import com.google.common.base.Optional.fromNullable -import org.hamcrest.MatcherAssert.assertThat -import org.hamcrest.Matchers.contains -import org.junit.Test - -class IterablesTest { - - @Test - fun testMapPresent() { - val originalList = listOf(1, 2, null, 3, null) - assertThat(originalList.mapPresent { fromNullable(it) }, contains(1, 2, 3)) - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt index 6d35509..76dd443 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt @@ -1,7 +1,9 @@ package net.pterodactylus.sone.utils import com.google.common.base.Optional +import com.google.common.base.Optional.fromNullable import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.nullValue import org.junit.Test @@ -50,4 +52,10 @@ class OptionalsTest { assertThat(optional.isPresent, equalTo(false)) } + @Test + fun testMapPresent() { + val originalList = listOf(1, 2, null, 3, null) + assertThat(originalList.mapPresent { fromNullable(it) }, contains(1, 2, 3)) + } + }