suppressions in Optional.kt

This commit is contained in:
InsanusMokrassar 2021-11-18 23:03:24 +06:00
parent e337cd98c8
commit 6bbbea0bc3

View File

@ -1,3 +1,5 @@
@file:Suppress("unused")
package dev.inmo.micro_utils.common package dev.inmo.micro_utils.common
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -41,7 +43,7 @@ inline val <T> T.optional
* Will call [block] when data presented ([Optional.dataPresented] == true) * Will call [block] when data presented ([Optional.dataPresented] == true)
*/ */
fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T> = apply { fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T> = apply {
if (dataPresented) { block(data as T) } if (dataPresented) { @Suppress("UNCHECKED_CAST") block(data as T) }
} }
/** /**
@ -54,20 +56,20 @@ fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T> = apply {
/** /**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or null otherwise * 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 fun <T> Optional<T>.dataOrNull() = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else null
/** /**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or throw [throwable] otherwise * 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 fun <T> Optional<T>.dataOrThrow(throwable: Throwable) = if (dataPresented) @Suppress("UNCHECKED_CAST") (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 * 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() fun <T> Optional<T>.dataOrElse(block: () -> T) = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else block()
/** /**
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it * 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() suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T) = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else block()