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

commonMain.jetbrains.datalore.base.registration.RegistrationMap.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * 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