commonMain.aws.sdk.kotlin.crt.util.CaseInsensitiveMap.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-crt-kotlin-android Show documentation
Show all versions of aws-crt-kotlin-android Show documentation
Kotlin Multiplatform bindings for AWS SDK Common Runtime
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.crt.util
private class CaseInsensitiveString(val s: String) {
val hash: Int = s.lowercase().hashCode()
override fun hashCode(): Int = hash
override fun equals(other: Any?): Boolean = other is CaseInsensitiveString && other.s.equals(s, ignoreCase = true)
override fun toString(): String = s
}
private fun String.toInsensitive(): CaseInsensitiveString =
CaseInsensitiveString(this)
/**
* Map of case-insensitive [String] to [Value]
*/
internal class CaseInsensitiveMap : MutableMap {
private val impl: MutableMap = mutableMapOf()
override val size: Int
get() = impl.size
override fun containsKey(key: String): Boolean = impl.containsKey(key.toInsensitive())
override fun containsValue(value: Value): Boolean = impl.containsValue(value)
override fun get(key: String): Value? = impl.get(key.toInsensitive())
override fun isEmpty(): Boolean = impl.isEmpty()
override val entries: MutableSet>
get() = impl.entries.map {
Entry(it.key.s, it.value)
}.toMutableSet()
override val keys: MutableSet
get() = impl.keys.map { it.s }.toMutableSet()
override val values: MutableCollection
get() = impl.values
override fun clear() = impl.clear()
override fun put(key: String, value: Value): Value? = impl.put(key.toInsensitive(), value)
override fun putAll(from: Map) {
for ((key, value) in from) {
put(key, value)
}
}
override fun remove(key: String): Value? = impl.remove(key.toInsensitive())
}
private class Entry(
override val key: Key,
override var value: Value,
) : MutableMap.MutableEntry {
override fun setValue(newValue: Value): Value {
value = newValue
return value
}
override fun hashCode(): Int = 17 * 31 + key!!.hashCode() + value!!.hashCode()
override fun equals(other: Any?): Boolean {
if (other == null || other !is Map.Entry<*, *>) return false
return other.key == key && other.value == value
}
override fun toString(): String = "$key=$value"
}