Pavlo Liapota
09/13/2018, 9:39 PMfun <T> getAs(clazz: Class<T>, converter: ((String) -> Any)? = null): T {
try {
val convertedValue =
if (converter != null) converter(value)
else when (clazz) {
Int::class.java -> value.toInt()
Integer::class.java -> value.toInt()
Double::class.java -> value.toDouble()
Long::class.java -> value.toLong()
Date::class.java -> Date(value)
else -> throw IllegalArgumentException("Can't auto-cast to $clazz. Add a converter as a second argument.")
}
return convertedValue as T
} catch (e: Exception) {
// ...
}
}