From 7ca9ff3a5b77d77886d9d396fbae4bd293afcd79 Mon Sep 17 00:00:00 2001 From: DBotThePony <dbotthepony@yandex.ru> Date: Wed, 21 Feb 2024 10:53:33 +0700 Subject: [PATCH] KOptional.flatMap --- gradle.properties | 2 +- src/main/kotlin/ru/dbotthepony/kommons/util/KOptional.kt | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 34ff5a3..ff9271c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.7.2 +projectVersion=2.7.3 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/util/KOptional.kt b/src/main/kotlin/ru/dbotthepony/kommons/util/KOptional.kt index 37fe34b..42b1836 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/util/KOptional.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/util/KOptional.kt @@ -5,6 +5,7 @@ import java.util.function.Supplier fun <T> KOptional(value: T) = KOptional.of(value) fun KOptional(value: Boolean) = KOptional.of(value) fun KOptional(value: Boolean?) = KOptional.of(value) +fun <T> KOptional() = KOptional.empty<T>() /** * [java.util.Optional] supporting nulls @@ -49,6 +50,14 @@ class KOptional<T> private constructor(private val _value: T, val isPresent: Boo } } + inline fun <R> flatMap(block: (T) -> KOptional<R>): KOptional<R> { + if (isPresent) { + return block.invoke(value) + } else { + return empty() + } + } + @Suppress("NOTHING_TO_INLINE") inline fun orElse(value: T): T { if (isPresent) {