org.hnau.base.extensions.NullableIfExtensions.kt Maven / Gradle / Ivy
package org.hnau.base.extensions
/**
* Return result of executing [ifNotNull] if not null, or result of executing [ifNull]
*/
inline fun T?.checkNullable(ifNotNull: (T) -> R, ifNull: () -> R) =
if (this != null) ifNotNull(this) else ifNull()
/**
* Return result of executing [ifNull] if null or return null
*/
inline fun T.ifNull(ifNull: () -> R) =
checkNullable({ null }, ifNull)
/**
* Return result of executing [ifNotNull] if not null or return null
*/
inline fun T?.ifNotNull(ifNotNull: (T) -> R) =
checkNullable(ifNotNull, { null })
/**
* Return [this] if not null, or result of executing [ifNull]
*/
inline fun T?.elvis(ifNull: () -> T) = this ?: ifNull()
inline fun isNullable() = null is T
fun T?.sureNotNull() = this!!
© 2015 - 2025 Weber Informatics LLC | Privacy Policy