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