From b1cf9b2bd9559328c3c9d26e73123e67d2bfea05 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 17 Mar 2020 22:26:15 +0100 Subject: refactor: Rework monitor interfaces --- .../atlarge/opendc/core/services/ServiceRegistry.kt | 19 +++++++++++++++---- .../opendc/core/services/ServiceRegistryImpl.kt | 20 ++++++++------------ 2 files changed, 23 insertions(+), 16 deletions(-) (limited to 'opendc/opendc-core/src/main') 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,9 +25,14 @@ 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> + /** * Determine if this map contains the service with the specified [ServiceKey]. * @@ -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 get(key: ServiceKey): 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 set(key: ServiceKey, service: T): ServiceRegistry + public fun put(key: ServiceKey, 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, Any> = mutableMapOf() +internal class ServiceRegistryImpl(private val map: Map, Any>) : ServiceRegistry { + override val keys: Collection> + get() = map.keys - override fun set(key: ServiceKey, 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 get(key: ServiceKey): T { @Suppress("UNCHECKED_CAST") - return services[key] as T + return map[key] as T } - override fun toString(): String = services.toString() + override fun put(key: ServiceKey, service: T): ServiceRegistry = ServiceRegistryImpl(map.plus(key to service)) + + override fun toString(): String = map.toString() } -- cgit v1.2.3