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

commonMain.com.bkahlert.kommons.enum.kt Maven / Gradle / Ivy

Go to download

Kommons Core is a Kotlin Multiplatform Library that offers shared features for all Kommons modules.

There is a newer version: 2.8.0
Show newest version
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