diff --git a/weak/src/jvmMain/kotlin/algorithms/JvmWeakHashMap.kt b/weak/src/jvmMain/kotlin/algorithms/JvmWeakHashMap.kt new file mode 100644 index 0000000..15a5d81 --- /dev/null +++ b/weak/src/jvmMain/kotlin/algorithms/JvmWeakHashMap.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025, OpenSavvy and contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opensavvy.pedestal.weak.algorithms + +import opensavvy.pedestal.weak.ExperimentalWeakApi +import opensavvy.pedestal.weak.WeakMap +import java.util.* + +private class JvmWeakHashMap( + private val java: WeakHashMap, +) : WeakMap { + + override fun get(key: K): V? = + java[key] + + override fun set(key: K, value: V) { + java[key] = value + } + + @ExperimentalWeakApi + override fun contains(key: K): Boolean = + key in java + + @ExperimentalWeakApi + override fun remove(key: K): V? = + java.remove(key) + +} + +/** + * Wraps a JVM [WeakHashMap] into a multiplatform [WeakMap]. + * + * The resulting weak map has the exact same properties as the JVM map. + * Most importantly: + * - The keys are held weakly. + * - The values are held strongly. + * - The map is not thread-safe. + */ +@ExperimentalWeakApi +fun WeakHashMap.asMultiplatform(): WeakMap = + JvmWeakHashMap(this)