Return old NamedEntry after removal from NamedEntryCollection

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent ed0b1db81b
commit ab3300a8c7
  1. 16
      cache/src/main/kotlin/org/openrs2/cache/NamedEntryCollection.kt
  2. 16
      cache/src/test/kotlin/org/openrs2/cache/NamedEntryCollectionTest.kt

@ -189,16 +189,20 @@ public abstract class NamedEntryCollection<T : NamedEntry>(
return createOrGetNamed(name.krHashCode()) return createOrGetNamed(name.krHashCode())
} }
public fun remove(id: Int) { public fun remove(id: Int): T? {
get(id)?.remove() val entry = get(id)
entry?.remove()
return entry
} }
public fun removeNamed(nameHash: Int) { public fun removeNamed(nameHash: Int): T? {
getNamed(nameHash)?.remove() val entry = getNamed(nameHash)
entry?.remove()
return entry
} }
public fun remove(name: String) { public fun remove(name: String): T? {
removeNamed(name.krHashCode()) return removeNamed(name.krHashCode())
} }
private fun allocateId(): Int { private fun allocateId(): Int {

@ -334,7 +334,7 @@ object NamedEntryCollectionTest {
assertEquals(entry, collection[0]) assertEquals(entry, collection[0])
assertEquals(listOf(entry), collection.toList()) assertEquals(listOf(entry), collection.toList())
collection.remove(0) assertEquals(entry, collection.remove(0))
assertEquals(0, collection.size) assertEquals(0, collection.size)
assertEquals(0, collection.capacity) assertEquals(0, collection.capacity)
@ -347,7 +347,7 @@ object NamedEntryCollectionTest {
assertFalse("hello" in collection) assertFalse("hello" in collection)
assertNull(collection["hello"]) assertNull(collection["hello"])
collection.remove(0) assertNull(collection.remove(0))
assertEquals(emptyList(), collection.toList()) assertEquals(emptyList(), collection.toList())
} }
@ -364,7 +364,7 @@ object NamedEntryCollectionTest {
assertEquals(entry, collection[0]) assertEquals(entry, collection[0])
assertEquals(listOf(entry), collection.toList()) assertEquals(listOf(entry), collection.toList())
collection.remove("hello") assertEquals(entry, collection.remove("hello"))
assertEquals(0, collection.size) assertEquals(0, collection.size)
assertEquals(0, collection.capacity) assertEquals(0, collection.capacity)
@ -377,7 +377,7 @@ object NamedEntryCollectionTest {
assertFalse("world" in collection) assertFalse("world" in collection)
assertNull(collection["world"]) assertNull(collection["world"])
collection.remove("hello") assertNull(collection.remove("hello"))
assertEquals(emptyList(), collection.toList()) assertEquals(emptyList(), collection.toList())
} }
@ -435,7 +435,7 @@ object NamedEntryCollectionTest {
assertEquals(entry1, collection[1]) assertEquals(entry1, collection[1])
assertEquals(listOf(entry0, entry1), collection.toList()) assertEquals(listOf(entry0, entry1), collection.toList())
collection.remove(0) assertEquals(entry0, collection.remove(0))
assertEquals(1, collection.size) assertEquals(1, collection.size)
assertEquals(2, collection.capacity) assertEquals(2, collection.capacity)
@ -449,7 +449,7 @@ object NamedEntryCollectionTest {
assertFalse("world" in collection) assertFalse("world" in collection)
assertNull(collection["world"]) assertNull(collection["world"])
collection.remove(0) assertNull(collection.remove(0))
assertEquals(listOf(entry1), collection.toList()) assertEquals(listOf(entry1), collection.toList())
} }
@ -476,7 +476,7 @@ object NamedEntryCollectionTest {
assertEquals(entry2, collection["abc"]) assertEquals(entry2, collection["abc"])
assertEquals(listOf(entry0, entry1, entry2), collection.toList()) assertEquals(listOf(entry0, entry1, entry2), collection.toList())
collection.remove("hello") assertEquals(entry0, collection.remove("hello"))
assertEquals(2, collection.size) assertEquals(2, collection.size)
assertEquals(3, collection.capacity) assertEquals(3, collection.capacity)
@ -491,7 +491,7 @@ object NamedEntryCollectionTest {
assertFalse("!" in collection) assertFalse("!" in collection)
assertNull(collection["!"]) assertNull(collection["!"])
collection.remove("hello") assertNull(collection.remove("hello"))
assertEquals(listOf(entry1, entry2), collection.toList()) assertEquals(listOf(entry1, entry2), collection.toList())
} }

Loading…
Cancel
Save