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

org.jetbrains.kotlin.statistics.metrics.MetricContainers.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.statistics.metrics

import java.util.*

interface IMetricContainer {
    fun addValue(t: T)

    fun toStringRepresentation(): String

    fun getValue(): T?
}

interface IMetricContainerFactory {
    fun newMetricContainer(): IMetricContainer

    fun fromStringRepresentation(state: String): IMetricContainer?
}

open class OverrideMetricContainer() : IMetricContainer {
    internal var myValue: T? = null

    override fun addValue(t: T) {
        myValue = t
    }

    internal constructor(v: T?) : this() {
        myValue = v
    }

    override fun toStringRepresentation(): String {
        return myValue?.toString() ?: "null"
    }

    override fun getValue() = myValue
}

class OverrideVersionMetricContainer() : OverrideMetricContainer() {
    constructor(v: String) : this() {
        myValue = v
    }

    override fun addValue(t: String) {
        if (myValue == null || myValue == "0.0.0") {
            myValue = t
        }
    }
}

class SumMetricContainer() : OverrideMetricContainer() {
    constructor(v: Long) : this() {
        myValue = v
    }

    override fun addValue(t: Long) {
        myValue = (myValue ?: 0) + t
    }
}

class AverageMetricContainer() : IMetricContainer {
    private var count = 0
    private var myValue: Long? = null

    constructor(v: Long) : this() {
        myValue = v
    }

    override fun addValue(t: Long) {
        myValue = (myValue ?: 0) + t
        count++
    }

    override fun toStringRepresentation(): String {
        return getValue()?.toString() ?: "null"
    }

    override fun getValue(): Long? {
        return myValue?.div(count)
    }
}

class OrMetricContainer() : OverrideMetricContainer() {
    constructor(v: Boolean) : this() {
        myValue = v
    }

    override fun addValue(t: Boolean) {
        myValue = (myValue ?: false) || t
    }
}

class ConcatMetricContainer() : IMetricContainer {
    private val myValues = TreeSet()

    companion object {
        const val SEPARATOR = ";"
    }

    constructor(values: Collection) : this() {
        myValues.addAll(values)
    }

    override fun addValue(t: String) {
        myValues.add(t.replace(SEPARATOR, ","))
    }

    override fun toStringRepresentation(): String {
        return myValues.sorted().joinToString(SEPARATOR)
    }

    override fun getValue() = toStringRepresentation()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy