KOptional.flatMap
This commit is contained in:
parent
536b81e27b
commit
7ca9ff3a5b
@ -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
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user