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
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
@@ -29,4 +52,4 @@ class Reference<S : Persistable>(
companion object {
private const val serialVersionUID: Long = 1L
}
}
}