From 09f17d246bd19f84132ac667fb9d69a8c8dcf027 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 9 Apr 2020 20:01:42 +0100 Subject: [PATCH] Add additional removeFirst() variants to IterableUtils Signed-off-by: Graham --- .../dev/openrs2/util/collect/IterableUtils.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/util/src/main/java/dev/openrs2/util/collect/IterableUtils.kt b/util/src/main/java/dev/openrs2/util/collect/IterableUtils.kt index e59dc40600..2146ed86d4 100644 --- a/util/src/main/java/dev/openrs2/util/collect/IterableUtils.kt +++ b/util/src/main/java/dev/openrs2/util/collect/IterableUtils.kt @@ -1,5 +1,21 @@ package dev.openrs2.util.collect +fun MutableIterable.removeFirst(): T { + return removeFirstOrNull() ?: throw NoSuchElementException() +} + +fun MutableIterable.removeFirstOrNull(): T? { + val iterator = iterator() + if (!iterator.hasNext()) { + return null + } + + val element = iterator.next() + iterator.remove() + + return element +} + inline fun MutableIterable.removeFirst(predicate: (T) -> Boolean): Boolean { val iterator = iterator() for (element in iterator) {