
commonMain.io.ktor.util.Attributes.kt Maven / Gradle / Ivy
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.util
/**
* Specifies a key for an attribute in [Attributes]
* @param T is type of the value stored in the attribute
* @param name is a name of the attribute for diagnostic purposes
*/
class AttributeKey(val name: String) {
override fun toString(): String = if (name.isEmpty())
super.toString()
else
"AttributeKey: $name"
}
/**
* Create attributes instance suitable for the particular platform
*/
expect fun Attributes(concurrent: Boolean = false): Attributes
/**
* Map of attributes accessible by [AttributeKey] in a typed manner
*/
interface Attributes {
/**
* Gets a value of the attribute for the specified [key], or throws an exception if an attribute doesn't exist
*/
operator fun get(key: AttributeKey): T =
getOrNull(key) ?: throw IllegalStateException("No instance for key $key")
/**
* Gets a value of the attribute for the specified [key], or return `null` if an attribute doesn't exist
*/
fun getOrNull(key: AttributeKey): T?
/**
* Checks if an attribute with the specified [key] exists
*/
operator fun contains(key: AttributeKey<*>): Boolean
/**
* Creates or changes an attribute with the specified [key] using [value]
*/
fun put(key: AttributeKey, value: T)
/**
* Removes an attribute with the specified [key]
*/
fun remove(key: AttributeKey)
/**
* Removes an attribute with the specified [key] and returns its current value, throws an exception if an attribute doesn't exist
*/
@KtorExperimentalAPI
fun take(key: AttributeKey): T = get(key).also { remove(key) }
/**
* Removes an attribute with the specified [key] and returns its current value, returns `null` if an attribute doesn't exist
*/
@KtorExperimentalAPI
fun takeOrNull(key: AttributeKey): T? = getOrNull(key).also { remove(key) }
/**
* Gets a value of the attribute for the specified [key], or calls supplied [block] to compute its value
*/
fun computeIfAbsent(key: AttributeKey, block: () -> T): T
/**
* Returns [List] of all [AttributeKey] instances in this map
*/
val allKeys: List>
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy