commonMain.jetbrains.datalore.base.registration.RegistrationMap.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.base.registration
import jetbrains.datalore.base.function.Supplier
class RegistrationMap {
private val myMap = HashMap()
fun put(key: KeyT, registration: Registration) {
val prev = myMap.put(key, registration)
if (prev != null) {
prev.remove()
myMap.remove(key)!!.remove()
throw IllegalStateException("Registration for the key '$key' already exists.")
}
}
fun replace(key: KeyT, registrationSupplier: Supplier): Boolean {
val res = removeOptional(key)
myMap[key] = registrationSupplier.get()
return res
}
fun remove(key: KeyT) {
val prev = myMap.remove(key)
if (prev != null) {
prev.remove()
} else {
throw IllegalStateException("Registration for the key '$key' not found.")
}
}
fun removeOptional(key: KeyT): Boolean {
val prev = myMap.remove(key)
if (prev != null) {
prev.remove()
return true
} else {
return false
}
}
fun keys(): Set {
return myMap.keys
}
fun clear() {
try {
for (r in myMap.values) {
r.remove()
}
} finally {
myMap.clear()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy