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

commonMain.jetbrains.datalore.plot.base.scale.AbstractScale.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.plot.base.scale

import jetbrains.datalore.base.gcommon.base.Preconditions
import jetbrains.datalore.plot.base.Scale
import jetbrains.datalore.plot.base.Transform

internal abstract class AbstractScale : Scale {

    final override val name: String
    final override var mapper: ((Double?) -> T?)
        private set
    final override var multiplicativeExpand = 0.0
        protected set
    final override var additiveExpand = 0.0
        protected set

    private val myTransform: Transform?
    private var myBreaks: List? = null
    private var myLabels: List? = null
    private var myLabelFormatter: ((Any) -> String)? = null

    override val isContinuous: Boolean
        get() = false

    override val isContinuousDomain: Boolean
        get() = false

    override var breaks: List
        get() {
            Preconditions.checkState(hasBreaks(), "No breaks defined for scale $name")
            @Suppress("UNCHECKED_CAST")
            return myBreaks!! as List
        }
        protected set(breaks) {
            myBreaks = breaks.map {
                @Suppress("UNCHECKED_CAST")
                it as DomainT
            }
        }

    override val labels: List
        get() {
            Preconditions.checkState(labelsDefined(), "No labels defined for scale $name")
            return myLabels!!
        }

    override val labelFormatter: ((Any) -> String)?
        get() = myLabelFormatter

    override val transform: Transform
        get() = myTransform ?: defaultTransform

    protected abstract val defaultTransform: Transform

    protected constructor(name: String, mapper: ((Double?) -> T?)) {
        this.name = name
        this.mapper = mapper
        myTransform = null
    }

    protected constructor(b: AbstractBuilder) {
        name = b.myName
        myBreaks = b.myBreaks
        myLabels = b.myLabels
        myLabelFormatter = b.myLabelFormatter
        myTransform = b.myTransform
        mapper = b.myMapper

        multiplicativeExpand = b.myMultiplicativeExpand
        additiveExpand = b.myAdditiveExpand
    }

    override fun hasBreaks(): Boolean {
        return myBreaks != null
    }

    override fun hasLabels(): Boolean {
        return labelsDefined()
    }

    private fun labelsDefined(): Boolean {
        return myLabels != null
    }

    protected abstract class AbstractBuilder(scale: AbstractScale) : Scale.Builder {
        internal val myName: String = scale.name
        internal var myTransform: Transform? = scale.myTransform

        internal var myBreaks: List? = scale.myBreaks
        internal var myLabels: List? = scale.myLabels
        internal var myLabelFormatter: ((Any) -> String)? = scale.myLabelFormatter
        internal var myMapper: (Double?) -> T? = scale.mapper

        internal var myMultiplicativeExpand: Double = scale.multiplicativeExpand
        internal var myAdditiveExpand: Double = scale.additiveExpand

        override fun breaks(l: List): Scale.Builder {
            myBreaks = l.map {
                @Suppress("UNCHECKED_CAST")
                it as DomainT
            }
            return this
        }

        override fun labels(l: List): Scale.Builder {
            myLabels = l
            return this
        }

        override fun labelFormatter(v: (Any) -> String): Scale.Builder{
            myLabelFormatter = v
            return this
        }

        override fun mapper(m: (Double?) -> T?): Scale.Builder {
            myMapper = m
            return this
        }

        override fun multiplicativeExpand(v: Double): Scale.Builder {
            myMultiplicativeExpand = v
            return this
        }

        override fun additiveExpand(v: Double): Scale.Builder {
            myAdditiveExpand = v
            return this
        }

        protected fun transform(v: Transform): Scale.Builder {
            myTransform = v
            return this
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy