Add addAll and clear methods to UniqueQueue

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 82c78d514d
commit 49e5781288
  1. 19
      util/src/main/kotlin/org/openrs2/util/collect/UniqueQueue.kt
  2. 20
      util/src/test/kotlin/org/openrs2/util/collect/UniqueQueueTest.kt

@ -13,6 +13,20 @@ public class UniqueQueue<T> {
return false return false
} }
public operator fun plusAssign(v: T) {
add(v)
}
public fun addAll(vs: Iterable<T>) {
for (v in vs) {
add(v)
}
}
public operator fun plusAssign(vs: Iterable<T>) {
addAll(vs)
}
public fun poll(): T? { public fun poll(): T? {
val v = queue.removeFirstOrNull() val v = queue.removeFirstOrNull()
if (v != null) { if (v != null) {
@ -22,4 +36,9 @@ public class UniqueQueue<T> {
return null return null
} }
public fun clear() {
queue.clear()
set.clear()
}
} }

@ -18,4 +18,24 @@ class UniqueQueueTest {
assertEquals("b", queue.poll()) assertEquals("b", queue.poll())
assertNull(queue.poll()) assertNull(queue.poll())
} }
@Test
fun testAddAll() {
val queue = UniqueQueue<String>()
queue.addAll(listOf("a", "b", "a"))
assertEquals("a", queue.poll())
assertEquals("b", queue.poll())
assertNull(queue.poll())
}
@Test
fun testClear() {
val queue = UniqueQueue<String>()
assertTrue(queue.add("a"))
queue.clear()
assertNull(queue.poll())
}
} }

Loading…
Cancel
Save