nullable, non-nullable reference

This commit is contained in:
2024-05-06 20:11:01 +02:00
parent 5fe320581b
commit 68562160f1
2 changed files with 50 additions and 21 deletions

View File

@@ -6,10 +6,33 @@ import java.io.Serializable
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
inline fun <reified T : Persistable> reference( inline fun <reified T : Persistable> reference(
) = Reference(T::class.java) initialValue:T
) = Reference(T::class.java, initialValue)
inline fun <reified T : Persistable> nullableReference(
) = NullableReference(T::class.java)
class Reference<S : Persistable>( class Reference<S : Persistable>(
val cls: Class<S>,
initialValue: S
) : Serializable {
var id: Long = initialValue.id
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S {
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
}
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S) {
currentTransaction()?.store(value)
id = value.id
}
companion object {
private const val serialVersionUID: Long = 1L
}
}
class NullableReference<S : Persistable>(
val cls: Class<S> val cls: Class<S>
) : Serializable { ) : Serializable {
var id: Long? = null var id: Long? = null
@@ -29,4 +52,4 @@ class Reference<S : Persistable>(
companion object { companion object {
private const val serialVersionUID: Long = 1L private const val serialVersionUID: Long = 1L
} }
} }

View File

@@ -5,6 +5,7 @@ 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.reference.reference 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 org.junit.jupiter.api.assertThrows
import java.io.File import java.io.File
import kotlin.test.Test import kotlin.test.Test
@@ -31,15 +32,16 @@ class TestReferences {
override var version: Long = 0, override var version: Long = 0,
val name: String, val name: String,
val age: Int, val age: Int,
company: Company
) : Persistable, Cloneable { ) : Persistable, Cloneable {
var company: Company? by reference() var company: Company by reference(company)
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=$company)" return "Person(id=$id, version=$version, name='$name', age=$age)"
} }
} }
@@ -57,31 +59,35 @@ class TestReferences {
) )
pst.transaction { pst.transaction {
val company = find(Company::class, 1L) ?: Company( val company = Company(
id = 1L, id = 1L,
name = "ACME" name = "ACME"
) )
if (company.persons.isEmpty()) { val person = Person(
val person = Person( id = 0L,
id = 0L, name = "John Doe",
name = "John Doe", age = 25,
age = 25 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)
for (person in company.persons) { for (p in company.persons) {
println("Person: $person") println("Person: $p")
} }
delete(company)
// company is gone, can't get it through person anymore
assertThrows<IllegalStateException> {
println("Company in person: ${person.company}")
}
delete(person)
} }
} }