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

commonMain.jetbrains.datalore.base.observable.property.ValueProperty.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2020. 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.observable.property

import jetbrains.datalore.base.observable.event.EventHandler
import jetbrains.datalore.base.observable.event.ListenerCaller
import jetbrains.datalore.base.observable.event.Listeners
import jetbrains.datalore.base.registration.Registration

/**
 * A simple implementation of Read/Write property which stores the value in a field
 */
open class ValueProperty(private var myValue: ValueT) :
    BaseReadableProperty(),
    Property {

    private var myHandlers: Listeners>>? = null

    override val propExpr: String
        get() = "valueProperty()"

    override fun get(): ValueT {
        return myValue
    }

    override fun set(value: ValueT) {
        if (value == myValue) return
        val oldValue = myValue
        myValue = value

        fireEvents(oldValue, myValue)
    }

    private fun fireEvents(oldValue: ValueT, newValue: ValueT) {
        if (myHandlers != null) {
            val event =
                PropertyChangeEvent(oldValue, newValue)
            myHandlers!!.fire(object : ListenerCaller>> {
                override fun call(l: EventHandler>) {
                    l.onEvent(event)
                }
            })
        }
    }

    override fun addHandler(handler: EventHandler>): Registration {
        if (myHandlers == null) {
            myHandlers = object : Listeners>>() {
                override fun afterLastRemoved() {
                    myHandlers = null
                }
            }
        }

        return myHandlers!!.add(handler)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy