timetable/module-model/src/main/kotlin/de/twomartens/timetable/model/common/StationId.kt

30 lines
882 B
Kotlin

package de.twomartens.timetable.model.common
import de.twomartens.timetable.types.NonEmptyString
class StationId private constructor(val value: String) {
companion object {
private val idPattern = Regex("^\\w{2}-(?<countryStationId>\\w.*)")
fun of(id: NonEmptyString): StationId {
require(idPattern.matches(id.value)) {
"StationId must start with two character country code, followed by a dash, " +
"and at least one word character"
}
return StationId(id.value)
}
}
val stationIdWithinCountry: String
get() {
val matchResult = idPattern.matchEntire(value)
val groupMatch = matchResult?.groups?.get("countryStationId")?.value
require(matchResult != null && groupMatch != null)
return groupMatch
}
}