Skip to content

Commit

Permalink
Adds a hasAttribute() method to Retrievable.
Browse files Browse the repository at this point in the history
Signed-off-by: Ralph Gasser <[email protected]>
  • Loading branch information
ppanopticon committed Mar 28, 2024
1 parent b7e0b8b commit fa0e81b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ 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) {
retrievable.attributes.forEach { this.addAttribute(it) }
}

private val attributeSet = mutableSetOf<RetrievableAttribute>()

/** [Collection] of [RetrievableAttribute]s held by this [AbstractRetrievable]. */
override val attributes: Collection<RetrievableAttribute>
get() = this.attributeSet

override fun <T : RetrievableAttribute> filteredAttributes(c: Class<T>): Collection<T> = this.attributeSet.filterIsInstance(c)

inline fun <reified T : RetrievableAttribute> filteredAttributes(): Collection<T> = filteredAttributes(T::class.java)

@Suppress("UNCHECKED_CAST")
override fun <T : RetrievableAttribute> filteredAttribute(c: Class<T>): T? = attributeSet.firstOrNull { c.isInstance(it) } as? T

inline fun <reified T : RetrievableAttribute> 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
Expand All @@ -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 <T : RetrievableAttribute> removeAttributes(c: Class<T>) {
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 <T : RetrievableAttribute> hasAttribute(c: Class<T>): 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 <T : RetrievableAttribute> filteredAttributes(c: Class<T>): Collection<T> = this.attributeSet.filterIsInstance(c)

inline fun <reified T : RetrievableAttribute> filteredAttributes(): Collection<T> = 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 <T : RetrievableAttribute> filteredAttribute(c: Class<T>): T? = this.attributeSet.filterIsInstance(c).firstOrNull()

inline fun <reified T : RetrievableAttribute> filteredAttribute(): T? = filteredAttribute(T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]. */
Expand All @@ -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<RetrievableAttribute>

/**
* 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 <T : RetrievableAttribute> filteredAttributes(c: Class<T>): Collection<T>

fun <T : RetrievableAttribute> filteredAttribute(c: Class<T>): T?
fun <T : RetrievableAttribute> hasAttribute(c: Class<T>): 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 <T : RetrievableAttribute> removeAttributes(c: Class<T>)

/**
* Returns all [RetrievableAttribute] of a certain type.
*
* @param c The [Class] of the [RetrievableAttribute] to return.
* @return [Collection] of [RetrievableAttribute]s.
*/
fun <T : RetrievableAttribute> filteredAttributes(c: Class<T>): Collection<T>

/**
* Returns the first [RetrievableAttribute] of a certain type.
*
* @param c The [Class] of the [RetrievableAttribute] to return.
* @return [RetrievableAttribute] or null.
*/
fun <T : RetrievableAttribute> filteredAttribute(c: Class<T>): T?
}

0 comments on commit fa0e81b

Please sign in to comment.