diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-03-17 22:26:15 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-03-25 10:48:58 +0100 |
| commit | b1cf9b2bd9559328c3c9d26e73123e67d2bfea05 (patch) | |
| tree | 62de7a5a2b386e1467171578742dc983bd9f7c19 /opendc/opendc-core | |
| parent | 6b10881f123f5e6a8e7bce1045d02eba5e48c3a2 (diff) | |
refactor: Rework monitor interfaces
Diffstat (limited to 'opendc/opendc-core')
2 files changed, 23 insertions, 16 deletions
diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistry.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistry.kt index a036a705..75aa778f 100644 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistry.kt +++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistry.kt @@ -25,10 +25,15 @@ package com.atlarge.opendc.core.services /** - * A service registry for a datacenter zone. + * An immutable service registry interface. */ public interface ServiceRegistry { /** + * The keys in this registry. + */ + public val keys: Collection<ServiceKey<*>> + + /** * Determine if this map contains the service with the specified [ServiceKey]. * * @param key The key of the service to check for. @@ -41,12 +46,18 @@ public interface ServiceRegistry { * * @param key The key of the service to obtain. * @return The references to the service. - * @throws IllegalArgumentException if the key does not exists in the map. + * @throws IllegalArgumentException if the key does not exist in the map. */ public operator fun <T : Any> get(key: ServiceKey<T>): T /** - * Register the specified [ServiceKey] in this registry. + * Return the result of associating the specified [service] with the given [key] in this registry. */ - public operator fun <T : Any> set(key: ServiceKey<T>, service: T): ServiceRegistry + public fun <T : Any> put(key: ServiceKey<T>, service: T): ServiceRegistry } + +/** + * Construct an empty [ServiceRegistry]. + */ +@Suppress("FunctionName") +public fun ServiceRegistry(): ServiceRegistry = ServiceRegistryImpl(emptyMap()) diff --git a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistryImpl.kt b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistryImpl.kt index e3fa171d..0686ebaf 100644 --- a/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistryImpl.kt +++ b/opendc/opendc-core/src/main/kotlin/com/atlarge/opendc/core/services/ServiceRegistryImpl.kt @@ -27,22 +27,18 @@ package com.atlarge.opendc.core.services /** * Default implementation of the [ServiceRegistry] interface. */ -public class ServiceRegistryImpl : ServiceRegistry { - /** - * The map containing the registered services. - */ - private val services: MutableMap<ServiceKey<*>, Any> = mutableMapOf() +internal class ServiceRegistryImpl(private val map: Map<ServiceKey<*>, Any>) : ServiceRegistry { + override val keys: Collection<ServiceKey<*>> + get() = map.keys - override fun <T : Any> set(key: ServiceKey<T>, service: T) { - services[key] = service - } - - override fun contains(key: ServiceKey<*>): Boolean = key in services + override fun contains(key: ServiceKey<*>): Boolean = key in map override fun <T : Any> get(key: ServiceKey<T>): T { @Suppress("UNCHECKED_CAST") - return services[key] as T + return map[key] as T } - override fun toString(): String = services.toString() + override fun <T : Any> put(key: ServiceKey<T>, service: T): ServiceRegistry = ServiceRegistryImpl(map.plus(key to service)) + + override fun toString(): String = map.toString() } |
