2016-07-30 02:08:57 +02:00
|
|
|
package org.mariotaku.ktextension
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 16/7/30.
|
|
|
|
*/
|
|
|
|
|
2016-12-07 07:15:12 +01:00
|
|
|
fun String?.toLong(def: Long): Long {
|
2016-07-30 02:08:57 +02:00
|
|
|
try {
|
2016-12-07 07:15:12 +01:00
|
|
|
return this?.toLong() ?: def
|
2016-07-30 02:08:57 +02:00
|
|
|
} catch (e: NumberFormatException) {
|
|
|
|
return def
|
|
|
|
}
|
2016-08-29 13:03:55 +02:00
|
|
|
}
|
|
|
|
|
2016-12-07 07:15:12 +01:00
|
|
|
fun String?.toInt(def: Int): Int {
|
2016-12-03 16:45:13 +01:00
|
|
|
try {
|
2016-12-07 07:15:12 +01:00
|
|
|
return this?.toInt() ?: def
|
2016-12-03 16:45:13 +01:00
|
|
|
} catch (e: NumberFormatException) {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 14:31:14 +01:00
|
|
|
fun String?.toDouble(def: Double): Double {
|
2016-08-29 13:03:55 +02:00
|
|
|
try {
|
2017-01-23 14:31:14 +01:00
|
|
|
return this?.toDouble() ?: def
|
2016-08-29 13:03:55 +02:00
|
|
|
} catch (e: NumberFormatException) {
|
2017-01-23 14:31:14 +01:00
|
|
|
return def
|
2016-08-29 13:03:55 +02:00
|
|
|
}
|
2017-01-31 05:04:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun Int.coerceInOr(range: ClosedRange<Int>, or: Int): Int {
|
|
|
|
if (range.isEmpty()) return or
|
|
|
|
return coerceIn(range)
|
2016-07-30 02:08:57 +02:00
|
|
|
}
|