From 801506533869a1d97f4bb208bf7dfeb7a90c8153 Mon Sep 17 00:00:00 2001 From: Almas Baimagambetov Date: Fri, 2 Sep 2022 23:09:32 +0100 Subject: [PATCH] feat: added PropertyMap::addAll(), updated forEach impl and doc, closes #1199 --- .../fxgl/core/collection/PropertyMap.kt | 24 ++++++++++++-- .../fxgl/core/collection/PropertyMapTest.kt | 33 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/fxgl-core/src/main/kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt b/fxgl-core/src/main/kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt index 77d146c1d..2cbc3a689 100644 --- a/fxgl-core/src/main/kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt +++ b/fxgl-core/src/main/kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt @@ -235,14 +235,32 @@ class PropertyMap { return keys().map { it to getValue(it) }.toMap() } - /*** - * provides functionality of Map.forEach for PropertyMap - * @param action - lambda or method reference with signature (String, Any) + /** + * Provides functionality of Map.forEach for PropertyMap. + * + * @param action - lambda or method reference with signature (String, Any), + * where Any is the unwrapped (e.g. String, int, etc., not Observable) type */ fun forEach(action: (String, Any) -> Unit) { + properties.forEach { (key, _) -> action(key, getValue(key)) } + } + + /** + * Provides functionality of Map.forEach for PropertyMap. + * + * @param action - lambda or method reference with signature (String, Any), + * where Any is an Observable type + */ + fun forEachObservable(action: (String, Any) -> Unit) { properties.forEach(action) } + fun addAll(other: PropertyMap) { + other.forEach { key, value -> + setValue(key, value) + } + } + fun toStringMap(): Map { return keys().map { it to getValue(it).toString() }.toMap() } diff --git a/fxgl-core/src/test/kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt b/fxgl-core/src/test/kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt index b8f88133e..5e3ba5e88 100644 --- a/fxgl-core/src/test/kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt +++ b/fxgl-core/src/test/kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt @@ -341,7 +341,7 @@ class PropertyMapTest { map.setValue("testElement1", 2) map.setValue("testElement2", 2) - map.forEach { _, value -> + map.forEachObservable { _, value -> run { timesCounter++ sumCounter += (value as SimpleIntegerProperty).value @@ -350,6 +350,37 @@ class PropertyMapTest { assertEquals(2, timesCounter) assertEquals(4, sumCounter) + + map.forEach { _, value -> + run { + timesCounter++ + sumCounter += value as Int + } + } + + assertEquals(4, timesCounter) + assertEquals(8, sumCounter) + } + + @Test + fun `Add all`() { + val map2 = PropertyMap() + + assertThat(map2.keys().size, `is`(0)) + + map.setValue("key1", "aaa") + map.setValue("key2", -55) + map.setValue("key3", 900.0) + map.setValue("key4", MyClass(2)) + map.setValue("key5", true) + + map2.addAll(map) + + assertThat(map2.getString("key1"), `is`("aaa")) + assertThat(map2.getInt("key2"), `is`(-55)) + assertThat(map2.getDouble("key3"), `is`(900.0)) + assertThat(map2.getObject("key4").i, `is`(2)) + assertThat(map2.getBoolean("key5"), `is`(true)) } private class MyClass(val i: Int)