I have a usage case like;
val fieldType = "int" // we know what is the type of field
val value1 = "1" // can be double/int/long/float etc.
val value2 = "2" // can be double/int/long/float etc.
val operator = "=" // can be >, >=, <, <= etc.
def compare(operator, value1, value2): Boolean = {}
Couldn't find a good way to do this. Is there any way to do this efficiently?
EDIT
I've just made this one but not sure this is the most efficient way;
private def apply(fieldType: String, operator: String, value1: String, value2: String): Boolean =
operator match {
case ">" =>
compare(fieldType, value1, value2) > 0
case ">=" =>
compare(fieldType, value1, value2) >= 0
case "<" =>
compare(fieldType, value1, value2) < 0
case "<=" =>
compare(fieldType, value1, value2) <= 0
case "==" =>
compare(fieldType, value1, value2) == 0
}
private def compare(fieldType: String, value1: String, value2: String): Int =
fieldType match {
case "string" => value1.compareTo(value2)
case "int" => value1.toInt.compareTo(value2.toInt)
case "long" => value1.toLong.compareTo(value2.toLong)
case "float" => value1.toFloat.compareTo(value2.toFloat)
}