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.Persistable
import nl.astraeus.nl.astraeus.persistence.currentTransaction import nl.astraeus.nl.astraeus.persistence.currentTransaction
import java.io.Serializable
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
inline fun <reified T : Persistable> reference(
) = Reference(T::class.java)
class Reference<S : Persistable>( class Reference<S : Persistable>(
val cls: Class<S>, val cls: Class<S>
val setter: (Long?) -> Unit, ) : Serializable {
val getter: () -> Long?, var id: Long? = null
) {
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? { 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?) { operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
@@ -19,7 +23,7 @@ class Reference<S : Persistable>(
// todo: only store if not already stored? // todo: only store if not already stored?
currentTransaction()?.store(value) currentTransaction()?.store(value)
} }
setter(value?.id) id = value?.id
} }
companion object { 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.Persistable
import nl.astraeus.nl.astraeus.persistence.Persistent import nl.astraeus.nl.astraeus.persistence.Persistent
import nl.astraeus.nl.astraeus.persistence.TransactionLog 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 nl.astraeus.nl.astraeus.persistence.reference.referenceCollection
import java.io.File import java.io.File
import kotlin.test.Test import kotlin.test.Test
@@ -31,23 +31,15 @@ class TestReferences {
override var version: Long = 0, override var version: Long = 0,
val name: String, val name: String,
val age: Int, val age: Int,
private var companyId: Long? = null
) : Persistable, Cloneable { ) : Persistable, Cloneable {
var company: Company? var company: Company? by reference()
get() = currentTransaction()?.find(Company::class, companyId ?: 0L)
set(value) {
if (value != null) {
currentTransaction()?.store(value)
}
companyId = value?.id
}
companion object { companion object {
private const val serialVersionUID: Long = 1L private const val serialVersionUID: Long = 1L
} }
override fun toString(): String { 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 person.company = company
store(person) store(person)
company.persons.add(person) company.persons.add(person)
} else {
for (person in company.persons) {
println("Found stored Person: $person")
}
} }
store(company) store(company)

View File

@@ -38,11 +38,11 @@ class TestThreaded {
val age: Int, val age: Int,
var companyId: Long? = null var companyId: Long? = null
) : Persistable, Cloneable { ) : Persistable, Cloneable {
var company: Company? by Reference( /* var company: Company? by Reference(
Company::class.java, Company::class.java,
{ id -> companyId = id }, { id -> companyId = id },
{ companyId } { companyId }
) )*/
companion object { companion object {
private const val serialVersionUID: Long = 1L private const val serialVersionUID: Long = 1L
@@ -113,7 +113,7 @@ class TestThreaded {
name = names[random.nextInt(names.size)], name = names[random.nextInt(names.size)],
age = random.nextInt(0, 100), age = random.nextInt(0, 100),
) )
person.company = company //person.company = company
store(person) store(person)
} }