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

commonMain.jetbrains.datalore.base.observable.property.SimpleDerivedProperty.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.observable.property

import jetbrains.datalore.base.function.Supplier
import jetbrains.datalore.base.observable.event.EventHandler
import jetbrains.datalore.base.registration.Registration

/**
 * Derived property based on a Supplier and a set of dependencies 
 */
class SimpleDerivedProperty(
        private val mySupplier: Supplier,
        vararg deps: ReadableProperty<*>) :
        BaseDerivedProperty(mySupplier.get()) {

    private val myDependencies: Array>
    private var myRegistrations: Array? = null

    override val propExpr: String
        get() {
            val result = StringBuilder()
            result.append("derived(")
            result.append(mySupplier)
            for (d in myDependencies) {
                result.append(", ")
                result.append(d.propExpr)
            }
            result.append(")")
            return result.toString()
        }

    init {

//        myDependencies = arrayOfNulls>(deps.size)
//        System.arraycopy(deps, 0, myDependencies, 0, deps.size)
        myDependencies = arrayOf(*deps)
    }

    override fun doAddListeners() {
        myRegistrations = Array(myDependencies.size) { i ->
            register(myDependencies[i])
        }

//        myRegistrations = arrayOfNulls(myDependencies.size)
//        myRegistrations = arr
//        var i = 0
//        val myDependenciesLength = myDependencies.size
//        while (i < myDependenciesLength) {
//            myRegistrations[i] = register(myDependencies[i])
//            i++
//        }
    }

    private fun  register(prop: ReadableProperty): Registration {
        return prop.addHandler(object : EventHandler> {
            override fun onEvent(event: PropertyChangeEvent) {
                somethingChanged()
            }
        })
    }

    override fun doRemoveListeners() {
        for (r in myRegistrations!!) {
            r.remove()
        }
        myRegistrations = null
    }

    override fun doGet(): ValueT {
        return mySupplier.get()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy