From 505e16e44f6b5d6ece6845a50f3f561214cc5709 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 19 Jun 2017 10:14:42 +0200 Subject: [PATCH] Move function that deals with optionals to optional utils --- .../kotlin/net/pterodactylus/sone/utils/Iterables.kt | 6 ------ .../kotlin/net/pterodactylus/sone/utils/Optionals.kt | 3 +++ .../kotlin/net/pterodactylus/sone/utils/IterablesTest.kt | 16 ---------------- .../kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt | 8 ++++++++ 4 files changed, 11 insertions(+), 22 deletions(-) delete mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Iterables.kt delete mode 100644 src/test/kotlin/net/pterodactylus/sone/utils/IterablesTest.kt 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)) + } + } -- 2.7.4