Add Reference/Collections
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package nl.astraeus.nl.astraeus.persistence.reference
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Reference<S : Persistable>(
|
||||
val cls: Class<S>,
|
||||
val setter: (Long?) -> Unit,
|
||||
val getter: () -> Long?,
|
||||
) {
|
||||
|
||||
operator fun getValue(thisRef: Persistable, property: KProperty<*>): S? {
|
||||
return currentTransaction()?.find(cls.kotlin, (getter() ?: 0L))
|
||||
}
|
||||
|
||||
operator fun setValue(thisRef: Persistable, property: KProperty<*>, value: S?) {
|
||||
if (value != null) {
|
||||
// todo: only store if not already stored?
|
||||
currentTransaction()?.store(value)
|
||||
}
|
||||
setter(value?.id)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package nl.astraeus.nl.astraeus.persistence.reference
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
import java.io.Serializable
|
||||
|
||||
inline fun <reified T : Persistable> referenceCollection(
|
||||
ids: MutableCollection<Long> = mutableListOf()
|
||||
) = ReferenceCollection(T::class.java, ids)
|
||||
|
||||
open class ReferenceCollection<T : Persistable>(
|
||||
val cls: Class<T>,
|
||||
val ids: MutableCollection<Long> = mutableListOf()
|
||||
) : MutableCollection<T>, Serializable {
|
||||
|
||||
protected fun checkElementIsPersisted(element: T) {
|
||||
if (currentTransaction()?.find(cls.kotlin, element.id) == null) {
|
||||
currentTransaction()?.store(element)
|
||||
}
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() {
|
||||
return ids.size
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
ids.clear()
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean = ids.isEmpty()
|
||||
|
||||
override fun iterator(): MutableIterator<T> {
|
||||
return object : MutableIterator<T> {
|
||||
private var idsIterator = ids.iterator()
|
||||
|
||||
override fun hasNext(): Boolean = idsIterator.hasNext()
|
||||
|
||||
override fun next(): T = idsIterator.next().let {
|
||||
currentTransaction()?.find(cls.kotlin, it)
|
||||
} ?: throw IllegalStateException("Reference not found")
|
||||
|
||||
override fun remove() = idsIterator.remove()
|
||||
}
|
||||
}
|
||||
|
||||
override fun retainAll(elements: Collection<T>): Boolean {
|
||||
return ids.retainAll(elements.map { it.id }.toSet())
|
||||
}
|
||||
|
||||
override fun removeAll(elements: Collection<T>): Boolean {
|
||||
return ids.removeAll(elements.map { it.id }.toSet())
|
||||
}
|
||||
|
||||
override fun remove(element: T): Boolean {
|
||||
return ids.remove(element.id)
|
||||
}
|
||||
|
||||
override fun containsAll(elements: Collection<T>): Boolean {
|
||||
return ids.containsAll(elements.map { it.id })
|
||||
}
|
||||
|
||||
override fun contains(element: T): Boolean {
|
||||
return ids.contains(element.id)
|
||||
}
|
||||
|
||||
override fun addAll(elements: Collection<T>): Boolean {
|
||||
for (element in elements) {
|
||||
checkElementIsPersisted(element)
|
||||
}
|
||||
return ids.addAll(elements.map { it.id })
|
||||
}
|
||||
|
||||
override fun add(element: T): Boolean {
|
||||
checkElementIsPersisted(element)
|
||||
return ids.add(element.id)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package nl.astraeus.nl.astraeus.persistence.reference
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
import java.io.Serializable
|
||||
|
||||
inline fun <reified T : Persistable> referenceList(
|
||||
ids: MutableList<Long> = mutableListOf()
|
||||
) = ReferenceList(T::class.java, ids)
|
||||
|
||||
class ReferenceList<T : Persistable>(
|
||||
cls: Class<T>,
|
||||
val idsList: MutableList<Long> = mutableListOf()
|
||||
) : ReferenceCollection<T>(cls, idsList), MutableList<T>, Serializable {
|
||||
|
||||
override fun add(index: Int, element: T) {
|
||||
checkElementIsPersisted(element)
|
||||
idsList.add(index, element.id)
|
||||
}
|
||||
|
||||
override fun addAll(index: Int, elements: Collection<T>): Boolean {
|
||||
for(element in elements) {
|
||||
checkElementIsPersisted(element)
|
||||
}
|
||||
return idsList.addAll(index, elements.map { it.id })
|
||||
}
|
||||
|
||||
override fun get(index: Int): T = currentTransaction()?.find(cls.kotlin, idsList[index]) ?: throw IllegalStateException("Reference not found")
|
||||
override fun indexOf(element: T): Int {
|
||||
return idsList.indexOf(element.id)
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean = ids.isEmpty()
|
||||
override fun lastIndexOf(element: T): Int {
|
||||
return idsList.lastIndexOf(element.id)
|
||||
}
|
||||
|
||||
override fun listIterator(): MutableListIterator<T> {
|
||||
return ReferenceListIterator(cls, idsList)
|
||||
}
|
||||
|
||||
override fun listIterator(index: Int): MutableListIterator<T> {
|
||||
return ReferenceListIterator(cls, idsList, index)
|
||||
}
|
||||
|
||||
override fun removeAt(index: Int): T {
|
||||
val id = idsList.removeAt(index)
|
||||
|
||||
return currentTransaction()?.find(cls.kotlin, id) ?: throw IllegalStateException("Reference not found")
|
||||
}
|
||||
|
||||
override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> {
|
||||
return ReferenceList(cls, idsList.subList(fromIndex, toIndex))
|
||||
}
|
||||
|
||||
override fun set(index: Int, element: T): T {
|
||||
checkElementIsPersisted(element)
|
||||
idsList[index] = element.id
|
||||
return element
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package nl.astraeus.nl.astraeus.persistence.reference
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.currentTransaction
|
||||
|
||||
class ReferenceListIterator<T : Persistable>(
|
||||
private val cls: Class<T>,
|
||||
idsList: MutableList<Long>,
|
||||
index: Int = 0
|
||||
) : MutableListIterator<T> {
|
||||
private val idsIterator = idsList.listIterator(index)
|
||||
|
||||
private fun checkElementIsPersisted(element: T) {
|
||||
if (currentTransaction()?.find(cls.kotlin, element.id) == null) {
|
||||
currentTransaction()?.store(element)
|
||||
}
|
||||
}
|
||||
|
||||
override fun add(element: T) {
|
||||
checkElementIsPersisted(element)
|
||||
idsIterator.add(element.id)
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean = idsIterator.hasNext()
|
||||
override fun hasPrevious(): Boolean = idsIterator.hasPrevious()
|
||||
|
||||
override fun next(): T = idsIterator.next().let {
|
||||
currentTransaction()?.find(cls.kotlin, it)
|
||||
} ?: throw IllegalStateException("Reference not found")
|
||||
|
||||
override fun nextIndex(): Int = idsIterator.nextIndex()
|
||||
|
||||
override fun previous(): T = idsIterator.previous().let {
|
||||
currentTransaction()?.find(cls.kotlin, it)
|
||||
} ?: throw IllegalStateException("Reference not found")
|
||||
|
||||
override fun previousIndex(): Int = idsIterator.previousIndex()
|
||||
|
||||
override fun remove() {
|
||||
idsIterator.remove()
|
||||
}
|
||||
|
||||
override fun set(element: T) {
|
||||
checkElementIsPersisted(element)
|
||||
idsIterator.set(element.id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user