optional workaround

This commit is contained in:
InsanusMokrassar 2021-11-17 21:31:35 +06:00
parent bcbab3b380
commit e337cd98c8
2 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,7 @@
* `Common`:
* Ranges intersection functionality
* New type `Optional`
* `Pagination`:
* `Pagination` now extends `ClosedRange<Int>`
* `Pagination` intersection functionality

View File

@ -50,3 +50,24 @@ fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T> = apply {
fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T> = apply {
if (!dataPresented) { block() }
}
/**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or null otherwise
*/
fun <T> Optional<T>.dataOrNull() = if (dataPresented) data as T else null
/**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or throw [throwable] otherwise
*/
fun <T> Optional<T>.dataOrThrow(throwable: Throwable) = if (dataPresented) data as T else throw throwable
/**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it
*/
fun <T> Optional<T>.dataOrElse(block: () -> T) = if (dataPresented) data as T else block()
/**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it
*/
suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T) = if (dataPresented) data as T else block()