All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.frtu.kotlin.action.management.ActionRegistry.kt Maven / Gradle / Ivy

There is a newer version: 2.0.12
Show newest version
package com.github.frtu.kotlin.action.management

import javax.annotation.PostConstruct
import org.slf4j.LoggerFactory

/**
 * Registry of all `ActionMetadata` in the classpath
 *
 * @author Frédéric TU
 * @since 2.0.8
 */
open class ActionRegistry(
    registry: List = listOf()
) {
    /** registry key MUST be Tool name since this is what Function call will use */
    protected open val registry: MutableMap = registry.associateBy { it.id }.toMutableMap()

    @PostConstruct
    fun init() {
        registry.entries.forEach { (key, tool) ->
            logger.info("Registered Tool key:[$key] with type ${tool.javaClass}")
        }
    }

    open operator fun get(key: ActionId) = registry[key]

    /** Convenience function as a getter */
    open operator fun get(key: String) = get(ActionId(key))

    open fun getAll() = registry.values
    open fun getAllNames(separator: String = " | ") = registry.keys.joinToString(separator) { it.value }

    open fun register(action: ACTION) = set(key = action.id, action = action)

    open operator fun set(key: ActionId, action: ACTION) {
        logger.debug(
            "Registering new action : key=[$key] id=[${action.id}] description=[${action.description}] " +
                    " parameterJsonSchema=[${action.parameterJsonSchema}]"
        )
        registry[key] = action
    }

    open fun split(keys: List) = ActionRegistry(getAll().filter { keys.contains(it.id.value) })

    private val logger = LoggerFactory.getLogger(this::class.java)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy