io.javalin.config.AppDataManager.kt Maven / Gradle / Ivy
package io.javalin.config
data class Key(val id: String)
class KeyAlreadyExistsException(key: Key<*>) : IllegalStateException("Key '$key' already exists")
class NoValueForKeyException(key: Key<*>) : IllegalStateException("No value for key '$key'")
class AppDataManager {
private val data: MutableMap, Any?> = mutableMapOf()
fun register(key: Key, value: T) {
if (data.containsKey(key)) {
throw KeyAlreadyExistsException(key)
}
data[key] = value
}
fun registerIfAbsent(key: Key, value: T) {
registerResolverIfAbsent(key, value)
}
fun registerResolverIfAbsent(key: Key, value: T) {
data.putIfAbsent(key, value)
}
fun get(key: Key): T {
return data[key] as T? ?: throw NoValueForKeyException(key)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy