Add Reference/Collections
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user