From fa0e81b3abfcdb8d11c72ac00b595be90a2cfb9e Mon Sep 17 00:00:00 2001 From: Ralph Gasser Date: Thu, 28 Mar 2024 10:15:49 +0100 Subject: [PATCH] Adds a hasAttribute() method to Retrievable. Signed-off-by: Ralph Gasser --- .../model/retrievable/AbstractRetrievable.kt | 55 ++++++++++++++++--- .../core/model/retrievable/Retrievable.kt | 41 +++++++++++--- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/AbstractRetrievable.kt b/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/AbstractRetrievable.kt index 9a175e96..bff35bc7 100644 --- a/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/AbstractRetrievable.kt +++ b/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/AbstractRetrievable.kt @@ -4,6 +4,13 @@ import org.vitrivr.engine.core.model.retrievable.attributes.MergingRetrievableAt import org.vitrivr.engine.core.model.retrievable.attributes.RetrievableAttribute import java.util.* +/** + * An abstract implementation of a [Retrievable] implementing basic logic to manage [RetrievableAttribute]s. + * + * @author Luca Rossetto + * @author Ralph Gasser + * @version 1.1.0 + */ abstract class AbstractRetrievable(override val id: UUID, override val type: String?, override val transient: Boolean) : Retrievable { constructor(retrievable: Retrievable) : this(retrievable.id, retrievable.type, retrievable.transient) { @@ -11,18 +18,16 @@ abstract class AbstractRetrievable(override val id: UUID, override val type: Str } private val attributeSet = mutableSetOf() + + /** [Collection] of [RetrievableAttribute]s held by this [AbstractRetrievable]. */ override val attributes: Collection get() = this.attributeSet - override fun filteredAttributes(c: Class): Collection = this.attributeSet.filterIsInstance(c) - - inline fun filteredAttributes(): Collection = filteredAttributes(T::class.java) - - @Suppress("UNCHECKED_CAST") - override fun filteredAttribute(c: Class): T? = attributeSet.firstOrNull { c.isInstance(it) } as? T - - inline fun filteredAttribute(): T? = filteredAttribute(T::class.java) - + /** + * Adds a [RetrievableAttribute] to this [AbstractRetrievable]. + * + * @param attribute The [RetrievableAttribute] to add. + */ override fun addAttribute(attribute: RetrievableAttribute) { if (this.attributeSet.contains(attribute)) { return @@ -41,8 +46,40 @@ abstract class AbstractRetrievable(override val id: UUID, override val type: Str } } + /** + * Removes all [RetrievableAttribute]s of a certain type from this [AbstractRetrievable]. + * + * @param c The [Class] of the [RetrievableAttribute] to remove. + */ override fun removeAttributes(c: Class) { this.attributeSet.removeIf { c.isInstance(it) } } + /** + * Checks if this [Retrievable] has a [RetrievableAttribute] of the given type. + * + * @param c The [Class] of the [RetrievableAttribute] to check for. + * @return True, if [RetrievableAttribute] + */ + override fun hasAttribute(c: Class): Boolean = this.attributeSet.any { c.isInstance(it) } + + /** + * Returns all [RetrievableAttribute] of a certain type. + * + * @param c The [Class] of the [RetrievableAttribute] to return. + * @return [Collection] of [RetrievableAttribute]s. + */ + override fun filteredAttributes(c: Class): Collection = this.attributeSet.filterIsInstance(c) + + inline fun filteredAttributes(): Collection = filteredAttributes(T::class.java) + + /** + * Returns the first [RetrievableAttribute] of a certain type. + * + * @param c The [Class] of the [RetrievableAttribute] to return. + * @return [RetrievableAttribute] or null. + */ + override fun filteredAttribute(c: Class): T? = this.attributeSet.filterIsInstance(c).firstOrNull() + + inline fun filteredAttribute(): T? = filteredAttribute(T::class.java) } \ No newline at end of file diff --git a/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/Retrievable.kt b/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/Retrievable.kt index b0b64a0c..8c96cb48 100644 --- a/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/Retrievable.kt +++ b/vitrivr-engine-core/src/main/kotlin/org/vitrivr/engine/core/model/retrievable/Retrievable.kt @@ -12,7 +12,7 @@ typealias RetrievableId = UUID * * @author Luca Rossetto * @author Ralph Gasser - * @version 1.0.0 + * @version 1.1.0 */ interface Retrievable : Persistable { /** The [RetrievableId] held by this [Retrievable]. */ @@ -21,19 +21,44 @@ interface Retrievable : Persistable { /** The type of this [Retrievable]. This is basically a string that can help to keep apart different types of [Retrievable]. */ val type: String? - /** - * The attributes of this retrievable - */ + /** The attributes of this retrievable */ val attributes: Collection /** - * Returns only attributes of a certain type + * Checks if this [Retrievable] has a [RetrievableAttribute] of the given type. + * + * @param c The [Class] of the [RetrievableAttribute] to check for. + * @return True, if [RetrievableAttribute] */ - fun filteredAttributes(c: Class): Collection - - fun filteredAttribute(c: Class): T? + fun hasAttribute(c: Class): Boolean + /** + * Adds a [RetrievableAttribute] to this [Retrievable]. + * + * @param attribute The [RetrievableAttribute] to add. + */ fun addAttribute(attribute: RetrievableAttribute) + /** + * Removes all [RetrievableAttribute]s of a certain type from this [Retrievable]. + * + * @param c The [Class] of the [RetrievableAttribute] to remove. + */ fun removeAttributes(c: Class) + + /** + * Returns all [RetrievableAttribute] of a certain type. + * + * @param c The [Class] of the [RetrievableAttribute] to return. + * @return [Collection] of [RetrievableAttribute]s. + */ + fun filteredAttributes(c: Class): Collection + + /** + * Returns the first [RetrievableAttribute] of a certain type. + * + * @param c The [Class] of the [RetrievableAttribute] to return. + * @return [RetrievableAttribute] or null. + */ + fun filteredAttribute(c: Class): T? } \ No newline at end of file