From d78c382ea7c8d23c3e3cfc359561efc1755e4ec5 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 13 Dec 2020 14:48:15 +0000 Subject: [PATCH] Add in operator support to NamedEntryCollection Signed-off-by: Graham --- .../org/openrs2/cache/NamedEntryCollection.kt | 4 +- .../kotlin/org/openrs2/cache/Js5IndexTest.kt | 12 ++-- .../openrs2/cache/NamedEntryCollectionTest.kt | 58 +++++++++---------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/cache/src/main/kotlin/org/openrs2/cache/NamedEntryCollection.kt b/cache/src/main/kotlin/org/openrs2/cache/NamedEntryCollection.kt index 9f669f0c..f9d268b0 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/NamedEntryCollection.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/NamedEntryCollection.kt @@ -78,7 +78,7 @@ public abstract class NamedEntryCollection( return entries.lastIntKey() + 1 } - public fun contains(id: Int): Boolean { + public operator fun contains(id: Int): Boolean { require(id >= 0) val entry = singleEntry @@ -101,7 +101,7 @@ public abstract class NamedEntryCollection( return nameHashTable?.containsKey(nameHash) ?: false } - public fun contains(name: String): Boolean { + public operator fun contains(name: String): Boolean { return containsNamed(name.krHashCode()) } diff --git a/cache/src/test/kotlin/org/openrs2/cache/Js5IndexTest.kt b/cache/src/test/kotlin/org/openrs2/cache/Js5IndexTest.kt index 2a9316f9..4bc17ba9 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/Js5IndexTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/Js5IndexTest.kt @@ -367,19 +367,19 @@ object Js5IndexTest { val group = index.createOrGet(0) assertEquals(1, index.size) assertEquals(1, index.capacity) - assertTrue(index.contains(0)) + assertTrue(0 in index) assertEquals(group, index[0]) group.remove() assertEquals(0, index.size) assertEquals(0, index.capacity) - assertFalse(index.contains(0)) + assertFalse(0 in index) assertNull(index[0]) group.remove() assertEquals(0, index.size) assertEquals(0, index.capacity) - assertFalse(index.contains(0)) + assertFalse(0 in index) assertNull(index[0]) } @@ -391,19 +391,19 @@ object Js5IndexTest { val file = group.createOrGet(0) assertEquals(1, group.size) assertEquals(1, group.capacity) - assertTrue(group.contains(0)) + assertTrue(0 in group) assertEquals(file, group[0]) file.remove() assertEquals(0, group.size) assertEquals(0, group.capacity) - assertFalse(group.contains(0)) + assertFalse(0 in group) assertNull(group[0]) file.remove() assertEquals(0, group.size) assertEquals(0, group.capacity) - assertFalse(group.contains(0)) + assertFalse(0 in group) assertNull(group[0]) } diff --git a/cache/src/test/kotlin/org/openrs2/cache/NamedEntryCollectionTest.kt b/cache/src/test/kotlin/org/openrs2/cache/NamedEntryCollectionTest.kt index deafcf43..1dd1ee3f 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/NamedEntryCollectionTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/NamedEntryCollectionTest.kt @@ -34,7 +34,7 @@ object NamedEntryCollectionTest { val collection = TestCollection() assertThrows { - collection.contains(-1) + -1 in collection } assertThrows { @@ -90,8 +90,8 @@ object NamedEntryCollectionTest { assertEquals(1, collection.size) assertEquals(1, collection.capacity) - assertTrue(collection.contains(0)) - assertFalse(collection.contains(1)) + assertTrue(0 in collection) + assertFalse(1 in collection) assertEquals(entry, collection[0]) assertNull(collection[1]) @@ -120,9 +120,9 @@ object NamedEntryCollectionTest { assertEquals(2, collection.size) assertEquals(2, collection.capacity) - assertTrue(collection.contains(0)) - assertTrue(collection.contains(1)) - assertFalse(collection.contains(2)) + assertTrue(0 in collection) + assertTrue(1 in collection) + assertFalse(2 in collection) assertEquals(entry0, collection[0]) assertEquals(entry1, collection[1]) @@ -147,8 +147,8 @@ object NamedEntryCollectionTest { assertEquals(1, collection.size) assertEquals(1, collection.capacity) - assertTrue(collection.contains("hello")) - assertFalse(collection.contains("world")) + assertTrue("hello" in collection) + assertFalse("world" in collection) assertEquals(entry, collection["hello"]) assertNull(collection["world"]) @@ -177,9 +177,9 @@ object NamedEntryCollectionTest { assertEquals(2, collection.size) assertEquals(2, collection.capacity) - assertTrue(collection.contains("hello")) - assertTrue(collection.contains("world")) - assertFalse(collection.contains("!")) + assertTrue("hello" in collection) + assertTrue("world" in collection) + assertFalse("!" in collection) assertEquals(entry0, collection["hello"]) assertEquals(entry1, collection["world"]) @@ -196,16 +196,16 @@ object NamedEntryCollectionTest { assertEquals(0, entry.id) assertEquals("hello".krHashCode(), entry.nameHash) - assertTrue(collection.contains("hello")) - assertFalse(collection.contains("world")) + assertTrue("hello" in collection) + assertFalse("world" in collection) assertEquals(entry, collection["hello"]) assertNull(collection["world"]) entry.setName("world") - assertFalse(collection.contains("hello")) - assertTrue(collection.contains("world")) + assertFalse("hello" in collection) + assertTrue("world" in collection) assertNull(collection["hello"]) assertEquals(entry, collection["world"]) @@ -219,13 +219,13 @@ object NamedEntryCollectionTest { assertEquals(0, entry.id) assertEquals(-1, entry.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) entry.setName("hello") assertEquals("hello".krHashCode(), entry.nameHash) - assertTrue(collection.contains("hello")) + assertTrue("hello" in collection) assertEquals(entry, collection["hello"]) } @@ -237,13 +237,13 @@ object NamedEntryCollectionTest { assertEquals(0, entry.id) assertEquals("hello".krHashCode(), entry.nameHash) - assertTrue(collection.contains("hello")) + assertTrue("hello" in collection) assertEquals(entry, collection["hello"]) entry.nameHash = -1 assertEquals(-1, entry.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) } @@ -259,13 +259,13 @@ object NamedEntryCollectionTest { assertEquals(1, entry1.id) assertEquals(-1, entry1.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) entry0.setName("hello") assertEquals("hello".krHashCode(), entry0.nameHash) - assertTrue(collection.contains("hello")) + assertTrue("hello" in collection) assertEquals(entry0, collection["hello"]) } @@ -281,13 +281,13 @@ object NamedEntryCollectionTest { assertEquals(1, entry1.id) assertEquals(-1, entry1.nameHash) - assertTrue(collection.contains("hello")) + assertTrue("hello" in collection) assertEquals(entry0, collection["hello"]) entry0.nameHash = -1 assertEquals(-1, entry0.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) } @@ -314,7 +314,7 @@ object NamedEntryCollectionTest { entry.setName("hello") assertEquals("hello".krHashCode(), entry.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) entry.remove() @@ -344,7 +344,7 @@ object NamedEntryCollectionTest { entry.setName("hello") assertEquals("hello".krHashCode(), entry.nameHash) - assertFalse(collection.contains("hello")) + assertFalse("hello" in collection) assertNull(collection["hello"]) collection.remove(0) @@ -374,7 +374,7 @@ object NamedEntryCollectionTest { entry.setName("world") assertEquals("world".krHashCode(), entry.nameHash) - assertFalse(collection.contains("world")) + assertFalse("world" in collection) assertNull(collection["world"]) collection.remove("hello") @@ -410,7 +410,7 @@ object NamedEntryCollectionTest { entry0.setName("world") assertEquals("world".krHashCode(), entry0.nameHash) - assertFalse(collection.contains("world")) + assertFalse("world" in collection) assertNull(collection["world"]) entry0.remove() @@ -446,7 +446,7 @@ object NamedEntryCollectionTest { entry0.setName("world") assertEquals("world".krHashCode(), entry0.nameHash) - assertFalse(collection.contains("world")) + assertFalse("world" in collection) assertNull(collection["world"]) collection.remove(0) @@ -488,7 +488,7 @@ object NamedEntryCollectionTest { entry0.setName("!") assertEquals("!".krHashCode(), entry0.nameHash) - assertFalse(collection.contains("!")) + assertFalse("!" in collection) assertNull(collection["!"]) collection.remove("hello")