commonMain.com.bkahlert.kommons.enum.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kommons-core-jvm Show documentation
Show all versions of kommons-core-jvm Show documentation
Kommons Core is a Kotlin Multiplatform Library that offers shared features for all Kommons modules.
package com.bkahlert.kommons
import kotlin.reflect.KProperty1
/** Returns the first enum entry matched by the specified [filter], or `null` otherwise. */
public inline fun > firstEnumValueOfOrNull(filter: (E) -> Boolean): E? =
enumValues().firstOrNull(filter)
/** Returns the first enum entry of which the specified [property] is matched by the specified [filter], or `null` otherwise. */
public inline fun , V> firstEnumValueOfOrNull(property: KProperty1, filter: (V) -> Boolean): E? =
enumValues().firstOrNull { filter(property.get(it)) }
/** Returns the first enum entry of which the specified [property] matches the specified [probe], or `null` otherwise. */
public inline fun , V> firstEnumValueOfOrNull(property: KProperty1, probe: V): E? =
enumValues().firstOrNull { property.get(it) == probe }
/** Returns the first enum entry matched by the specified [filter], or throws a [NoSuchElementException] otherwise. */
public inline fun > firstEnumValueOf(filter: (E) -> Boolean): E =
firstEnumValueOfOrNull(filter)
?: throw NoSuchElementException("${E::class.simpleName} contains no value matching the predicate.")
/** Returns the first enum entry of which the specified [property] is matched by the specified [filter], or throws a [NoSuchElementException] otherwise. */
public inline fun , V> firstEnumValueOf(property: KProperty1, filter: (V) -> Boolean): E =
firstEnumValueOfOrNull(property, filter)
?: throw NoSuchElementException("${E::class.simpleName} contains no value of which the property ${property.name.quoted} matches the predicate.")
/** Returns the first enum entry of which the specified [property] matches the specified [probe], or throws a [NoSuchElementException] otherwise. */
public inline fun , V> firstEnumValueOf(property: KProperty1, probe: V): E =
firstEnumValueOfOrNull(property, probe)
?: throw NoSuchElementException("${E::class.simpleName} contains no value of which the property ${property.name.quoted} is ${probe.toString().quoted}.")
© 2015 - 2025 Weber Informatics LLC | Privacy Policy