Skip to content

Commit 8015065

Browse files
committed
feat: added PropertyMap::addAll(), updated forEach impl and doc, closes #1199
1 parent f504af9 commit 8015065

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

fxgl-core/src/main/kotlin/com/almasb/fxgl/core/collection/PropertyMap.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,32 @@ class PropertyMap {
235235
return keys().map { it to getValue<Any>(it) }.toMap()
236236
}
237237

238-
/***
239-
* provides functionality of Map.forEach for PropertyMap
240-
* @param action - lambda or method reference with signature (String, Any)
238+
/**
239+
* Provides functionality of Map.forEach for PropertyMap.
240+
*
241+
* @param action - lambda or method reference with signature (String, Any),
242+
* where Any is the unwrapped (e.g. String, int, etc., not Observable) type
241243
*/
242244
fun forEach(action: (String, Any) -> Unit) {
245+
properties.forEach { (key, _) -> action(key, getValue(key)) }
246+
}
247+
248+
/**
249+
* Provides functionality of Map.forEach for PropertyMap.
250+
*
251+
* @param action - lambda or method reference with signature (String, Any),
252+
* where Any is an Observable type
253+
*/
254+
fun forEachObservable(action: (String, Any) -> Unit) {
243255
properties.forEach(action)
244256
}
245257

258+
fun addAll(other: PropertyMap) {
259+
other.forEach { key, value ->
260+
setValue(key, value)
261+
}
262+
}
263+
246264
fun toStringMap(): Map<String, String> {
247265
return keys().map { it to getValue<Any>(it).toString() }.toMap()
248266
}

fxgl-core/src/test/kotlin/com/almasb/fxgl/core/collection/PropertyMapTest.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ class PropertyMapTest {
341341
map.setValue("testElement1", 2)
342342
map.setValue("testElement2", 2)
343343

344-
map.forEach { _, value ->
344+
map.forEachObservable { _, value ->
345345
run {
346346
timesCounter++
347347
sumCounter += (value as SimpleIntegerProperty).value
@@ -350,6 +350,37 @@ class PropertyMapTest {
350350

351351
assertEquals(2, timesCounter)
352352
assertEquals(4, sumCounter)
353+
354+
map.forEach { _, value ->
355+
run {
356+
timesCounter++
357+
sumCounter += value as Int
358+
}
359+
}
360+
361+
assertEquals(4, timesCounter)
362+
assertEquals(8, sumCounter)
363+
}
364+
365+
@Test
366+
fun `Add all`() {
367+
val map2 = PropertyMap()
368+
369+
assertThat(map2.keys().size, `is`(0))
370+
371+
map.setValue("key1", "aaa")
372+
map.setValue("key2", -55)
373+
map.setValue("key3", 900.0)
374+
map.setValue("key4", MyClass(2))
375+
map.setValue("key5", true)
376+
377+
map2.addAll(map)
378+
379+
assertThat(map2.getString("key1"), `is`("aaa"))
380+
assertThat(map2.getInt("key2"), `is`(-55))
381+
assertThat(map2.getDouble("key3"), `is`(900.0))
382+
assertThat(map2.getObject<MyClass>("key4").i, `is`(2))
383+
assertThat(map2.getBoolean("key5"), `is`(true))
353384
}
354385

355386
private class MyClass(val i: Int)

0 commit comments

Comments
 (0)