Skip to content

Commit

Permalink
feat: added PropertyMap::addAll(), updated forEach impl and doc, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Sep 2, 2022
1 parent f504af9 commit 8015065
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Expand Up @@ -235,14 +235,32 @@ class PropertyMap {
return keys().map { it to getValue<Any>(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<String, String> {
return keys().map { it to getValue<Any>(it).toString() }.toMap()
}
Expand Down
Expand Up @@ -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
Expand All @@ -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<MyClass>("key4").i, `is`(2))
assertThat(map2.getBoolean("key5"), `is`(true))
}

private class MyClass(val i: Int)
Expand Down

0 comments on commit 8015065

Please sign in to comment.