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

commonMain.aws.sdk.kotlin.crt.util.CaseInsensitiveMap.kt Maven / Gradle / Ivy

There is a newer version: 0.8.9
Show newest version
/*
 * 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"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy