remove id in EntityI interface & remove usless EntitiesCollections
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class EntitiesCollections {
|
||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
|
||||
|
||||
fun <I, R: EntityI<I>> get(id: I, className: KClass<R>): R? {
|
||||
val collection = collections[className]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
inline fun <I, reified R: EntityI<I>> get(id: I): R? {
|
||||
return get(id, R::class)
|
||||
}
|
||||
|
||||
fun <I, R: EntityI<out I?>> set(entity: R): EntitiesCollections {
|
||||
if (collections[entity.className] == null) {
|
||||
collections[entity.className] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[entity.className]!!.set(entity as EntityI<Any?>)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,12 @@ import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/* ID */
|
||||
interface EntityI<T> {
|
||||
var id: T?
|
||||
val className: KClass<EntityI<T>>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI<T>>
|
||||
interface EntityI {
|
||||
val className: KClass<EntityI>
|
||||
@JsonIgnore() get() = this::class as KClass<EntityI>
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null): EntityI<T>
|
||||
abstract class Entity<T>(open var id: T? = null): EntityI
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()): Entity<UUID>(id)
|
||||
open class IdEntity(override var id: Int? = null): Entity<Int>(id)
|
||||
|
||||
@@ -57,62 +56,62 @@ class EntityDeletedAtImp: EntityDeletedAt {
|
||||
}
|
||||
|
||||
/* Author */
|
||||
interface EntityCreatedBy<T: EntityI<*>> {
|
||||
interface EntityCreatedBy<T: EntityI> {
|
||||
var createdBy: T?
|
||||
}
|
||||
|
||||
interface EntityUpdatedBy<T: EntityI<*>> {
|
||||
interface EntityUpdatedBy<T: EntityI> {
|
||||
var updatedBy: T?
|
||||
}
|
||||
|
||||
interface EntityDeletedBy<T: EntityI<*>> {
|
||||
interface EntityDeletedBy<T: EntityI> {
|
||||
var deletedBy: T?
|
||||
}
|
||||
|
||||
class EntityCreatedByImp<UserT: EntityI<*>>(
|
||||
class EntityCreatedByImp<UserT: EntityI>(
|
||||
override var createdBy: UserT?
|
||||
): EntityCreatedBy<UserT>
|
||||
|
||||
class EntityUpdatedByImp<UserT: EntityI<*>>(
|
||||
class EntityUpdatedByImp<UserT: EntityI>(
|
||||
override var updatedBy: UserT?
|
||||
): EntityUpdatedBy<UserT>
|
||||
|
||||
class EntityDeletedByImp<UserT: EntityI<*>>(
|
||||
class EntityDeletedByImp<UserT: EntityI>(
|
||||
override var deletedBy: UserT?
|
||||
): EntityDeletedBy<UserT>
|
||||
|
||||
/* Mixed */
|
||||
class EntityDeletedImp<UserT: EntityI<*>>(
|
||||
class EntityDeletedImp<UserT: EntityI>(
|
||||
override var deletedBy: UserT? = null
|
||||
): EntityDeletedBy<UserT>,
|
||||
EntityDeletedAt by EntityDeletedAtImp()
|
||||
|
||||
class EntityUpdatedImp<UserT: EntityI<*>>(
|
||||
class EntityUpdatedImp<UserT: EntityI>(
|
||||
override var updatedAt: DateTime? = null,
|
||||
override var updatedBy: UserT? = null
|
||||
): EntityUpdatedBy<UserT>,
|
||||
EntityUpdatedAt by EntityUpdatedAtImp()
|
||||
|
||||
class EntityCreatedImp<UserT: EntityI<*>>(
|
||||
class EntityCreatedImp<UserT: EntityI>(
|
||||
override var createdAt: DateTime? = null,
|
||||
override var createdBy: UserT? = null
|
||||
): EntityCreatedBy<UserT>,
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
|
||||
/* Published */
|
||||
interface Published<UserT: EntityI<*>> {
|
||||
interface Published<UserT: EntityI> {
|
||||
var publishedAt: DateTime?
|
||||
var publishedBy: UserT?
|
||||
}
|
||||
|
||||
class EntityPublishedImp<UserT: EntityI<*>>(
|
||||
class EntityPublishedImp<UserT: EntityI>(
|
||||
override var publishedBy: UserT?
|
||||
): Published<UserT> {
|
||||
override var publishedAt: DateTime? = null
|
||||
}
|
||||
|
||||
/* Implementation */
|
||||
abstract class EntityImp<T, UserT: EntityI<*>>(
|
||||
abstract class EntityImp<T, UserT: EntityI>(
|
||||
updatedBy: UserT?
|
||||
): Entity<T>(),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
@@ -122,7 +121,7 @@ abstract class EntityImp<T, UserT: EntityI<*>>(
|
||||
EntityUpdatedBy<UserT> by EntityUpdatedByImp(updatedBy),
|
||||
EntityDeletedBy<UserT> by EntityDeletedByImp(updatedBy)
|
||||
|
||||
abstract class UuidEntityExtended<T, UserT: EntityI<*>>(
|
||||
abstract class UuidEntityExtended<T, UserT: EntityI>(
|
||||
updatedBy: UserT?,
|
||||
publishedBy: UserT?
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user