aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
index 33e85e2fe..9590ba5c1 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
@@ -429,4 +429,14 @@ object CollectionUtils {
fun <K, V : Any> Map<K?, V>.filterNotNullKeys(): Map<K, V> {
return filterKeys { it != null } as Map<K, V>
}
+
+ /**
+ * Inserts the element at the index or appends it to the end if out of bounds of the list.
+ *
+ * @param index index to insert at, or append if >= size
+ * @param element element to insert or add
+ */
+ fun <E> MutableList<E>.addOrInsert(index: Int, element: E) {
+ if (index < size) add(index, element) else add(element)
+ }
}