Home > database >  Enum with alias
Enum with alias

Time:06-02

I have a functionality which expects aliases for certain operations

For example - count_mean has alias mean and countDistinct has alias approxCountDistinct

"count_missing", "countMissing" -> this.countMissing.toDouble().right()
"count" -> this.count.toDouble().right()
"count_populated", "countPopulated" -> this.countPopulated.toDouble().right()
"count_distinct", "countDistinct", "approxCountDistinct" -> this.countDistinct.toDouble().right()
"count_mean", "mean" -> this.getDecimalData("mean").right() 

I want to create an Enum class or something else to efficiently separate all the types of metrics in another file.

Right now I was thinking of Enum class

enum class FieldProfileMetrics(val value: String) { 
  CountDistinct("countDistinct", "approxDistinctCount", "count_distinct")
  .
  .
}

Followed by using the enum class like this, which would return the appropriate metric from Enum (using the aliases to check)

val metric = FieldProfileMetrics.valueOf(metricName) 

Any help to figure a better way to structure this is appreciated

CodePudding user response:

I recommend make aliasOf method.

This is sample code.

enum class FieldProfileMetrics {
    Unknown,
    CountMissing,
    Count,
    CountPopulated,
    CountDistinct,
    CountMean;
    
    companion object {
        fun aliasOf(value: String): FieldProfileMetrics {
            return when(value) {
                "count_missing", "countMissing" -> CountMissing
                "count" -> Count
                "count_populated", "countPopulated" -> CountPopulated
                "count_distinct", "countDistinct", "approxCountDistinct" -> CountDistinct
                "count_mean", "mean" -> CountMean
                else -> Unknown
            }
        }
    }
}

and you use the method.

val metric = FieldProfileMetrics.aliasOf(metricName)
  • Related