Optimize GradeCustomConverters
This commit is contained in:
parent
0f68c9b1d9
commit
9ac5de0aa5
1 changed files with 11 additions and 9 deletions
|
@ -5,6 +5,10 @@ import pl.droidsonroids.jspoon.ElementConverter
|
||||||
import pl.droidsonroids.jspoon.annotation.Selector
|
import pl.droidsonroids.jspoon.annotation.Selector
|
||||||
|
|
||||||
private val validGrade = "^(\\++|-|--|=)?[0-6](\\++|-|--|=)?$".toRegex()
|
private val validGrade = "^(\\++|-|--|=)?[0-6](\\++|-|--|=)?$".toRegex()
|
||||||
|
private val gradeMinus = "[-][0-6]|[0-6][-]".toRegex()
|
||||||
|
private val gradePlus = "[+][0-6]|[0-6][+]".toRegex()
|
||||||
|
private val gradeDoublePlus = "[+]{2}[0-6]|[0-6][+]{2}".toRegex()
|
||||||
|
private val gradeDoubleMinus = "[-|=]{1,2}[0-6]|[0-6][-|=]{1,2}".toRegex()
|
||||||
private const val modifierWeight = .33
|
private const val modifierWeight = .33
|
||||||
|
|
||||||
class GradeValueConverter : ElementConverter<Int?> {
|
class GradeValueConverter : ElementConverter<Int?> {
|
||||||
|
@ -36,16 +40,14 @@ fun isGradeValid(grade: String): Boolean {
|
||||||
|
|
||||||
fun getGradeValueWithModifier(grade: String): Pair<Int, Double> {
|
fun getGradeValueWithModifier(grade: String): Pair<Int, Double> {
|
||||||
return grade.substringBefore(" (").run {
|
return grade.substringBefore(" (").run {
|
||||||
if (this.matches(validGrade)) {
|
if (matches(validGrade)) {
|
||||||
when {
|
when {
|
||||||
matches("[-][0-6]|[0-6][-]".toRegex()) -> Pair(replace("-", "").toInt(), -modifierWeight)
|
matches(gradeMinus) -> replace("-", "").toInt() to -modifierWeight
|
||||||
matches("[+][0-6]|[0-6][+]".toRegex()) -> Pair(replace("+", "").toInt(), modifierWeight)
|
matches(gradePlus) -> replace("+", "").toInt() to modifierWeight
|
||||||
matches("[+]{2}[0-6]|[0-6][+]{2}".toRegex()) -> Pair(replace("++", "").toInt(), .5)
|
matches(gradeDoublePlus) -> replace("++", "").toInt() to .5
|
||||||
matches("[-|=]{1,2}[0-6]|[0-6][-|=]{1,2}".toRegex()) -> Pair(replace("[-|=]{1,2}".toRegex(), "").toInt(), -.5)
|
matches(gradeDoubleMinus) -> replace("[-|=]{1,2}".toRegex(), "").toInt() to -.5
|
||||||
else -> Pair(this.toInt(), .0)
|
else -> toInt() to .0
|
||||||
}
|
}
|
||||||
} else {
|
} else 0 to .0
|
||||||
Pair(0, .0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue