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.kt24
1 files changed, 24 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 48cf1ccb2..6b4d70046 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
@@ -184,6 +184,30 @@ object CollectionUtils {
}
}
+ inline fun <T, R> Iterator<T>.consumeWhile(block: (T) -> R): R? {
+ while (hasNext()) {
+ return block(next()) ?: continue
+ }
+ return null
+ }
+
+ inline fun <T> Iterator<T>.collectWhile(block: (T) -> Boolean): List<T> {
+ return collectWhileTo(mutableListOf(), block)
+ }
+
+ inline fun <T, C : MutableCollection<T>> Iterator<T>.collectWhileTo(collection: C, block: (T) -> Boolean): C {
+ while (hasNext()) {
+ val element = next()
+ if (block(element)) {
+ collection.add(element)
+ } else {
+ break
+ }
+ }
+ return collection
+ }
+
+
/** Updates a value if it is present in the set (equals), useful if the newValue is not reference equal with the value in the set */
inline fun <reified T> MutableSet<T>.refreshReference(newValue: T) = if (this.contains(newValue)) {
this.remove(newValue)