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 {
|
||||
|
||||
@@ -3,7 +3,7 @@ package nl.astraeus.persistence
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
import nl.astraeus.nl.astraeus.persistence.reference.reference
|
||||
import nl.astraeus.nl.astraeus.persistence.reference.referenceCollection
|
||||
import java.io.File
|
||||
import kotlin.test.Test
|
||||
@@ -31,23 +31,15 @@ class TestReferences {
|
||||
override var version: Long = 0,
|
||||
val name: String,
|
||||
val age: Int,
|
||||
private var companyId: Long? = null
|
||||
) : Persistable, Cloneable {
|
||||
var company: Company?
|
||||
get() = currentTransaction()?.find(Company::class, companyId ?: 0L)
|
||||
set(value) {
|
||||
if (value != null) {
|
||||
currentTransaction()?.store(value)
|
||||
}
|
||||
companyId = value?.id
|
||||
}
|
||||
var company: Company? by reference()
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "Person(id=$id, version=$version, name='$name', age=$age, company=$companyId)"
|
||||
return "Person(id=$id, version=$version, name='$name', age=$age, company=$company)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +71,10 @@ class TestReferences {
|
||||
person.company = company
|
||||
store(person)
|
||||
company.persons.add(person)
|
||||
} else {
|
||||
for (person in company.persons) {
|
||||
println("Found stored Person: $person")
|
||||
}
|
||||
}
|
||||
|
||||
store(company)
|
||||
|
||||
@@ -38,11 +38,11 @@ class TestThreaded {
|
||||
val age: Int,
|
||||
var companyId: Long? = null
|
||||
) : Persistable, Cloneable {
|
||||
var company: Company? by Reference(
|
||||
/* var company: Company? by Reference(
|
||||
Company::class.java,
|
||||
{ id -> companyId = id },
|
||||
{ companyId }
|
||||
)
|
||||
)*/
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
@@ -113,7 +113,7 @@ class TestThreaded {
|
||||
name = names[random.nextInt(names.size)],
|
||||
age = random.nextInt(0, 100),
|
||||
)
|
||||
person.company = company
|
||||
//person.company = company
|
||||
|
||||
store(person)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user