Skip to content

Commit

Permalink
Renamed module
Browse files Browse the repository at this point in the history
  • Loading branch information
zahichemaly committed Jun 13, 2021
1 parent df5cc15 commit 73a8fd5
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(path: ':EnumLookups')
implementation project(path: ':enumlookups')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,41 @@ package com.zc.enumlookups

/**
* Reverse lookup of an enum using ordinal.
* @param value: Ordinal value.
* @param default: Default enum if not found. If not specified, the first enum is returned.
*/
inline fun <reified T : Enum<T>> lookup(value: Int, default: T = enumValues<T>().first()): T {
return enumValues<T>().find { it.ordinal == value } ?: default
}

/**
* Reverse lookup of an enum using name.
* @param name: Name of enum
* @param default: Default enum if not found. If not specified, the first enum is returned.
* @param ignoreCase: `true` to ignore character case when comparing enum names.
*/
inline fun <reified T : Enum<T>> lookupByName(name: String, default: T = enumValues<T>().first(), ignoreCase: Boolean = true): T {
return enumValues<T>().find { it.name.contentEquals(name, ignoreCase) } ?: default
}

/**
* Reverse lookup of an enum using a field as key.
* @param value: Value of enum.
*/
inline fun <reified T : Enum<T>, V> ((T) -> V).lookupByValue(value: V, default: T = enumValues<T>().first()): T {
return enumValues<T>().find { this(it) == value } ?: default
}

/**
* Reverse lookup of enums using a predicate.
* Returns a list of enums matching the predicate.
*/
inline fun <reified T : Enum<T>> lookupFilter(predicate: (T) -> Boolean): List<T> {
return enumValues<T>().filter(predicate)
}

/**
* Reverse lookup of an enum using a field as key.
* Reverse lookup operator. Calls [lookupByValue] with default params.
* @param value: Value of enum.
*/
inline infix fun <reified T : Enum<T>, V> ((T) -> V).from(value: V): T {
return lookupByValue(value)
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = "EnumlookupsExample"
include ':app'
include ':EnumLookups'
include ':enumlookups'

0 comments on commit 73a8fd5

Please sign in to comment.