Object reference delegate

This commit is contained in:
2024-05-06 19:48:28 +02:00
parent 7a8d1cac62
commit 5fe320581b
3 changed files with 20 additions and 20 deletions

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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)
}