Object reference delegate
This commit is contained in:
@@ -2,16 +2,20 @@ package nl.astraeus.nl.astraeus.persistence.reference
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
import java.io.Serializable
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
inline fun <reified T : Persistable> reference(
|
||||
) = Reference(T::class.java)
|
||||
|
||||
|
||||
class Reference<S : Persistable>(
|
||||
val cls: Class<S>,
|
||||
val setter: (Long?) -> Unit,
|
||||
val getter: () -> Long?,
|
||||
) {
|
||||
val cls: Class<S>
|
||||
) : Serializable {
|
||||
var id: Long? = null
|
||||
|
||||
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? {
|
||||
return currentTransaction()?.find(cls.kotlin, (getter() ?: 0L))
|
||||
return currentTransaction()?.find(cls.kotlin, id ?: 0L)
|
||||
}
|
||||
|
||||
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
|
||||
@@ -19,7 +23,7 @@ class Reference<S : Persistable>(
|
||||
// todo: only store if not already stored?
|
||||
currentTransaction()?.store(value)
|
||||
}
|
||||
setter(value?.id)
|
||||
id = value?.id
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user