diff options
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt index 3082112ce..d33729ae3 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 <E> ConcurrentLinkedQueue<E>.drainTo(list: MutableCollection<E>) { + inline fun <reified T : Queue<E>, reified E> T.drainForEach(action: (E) -> Unit): T { + while (true) + action(this.poll() ?: break) + return this + } + + inline fun <reified T : Queue<E>, reified E> T.drain(amount: Int): T { + for (i in 1..amount) + this.poll() ?: break + return this + } + + inline fun <reified E, reified K, reified L : MutableCollection<K>> + Queue<E>.drainTo(list: L, action: (E) -> K): L { + while (true) + list.add(action(this.poll() ?: break)) + return list + } + + inline fun <reified E, reified L : MutableCollection<E>> + Queue<E>.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 <reified T> ConcurrentLinkedQueue<T>.drainForEach(action: (T) -> Unit) { - while (true) { - val value = this.poll() ?: break - action(value) - } - } - fun <T> Sequence<T>.takeWhileInclusive(predicate: (T) -> Boolean) = sequence { with(iterator()) { while (hasNext()) { |