KOptional.orThrow

This commit is contained in:
DBotThePony 2024-03-19 18:00:42 +07:00
parent e1afbee071
commit 83fe5d3ef1
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 12 additions and 1 deletions
gradle.properties
src/main/kotlin/ru/dbotthepony/kommons/util

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=2.9.21
projectVersion=2.9.22
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -26,6 +26,9 @@ class KOptional<T> private constructor(private val _value: T, val isPresent: Boo
throw NoSuchElementException("No value is present")
}
inline val isEmpty: Boolean
get() = !isPresent
inline fun ifPresent(block: (T) -> Unit): KOptional<T> {
if (isPresent) {
block.invoke(value)
@ -75,6 +78,14 @@ class KOptional<T> private constructor(private val _value: T, val isPresent: Boo
}
}
inline fun orThrow(supplier: () -> Throwable): T {
if (isPresent) {
return this.value
} else {
throw supplier.invoke()
}
}
infix fun or(value: KOptional<T>): KOptional<T> {
if (isPresent) {
return this