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

commonMain.org.jetbrains.letsPlot.intern.standardizing.Standardizing.kt Maven / Gradle / Ivy

There is a newer version: 4.9.2
Show newest version
/*
 * Copyright (c) 2021. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package org.jetbrains.letsPlot.intern.standardizing

import org.jetbrains.letsPlot.commons.intern.datetime.Instant

internal object Standardizing {
    fun standardizeValue(value: Any?): Any? {
        return when (value) {
            null -> value
            is String -> value
            is Number -> toDouble(value)
            is Char -> value.toString()
            is org.jetbrains.letsPlot.commons.values.Color -> value.toHexColor()
            is Map<*, *> -> MapStandardizing.standardize(value)
            is Enum<*> -> value.name
            is Instant -> value.timeSinceEpoch
            else -> {
                if (JvmStandardizing.isJvm(value)) {
                    JvmStandardizing.standardize(value)
                } else if (SeriesStandardizing.isListy(value)) {
                    val l = SeriesStandardizing.toList(value)
                    l.map { standardizeValue(it) }
                } else {
//                    throw IllegalArgumentException(
//                        "Can't standardize the value \"$value\" of type ${
//                            ReflectionPatch.qualifiedName(value)
//                        } as a string, number or date-time."
//                    )
                    // Just ignore: this might be some ligit object like `org.jetbrains.letsPlot.MappingMeta` for example.
                    value
                }
            }
        }
    }

    private fun toDouble(n: Number): Double? {
        return when (n) {
            is Float -> if (n.isFinite()) n.toDouble() else null
            is Double -> if (n.isFinite()) n else null
            else -> n.toDouble()
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy