Skip to content

Commit 8137c20

Browse files
sazzerpivovarit
authored andcommitted
Delegated Properties in Kotlin (eugenp#2357)
This produces a fake Database representation - working in terms of hardcoded data, a Delegate that loads values from this fake Database, and then a class with fields delegated to the fake database.
1 parent f02d117 commit 8137c20

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.kotlin.delegates
2+
3+
val data = arrayOf<MutableMap<String, Any?>>(
4+
mutableMapOf(
5+
"id" to 1,
6+
"name" to "George",
7+
"age" to 4
8+
),
9+
mutableMapOf(
10+
"id" to 2,
11+
"name" to "Charlotte",
12+
"age" to 2
13+
)
14+
)
15+
16+
class NoRecordFoundException(id: Int) : Exception("No record found for id $id") {
17+
init {
18+
println("No record found for ID $id")
19+
}
20+
}
21+
22+
fun queryForValue(field: String, id: Int): Any {
23+
println("Loading record $id from the fake database")
24+
val value = data.firstOrNull { it["id"] == id }
25+
?.get(field) ?: throw NoRecordFoundException(id)
26+
println("Loaded value $value for field $field of record $id")
27+
return value
28+
}
29+
30+
fun update(field: String, id: Int, value: Any?) {
31+
println("Updating field $field of record $id to value $value in the fake database")
32+
data.firstOrNull { it["id"] == id }
33+
?.put(field, value)
34+
?: throw NoRecordFoundException(id)
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.kotlin.delegates
2+
3+
import kotlin.properties.ReadWriteProperty
4+
import kotlin.reflect.KProperty
5+
6+
class DatabaseDelegate<in R, T>(private val field: String, private val id: Int) : ReadWriteProperty<R, T> {
7+
override fun getValue(thisRef: R, property: KProperty<*>): T =
8+
queryForValue(field, id) as T
9+
10+
override fun setValue(thisRef: R, property: KProperty<*>, value: T) {
11+
update(field, id, value)
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.kotlin.delegates
2+
3+
class User(val id: Int) {
4+
var name: String by DatabaseDelegate("name", id)
5+
var age: Int by DatabaseDelegate("age", id)
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.kotlin.delegates
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
class DatabaseDelegatesTest {
7+
@Test
8+
fun testGetKnownFields() {
9+
val user = User(1)
10+
assertEquals("George", user.name)
11+
assertEquals(4, user.age)
12+
}
13+
14+
@Test
15+
fun testSetKnownFields() {
16+
val user = User(2)
17+
user.age = 3
18+
assertEquals(3, user.age)
19+
}
20+
21+
@Test(expected = NoRecordFoundException::class)
22+
fun testGetKnownField() {
23+
val user = User(3)
24+
user.name
25+
}
26+
}

0 commit comments

Comments
 (0)