From 1b20694f9e2f90f3d1d80ba35406a12adb434c90 Mon Sep 17 00:00:00 2001 From: BeckFW Date: Mon, 12 Feb 2024 12:48:40 +0000 Subject: [PATCH 1/4] feat: Add SerializableType to PropertyMap, closes #1250 --- .../fxgl/core/collection/PropertyMap.kt | 19 +++++++++++- .../fxgl/core/collection/PropertyMapTest.kt | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) 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 452fb31c6..621c8c254 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 @@ -9,6 +9,8 @@ package com.almasb.fxgl.core.collection import javafx.beans.property.* import javafx.beans.value.ChangeListener import javafx.beans.value.ObservableValue +import com.almasb.fxgl.core.serialization.SerializableType +import com.almasb.fxgl.core.serialization.Bundle import java.util.* @@ -23,10 +25,11 @@ import java.util.* * SimpleObjectProperty. * * Null values are not allowed. + * Object Properties are not supported for Serialization. * * @author Almas Baimagambetov (almaslvl@gmail.com) */ -class PropertyMap { +class PropertyMap : SerializableType { companion object { @JvmStatic fun fromStringMap(map: Map): PropertyMap { @@ -309,6 +312,20 @@ class PropertyMap { } } + override fun write(bundle: Bundle) { + // Convert to string map + this.toStringMap().forEach { key, value -> + // write to bundle + bundle.put(key, value); + } + } + + override fun read(bundle: Bundle) { + bundle.data.forEach { (key) -> + this.setValue(key, toValue(bundle.get(key))); + } + } + override fun toString(): String { return properties.toMap().toString() } 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 8a3c544c8..1b68a5499 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 @@ -7,6 +7,8 @@ package com.almasb.fxgl.core.collection import com.almasb.fxgl.core.math.Vec2 +import com.almasb.fxgl.core.serialization.Bundle +import javafx.beans.property.Property import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.StringProperty import org.hamcrest.MatcherAssert.assertThat @@ -412,6 +414,33 @@ class PropertyMapTest { assertThat(map2.getBoolean("key5"), `is`(true)) } + @Test + fun `Serialize Property Map`() { + // Set test values + map.setValue("key1", "ABC") + map.setValue("key2", 100) + map.setValue("key3", 10.0) + map.setValue("key4", true) + + // Create a bundle + val testBundle = Bundle("propertyMap") + + // Write to bundle + map.write(testBundle) + + // Create a new PropertyMap to test + val map2 = PropertyMap() + + // Read from bundle + map2.read(testBundle) + + // Check test values + assertThat(map2.getString("key1"), `is`("ABC")) + assertThat(map2.getInt("key2"), `is`(100)) + assertThat(map2.getDouble("key3"), `is`(10.0)) + assertThat(map2.getBoolean("key4"), `is`(true)) + } + private class MyClass(val i: Int) From df459c0006a5f565f82fe8fd8c57a7af607b59e4 Mon Sep 17 00:00:00 2001 From: BeckFW Date: Mon, 12 Feb 2024 12:55:19 +0000 Subject: [PATCH 2/4] refactor: Remove unused import --- .../kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt | 1 - 1 file changed, 1 deletion(-) 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 1b68a5499..e958cd606 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 @@ -8,7 +8,6 @@ package com.almasb.fxgl.core.collection import com.almasb.fxgl.core.math.Vec2 import com.almasb.fxgl.core.serialization.Bundle -import javafx.beans.property.Property import javafx.beans.property.SimpleIntegerProperty import javafx.beans.property.StringProperty import org.hamcrest.MatcherAssert.assertThat From 8448c6041ffbc72b925adbe8e531662a76aaed2d Mon Sep 17 00:00:00 2001 From: BeckFW Date: Tue, 20 Feb 2024 16:06:48 +0000 Subject: [PATCH 3/4] fix: Use (key,value) in read() to avoid extra bundle lookup --- .../kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 621c8c254..f9779943b 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 @@ -321,8 +321,8 @@ class PropertyMap : SerializableType { } override fun read(bundle: Bundle) { - bundle.data.forEach { (key) -> - this.setValue(key, toValue(bundle.get(key))); + bundle.data.forEach { (key, value) -> + this.setValue(key, toValue(value.toString())) } } From 20159c2c3ea1a2d7040b89bf2f0231fa0831ecb7 Mon Sep 17 00:00:00 2001 From: BeckFW Date: Tue, 20 Feb 2024 16:11:36 +0000 Subject: [PATCH 4/4] refactor: Use Kotlin forEach in write() and remove semicolons --- .../kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 f9779943b..6a9118912 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 @@ -314,9 +314,9 @@ class PropertyMap : SerializableType { override fun write(bundle: Bundle) { // Convert to string map - this.toStringMap().forEach { key, value -> + this.toStringMap().forEach { (key, value) -> // write to bundle - bundle.put(key, value); + bundle.put(key, value) } }