Add Connection and refactoring EntityCollection
This commit is contained in:
@@ -3,11 +3,11 @@ package fr.postgresjson.entity
|
||||
import java.util.*
|
||||
|
||||
interface EntityI<T> {
|
||||
var id: T
|
||||
var id: T?
|
||||
}
|
||||
abstract class Entity<T>(override var id: T) : EntityI<T>
|
||||
abstract class UuidEntity(override var id: UUID = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
abstract class IdEntity(override var id: Int) : Entity<Int>(id)
|
||||
abstract class Entity<T>(override var id: T? = null) : EntityI<T?>
|
||||
abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID?>(id)
|
||||
abstract class IdEntity(override var id: Int? = null) : Entity<Int?>(id)
|
||||
|
||||
interface EntityVersioning<T> {
|
||||
var version: T
|
||||
|
||||
@@ -1,13 +1,36 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
class EntityCollection<T, E : EntityI<T>> {
|
||||
var collection: MutableMap<T, E> = mutableMapOf()
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun get(id: T): E? {
|
||||
return collection[id]
|
||||
class EntityCollection {
|
||||
val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
|
||||
inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
|
||||
val collection = collections[R::class]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
fun set(entity: E) {
|
||||
collection.set(entity.id, entity)
|
||||
inline fun <I, reified R : EntityI<I?>> set(entity: R) {
|
||||
if (collections[R::class] == null) {
|
||||
collections[R::class] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[R::class]!!.set(entity as EntityI<Any?>)
|
||||
}
|
||||
|
||||
class EntityCollection<T, E : EntityI<T?>> {
|
||||
private var collection: MutableMap<T, E> = mutableMapOf()
|
||||
|
||||
fun get(id: T): E? {
|
||||
return collection[id]
|
||||
}
|
||||
|
||||
fun set(entity: E) {
|
||||
val id = entity.id
|
||||
if (id !== null) {
|
||||
collection[id] = entity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user