All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.chestmc.common.extensions.Commons.kt Maven / Gradle / Ivy

package net.chestmc.common.extensions

typealias TransformAction = T.() -> Unit

/**
 * Transforms this object. This is equals to `apply`.
 */
inline fun  T.transform(action: TransformAction) = apply(action)

/**
 * Transforms this object as a nullable object.
 */
fun  T.nullable(): T? = this

/**
 * Transforms this object as a non null object.
 */
fun  T.nonnull(): T = this

/**
 * Returns this object if non null or the default value.
 */
fun  T?.orElse(defaultValue: T): T = this ?: defaultValue

/**
 * Returns this object if non null or the default value.
 */
inline fun  T?.ifNull(defaultValue: (T?) -> T): T = this ?: defaultValue(this)

/**
 * Executes the specified transform action if this object is not null and returns itself.
 */
inline fun  T?.ifNonnull(action: TransformAction) = this?.transform(action) ?: this

/**
 * Casts the specified value to a type generic type [K].
 */
fun  Any.cast(): K = this as K




© 2015 - 2025 Weber Informatics LLC | Privacy Policy