KOptional.flatMap

This commit is contained in:
DBotThePony 2024-02-21 10:53:33 +07:00
parent 536b81e27b
commit 7ca9ff3a5b
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -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) {