Skip to content

Commit

Permalink
Update based on review feedback...
Browse files Browse the repository at this point in the history
Also improve readability of a really long takeIf statement.
  • Loading branch information
tcobbs-bentley committed Feb 26, 2024
1 parent 19f8822 commit 6797449
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
22 changes: 11 additions & 11 deletions mobile-sdk/src/main/java/com/github/itwin/mobilesdk/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ fun <K, V> Map<K, V>.getOptionalDouble(key: K) =
* @return The receiver specialized with `K` and `V` if the key and value types match, otherwise
* `null`.
*/
inline fun <reified K: Any, reified V: Any> Map<*,*>.checkEntriesAre(): Map<K, V>? {
forEach {
if (it.key !is K) return null
if (it.value !is V) return null
inline fun <reified K: Any, reified V: Any> Map<*,*>.checkEntriesAre(): Map<K, V>? =
this.takeIf {
all { it.key is K && it.value is V }
}?.let {
@Suppress("UNCHECKED_CAST")
it as Map<K, V>
}
@Suppress("UNCHECKED_CAST")
return this as Map<K, V>
}

/**
* Ensure that all entries in the receiver have a key type of `K` and a value type of `V`.
Expand All @@ -108,8 +107,9 @@ inline fun <reified K: Any, reified V: Any> Map<*,*>.ensureEntriesAre(): Map<K,
* @return The receiver specialized with `T` if the item types match, otherwise `null`.
*/
inline fun <reified T> List<*>.checkItemsAre(): List<T>? =
if (all { it is T })
this.takeIf {
all { it is T }
}?.let {
@Suppress("UNCHECKED_CAST")
this as List<T>
else
null
it as List<T>
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ abstract class ITMActionable(nativeUI: ITMNativeUI): ITMNativeUIComponent(native
Destructive;

companion object {
fun fromString(style: String?) = style?.takeIf { it.isNotEmpty() }?.let { Style.valueOf(style.replaceFirstChar { it.uppercase() }) } ?: Default
fun fromString(style: String?) =
style?.takeIf {
it.isNotEmpty()
}?.let {
Style.valueOf(style.replaceFirstChar { it.uppercase() })
} ?: Default
}
}

Expand Down

0 comments on commit 6797449

Please sign in to comment.