diff options
Diffstat (limited to 'opendc/opendc-core')
3 files changed, 6 insertions, 113 deletions
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt index 4167cd20..6ba1cf0b 100644 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt +++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainer.kt @@ -27,36 +27,10 @@ package com.atlarge.opendc.core.resource /** * An immutable map containing the tags of some resource. */ -public interface TagContainer { - /** - * The keys in this container. - */ - public val keys: Collection<TagKey<*>> +typealias TagContainer = Map<String, Any> - /** - * Determine if this container contains a tag with the specified [TagKey]. - * - * @param key The key of the tag to check for. - * @return `true` if the tag is in the container, `false` otherwise. - */ - public operator fun contains(key: TagKey<*>): Boolean - - /** - * Obtain the tag with the specified [TagKey]. - * - * @param key The key of the tag to obtain. - * @return The value of the tag. - * @throws IllegalArgumentException if the tag does not exists in the container. - */ - public operator fun <T : Any> get(key: TagKey<T>): T - - /** - * Return the result of associating the specified [value] with the specified [key] in this container. - */ - public fun <T : Any> put(key: TagKey<T>, value: T): TagContainer - - /** - * Return the result of removing the specified [key] and its corresponding value from this container. - */ - public fun remove(key: TagKey<*>): TagContainer -} +/** + * Obtain the value of the tag with the specified [key] of type [T]. If the tag does not exist or the tag is of + * different type, `null` is returned. + */ +inline fun <reified T : Any> TagContainer.typed(key: String): T? = this[key] as? T diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt deleted file mode 100644 index 1acc7de4..00000000 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagContainerImpl.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.opendc.core.resource - -/** - * Default implementation of [TagContainer] interface. - * - * @property map The internal map object to hold the tags. - */ -public class TagContainerImpl private constructor(private val map: Map<TagKey<*>, Any>) : TagContainer { - /** - * Construct an empty [TagContainerImpl]. - */ - public constructor() : this(emptyMap()) - - override val keys: Collection<TagKey<*>> - get() = map.keys - - override fun contains(key: TagKey<*>): Boolean = map.contains(key) - - override fun <T : Any> get(key: TagKey<T>): T { - @Suppress("UNCHECKED_CAST") - return map.getValue(key) as T - } - - override fun <T : Any> put(key: TagKey<T>, value: T): TagContainer = TagContainerImpl(map.plus(key to value)) - - override fun remove(key: TagKey<*>): TagContainer = TagContainerImpl(map.minus(key)) -} diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt deleted file mode 100644 index 759f4461..00000000 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/resource/TagKey.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.opendc.core.resource - -/** - * Represents a typed key for a resource tag with a unique name. - */ -public data class TagKey<T : Any>(val name: String) |
