KOptional.orThrow
This commit is contained in:
parent
e1afbee071
commit
83fe5d3ef1
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=2.9.21
|
projectVersion=2.9.22
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -26,6 +26,9 @@ class KOptional<T> private constructor(private val _value: T, val isPresent: Boo
|
|||||||
throw NoSuchElementException("No value is present")
|
throw NoSuchElementException("No value is present")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline val isEmpty: Boolean
|
||||||
|
get() = !isPresent
|
||||||
|
|
||||||
inline fun ifPresent(block: (T) -> Unit): KOptional<T> {
|
inline fun ifPresent(block: (T) -> Unit): KOptional<T> {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
block.invoke(value)
|
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> {
|
infix fun or(value: KOptional<T>): KOptional<T> {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
return this
|
return this
|
||||||
|
Loading…
Reference in New Issue
Block a user