nullable, non-nullable reference
This commit is contained in:
@@ -6,10 +6,33 @@ import java.io.Serializable
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
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>(
|
||||
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>
|
||||
) : Serializable {
|
||||
var id: Long? = null
|
||||
|
||||
@@ -5,6 +5,7 @@ import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||
import nl.astraeus.nl.astraeus.persistence.TransactionLog
|
||||
import nl.astraeus.nl.astraeus.persistence.reference.reference
|
||||
import nl.astraeus.nl.astraeus.persistence.reference.referenceCollection
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.io.File
|
||||
import kotlin.test.Test
|
||||
|
||||
@@ -31,15 +32,16 @@ class TestReferences {
|
||||
override var version: Long = 0,
|
||||
val name: String,
|
||||
val age: Int,
|
||||
company: Company
|
||||
) : Persistable, Cloneable {
|
||||
var company: Company? by reference()
|
||||
var company: Company by reference(company)
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
|
||||
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 {
|
||||
val company = find(Company::class, 1L) ?: Company(
|
||||
val company = Company(
|
||||
id = 1L,
|
||||
name = "ACME"
|
||||
)
|
||||
|
||||
if (company.persons.isEmpty()) {
|
||||
val person = Person(
|
||||
id = 0L,
|
||||
name = "John Doe",
|
||||
age = 25
|
||||
)
|
||||
person.company = company
|
||||
store(person)
|
||||
company.persons.add(person)
|
||||
} else {
|
||||
for (person in company.persons) {
|
||||
println("Found stored Person: $person")
|
||||
}
|
||||
}
|
||||
val person = Person(
|
||||
id = 0L,
|
||||
name = "John Doe",
|
||||
age = 25,
|
||||
company = company
|
||||
)
|
||||
store(person)
|
||||
|
||||
company.persons.add(person)
|
||||
|
||||
store(company)
|
||||
|
||||
for (person in company.persons) {
|
||||
println("Person: $person")
|
||||
for (p in company.persons) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user