Add Reference/Collections

This commit is contained in:
2024-05-06 19:35:03 +02:00
parent 6bf4811110
commit 7a8d1cac62
12 changed files with 360 additions and 145 deletions

View File

@@ -0,0 +1,28 @@
package nl.astraeus.nl.astraeus.persistence.reference
import nl.astraeus.nl.astraeus.persistence.Persistable
import nl.astraeus.nl.astraeus.persistence.currentTransaction
import kotlin.reflect.KProperty
class Reference<S : Persistable>(
val cls: Class<S>,
val setter: (Long?) -> Unit,
val getter: () -> Long?,
) {
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? {
return currentTransaction()?.find(cls.kotlin, (getter() ?: 0L))
}
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
if (value != null) {
// todo: only store if not already stored?
currentTransaction()?.store(value)
}
setter(value?.id)
}
companion object {
private const val serialVersionUID: Long = 1L
}
}