forked from openrs2/openrs2
parent
ce741279b4
commit
81e665e9d8
@ -0,0 +1,25 @@ |
||||
package org.openrs2.util.collect |
||||
|
||||
public class UniqueQueue<T> { |
||||
private val queue = ArrayDeque<T>() |
||||
private val set = mutableSetOf<T>() |
||||
|
||||
public fun add(v: T): Boolean { |
||||
if (set.add(v)) { |
||||
queue.addLast(v) |
||||
return true |
||||
} |
||||
|
||||
return false |
||||
} |
||||
|
||||
public fun poll(): T? { |
||||
val v = queue.removeFirstOrNull() |
||||
if (v != null) { |
||||
set.remove(v) |
||||
return v |
||||
} |
||||
|
||||
return null |
||||
} |
||||
} |
@ -0,0 +1,21 @@ |
||||
package org.openrs2.util.collect |
||||
|
||||
import kotlin.test.Test |
||||
import kotlin.test.assertEquals |
||||
import kotlin.test.assertFalse |
||||
import kotlin.test.assertNull |
||||
import kotlin.test.assertTrue |
||||
|
||||
class UniqueQueueTest { |
||||
@Test |
||||
fun testAddPoll() { |
||||
val queue = UniqueQueue<String>() |
||||
assertTrue(queue.add("a")) |
||||
assertTrue(queue.add("b")) |
||||
assertFalse(queue.add("a")) |
||||
|
||||
assertEquals("a", queue.poll()) |
||||
assertEquals("b", queue.poll()) |
||||
assertNull(queue.poll()) |
||||
} |
||||
} |
Loading…
Reference in new issue