-
-
Save programmerr47/e1a62f912c735249c89c3f5c44b6b366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Enum<T>> requireEnumBy( | |
fallback: EnumSet<T>.() -> T = EnumSet<T>::defFallback, | |
predicate: (T) -> Boolean | |
): T = requireEnumBy(T::class.java, fallback, predicate) | |
inline fun <reified T : Enum<T>> findEnumBy( | |
predicate: (T) -> Boolean | |
): T? = with(EnumSet.allOf(T::class.java)) { find(predicate) } | |
inline fun <T : Enum<T>> requireEnumBy( | |
enumCls: Class<T>, | |
fallback: EnumSet<T>.() -> T = { defFallback(enumCls) }, | |
predicate: (T) -> Boolean | |
): T = with(EnumSet.allOf(enumCls)) { find(predicate) ?: fallback() } | |
inline fun <reified T : Enum<T>> find(predicate: (T) -> Boolean): T? = EnumSet.allOf(T::class.java).find(predicate) | |
inline fun <reified T : Enum<T>> EnumSet<T>.defFallback(): T = defFallback(T::class.java) | |
fun <T : Enum<T>> EnumSet<T>.defFallback(enumCls: Class<T>): T = firstOrNull() | |
?: throw IllegalStateException("In the enum ${enumCls.simpleName} there should be at least one enumeration") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment