diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt index 3082112ce596..d33729ae3040 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt @@ -4,14 +4,35 @@ import at.hannibal2.skyhanni.utils.NEUItems.getItemStack import at.hannibal2.skyhanni.utils.renderables.Renderable import net.minecraft.item.ItemStack import java.util.Collections +import java.util.Queue import java.util.WeakHashMap -import java.util.concurrent.ConcurrentLinkedQueue object CollectionUtils { - fun ConcurrentLinkedQueue.drainTo(list: MutableCollection) { + inline fun , reified E> T.drainForEach(action: (E) -> Unit): T { + while (true) + action(this.poll() ?: break) + return this + } + + inline fun , reified E> T.drain(amount: Int): T { + for (i in 1..amount) + this.poll() ?: break + return this + } + + inline fun > + Queue.drainTo(list: L, action: (E) -> K): L { + while (true) + list.add(action(this.poll() ?: break)) + return list + } + + inline fun > + Queue.drainTo(list: L): L { while (true) list.add(this.poll() ?: break) + return list } // Let garbage collector handle the removal of entries in this list @@ -143,13 +164,6 @@ object CollectionUtils { return toList().sorted().reversed().toMap() } - inline fun ConcurrentLinkedQueue.drainForEach(action: (T) -> Unit) { - while (true) { - val value = this.poll() ?: break - action(value) - } - } - fun Sequence.takeWhileInclusive(predicate: (T) -> Boolean) = sequence { with(iterator()) { while (hasNext()) {