Skip to content

Commit

Permalink
Add List.ensureItemsAre() (#92)
Browse files Browse the repository at this point in the history
This is the counterpoint to Map.ensureEntriesAre, and will be used in the code cleanup of the sample apps.
  • Loading branch information
tcobbs-bentley authored Apr 2, 2024
1 parent dfa8a6f commit 151d34b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mobile-sdk/src/main/java/com/github/itwin/mobilesdk/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ inline fun <reified K: Any, reified V: Any> Map<*,*>.checkEntriesAre(): Map<K, V

/**
* Ensure that all entries in the receiver have a key type of `K` and a value type of `V`.
* @throws Throwable If the entries don't have the proper types, this throws while failing to convert
* `null` to `Map<K, V>`.
* @throws Throwable If the entries don't have the proper types, this throws while failing to
* convert `null` to `Map<K, V>`.
* @return The receiver specialized with `K` and `V`.
*/
inline fun <reified K: Any, reified V: Any> Map<*,*>.ensureEntriesAre(): Map<K, V> =
Expand All @@ -114,6 +114,15 @@ inline fun <reified T> List<*>.checkItemsAre(): List<T>? =
it as List<T>
}

/**
* Ensure that all items in the receiver have a type of `T`.
* @throws Throwable If the items don't have the proper type, this throws while failing to convert
* `null` to `List<T>`.
* @return The receiver specialized with `T` if the item types match, otherwise `null`.
*/
inline fun <reified T> List<*>.ensureItemsAre(): List<T> =
checkItemsAre<T>() as List<T>

/**
* Execute a block of code if all other parameters are non-null.
*
Expand Down

0 comments on commit 151d34b

Please sign in to comment.