Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions src/main/java/org/mtransit/commons/Letters.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package org.mtransit.commons

object Letters {
const val NONE_: Long = 0L
const val NONE_ = 0

const val A: Long = 1L
const val B: Long = 2L
const val C: Long = 3L
const val D: Long = 4L
const val E: Long = 5L
const val F: Long = 6L
const val G: Long = 7L
const val H: Long = 8L
const val I: Long = 9L
const val J: Long = 10L
const val K: Long = 11L
const val L: Long = 12L
const val M: Long = 13L
const val N: Long = 14L
const val O: Long = 15L
const val P: Long = 16L
const val Q: Long = 17L
const val R: Long = 18L
const val S: Long = 19L
const val T: Long = 20L
const val U: Long = 21L
const val V: Long = 22L
const val W: Long = 23L
const val X: Long = 24L
const val Y: Long = 25L
const val Z: Long = 26L
const val A = 1
const val B = 2
const val C = 3
const val D = 4
const val E = 5
const val F = 6
const val G = 7
const val H = 8
const val I = 9
const val J = 10
const val K = 11
const val L = 12
const val M = 13
const val N = 14
const val O = 15
const val P = 16
const val Q = 17
const val R = 18
const val S = 19
const val T = 20
const val U = 21
const val V = 22
const val W = 23
const val X = 24
const val Y = 25
const val Z = 26

const val OTHER_MIN_: Long = 27L
const val OTHER_MIN_ = 27
}
24 changes: 20 additions & 4 deletions src/main/java/org/mtransit/parser/DefaultAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.mtransit.parser.mt.data.MServiceId;
import org.mtransit.parser.mt.data.MServiceIds;
import org.mtransit.parser.mt.data.MSpec;
import org.mtransit.parser.mt.data.MStopIDConverter;
import org.mtransit.parser.mt.data.MString;
import org.mtransit.parser.mt.data.MStrings;
import org.mtransit.parser.mt.data.MTripId;
Expand Down Expand Up @@ -1267,21 +1268,36 @@ public int getStopId(@NotNull GStop gStop) {
useStopCodeForStopId() ? cleanStopOriginalId(getStopCode(gStop))
: gStopId;
if (stopIdS.isBlank() || !CharUtils.isDigitsOnly(stopIdS)) {
Integer stopIdSInt = convertStopIdFromCodeNotSupported(stopIdS);
Integer stopIdSInt = convertStopIdNotSupported(stopIdS);
if (stopIdSInt != null) return stopIdSInt;
stopIdSInt = Configs.getRouteConfig().convertStopIdFromOriginalNotSupported(gStopId);
if (stopIdSInt != null) return stopIdSInt;
return MStopIDConverter.convert(
stopIdS,
this::convertStopIdNotSupported,
this::convertStopIdNextChars,
this::convertStopIdPreviousChars
);
}
return Integer.parseInt(stopIdS);
Comment thread
mmathieum marked this conversation as resolved.
} catch (Exception e) {
throw new MTLog.Fatal(e, "Error while extracting stop ID from %s!", gStop.toStringPlus(true));
}
}

@Nullable
@Override
public Integer convertStopIdFromCodeNotSupported(@NotNull String stopCode) {
return Configs.getRouteConfig().convertStopIdFromCodeNotSupported(stopCode);
public @Nullable Integer convertStopIdNotSupported(@NotNull String stopCode) {
return Configs.getRouteConfig().convertStopIdNotSupported(stopCode);
}

@Override
public @Nullable Integer convertStopIdNextChars(@NotNull String nextChars) {
return Configs.getRouteConfig().convertStopIdNextChars(nextChars);
}

@Override
public @Nullable Integer convertStopIdPreviousChars(@NotNull String previousChars) {
return Configs.getRouteConfig().convertStopIdPreviousChars(previousChars);
}

@Override
Expand Down
45 changes: 35 additions & 10 deletions src/main/java/org/mtransit/parser/config/gtfs/data/RouteConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.mtransit.commons.CleanUtils
import org.mtransit.commons.StringUtils.EMPTY
import org.mtransit.commons.toIntOrNull
import org.mtransit.parser.gtfs.data.GRoute
import org.mtransit.parser.gtfs.data.GStopTime
import org.mtransit.parser.gtfs.data.GTrip
Expand Down Expand Up @@ -31,9 +32,9 @@ data class RouteConfig(
@SerialName("route_short_name_to_route_id_configs")
val routeShortNameToRouteIdConfigs: List<RouteShortNameToRouteIdConfig> = emptyList(),
@SerialName("route_id_next_char_configs")
val routeIdNextCharConfigs: List<RouteIdCharToRouteIdPartConfig> = emptyList(),
val routeIdNextCharConfigs: List<IdCharToIdPartConfig> = emptyList(),
@SerialName("route_id_previous_char_configs")
val routeIdPreviousCharConfigs: List<RouteIdCharToRouteIdPartConfig> = emptyList(),
val routeIdPreviousCharConfigs: List<IdCharToIdPartConfig> = emptyList(),
// short-name
@SerialName("use_route_long_name_for_route_short_name")
val useRouteLongNameForRouteShortName: Boolean = false, // OPT-IN feature
Expand Down Expand Up @@ -102,8 +103,15 @@ data class RouteConfig(
val useStopCodeForStopId: Boolean = false, // OPT-IN feature
@SerialName("use_stop_id_for_stop_code")
val useStopIdForStopCode: Boolean = false, // OPT-IN feature
@Deprecated("use stopIdNotSupportedConfigs instead")
@SerialName("stop_code_to_stop_id_configs")
val stopCodeToStopIdConfigs: List<StopCodeToStopIdConfig> = emptyList(),
val stopCodeToStopIdConfigs: List<StopIdConfig> = emptyList(),
@SerialName("stop_id_not_supported_configs")
val stopIdNotSupportedConfigs: List<StopIdConfig> = stopCodeToStopIdConfigs,
@SerialName("stop_id_next_char_configs")
val stopIdNextCharConfigs: List<IdCharToIdPartConfig> = emptyList(),
@SerialName("stop_id_previous_char_configs")
val stopIdPreviousCharConfigs: List<IdCharToIdPartConfig> = emptyList(),
@SerialName("stop_original_id_to_stop_id_configs")
val stopOriginalIdToStopIdConfigs: List<StopOriginalIdToStopIdConfig> = emptyList(),
@SerialName("stop_code_prepend_if_missing")
Expand Down Expand Up @@ -146,11 +154,14 @@ data class RouteConfig(
)

@Serializable
data class RouteIdCharToRouteIdPartConfig(
data class IdCharToIdPartConfig(
@SerialName("char")
val char: String,
@SerialName("route_id_part")
@Deprecated("use idPart instead")
val routeIdPart: Long,
@SerialName("id_part")
val idPart: Long = routeIdPart
)

@Serializable
Expand All @@ -174,11 +185,17 @@ data class RouteConfig(
)

@Serializable
data class StopCodeToStopIdConfig(
data class StopIdConfig(
@Deprecated("use originalStopId instead")
@SerialName("stop_code")
val stopCode: String,
@SerialName("original_stop_id")
val originalStopId: String = stopCode,
@Deprecated("use stopIdInt instead")
@SerialName("stop_id")
val stopId: Int,
@SerialName("stop_id_int")
val stopIdInt: Int = stopId
)

@Serializable
Expand Down Expand Up @@ -237,11 +254,11 @@ data class RouteConfig(

fun convertRouteIdNextChars(nextChars: String) =
this.routeIdNextCharConfigs
.singleOrNull { it.char == nextChars }?.routeIdPart
.singleOrNull { it.char == nextChars }?.idPart

fun convertRouteIdPreviousChars(previousChars: String) =
this.routeIdPreviousCharConfigs
.singleOrNull { it.char == previousChars }?.routeIdPart
.singleOrNull { it.char == previousChars }?.idPart

fun getRouteShortNameFromRouteId(routeId: String) =
this.routeIdToRouteShortNameConfigs
Expand Down Expand Up @@ -342,9 +359,17 @@ data class RouteConfig(
this.stopOriginalIdToStopIdConfigs
.singleOrNull { it.originalStopId == originalStopId }?.stopId

fun convertStopIdFromCodeNotSupported(stopCode: String) =
this.stopCodeToStopIdConfigs
.singleOrNull { it.stopCode == stopCode }?.stopId
fun convertStopIdNotSupported(originalStopId: String) =
this.stopIdNotSupportedConfigs
.singleOrNull { it.originalStopId == originalStopId }?.stopIdInt

fun convertStopIdNextChars(nextChars: String) =
this.stopIdNextCharConfigs
.singleOrNull { it.char == nextChars }?.idPart?.toIntOrNull()

fun convertStopIdPreviousChars(previousChars: String) =
this.stopIdPreviousCharConfigs
.singleOrNull { it.char == previousChars }?.idPart?.toIntOrNull()

private fun cleanString(lang: Locale, originalString: String, cleaners: List<Cleaner>): String {
if (cleaners.isEmpty()) return originalString
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ public interface GAgencyTools {
// STOP
int getStopId(@NotNull GStop gStop);

@Nullable
Integer convertStopIdFromCodeNotSupported(@NotNull String stopCode);
@Nullable Integer convertStopIdNotSupported(@NotNull String stopCode);

@Nullable Integer convertStopIdNextChars(@NotNull String nextChars);

@Nullable Integer convertStopIdPreviousChars(@NotNull String previousChars);

boolean useStopCodeForStopId();

Expand Down
33 changes: 14 additions & 19 deletions src/main/java/org/mtransit/parser/mt/data/MRouteSNToIDConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ object MRouteSNToIDConverter {

// endregion

const val PREVIOUS: Long = 1_000_000L
const val NEXT: Long = 10_000L
const val PREVIOUS = 1_000_000L
const val NEXT = 10_000L
const val MAX_DIGIT = 1000L

@JvmOverloads
@JvmStatic
Expand All @@ -128,19 +129,19 @@ object MRouteSNToIDConverter {
if (rsn.length == 1 && rsn[0].isLetter()) {
endsWithLetter(rsn)?.let { return it }
}
val matcher: Matcher = RSN.matcher(rsn)
val matcher = RSN.matcher(rsn)
if (!matcher.find()) {
return notSupportedToRouteId?.invoke(rsn)
?: throw MTLog.Fatal("Unexpected route short name '$rsn' can not be parsed by regex!")
}
val previousChars: String = matcher.group(2).uppercase()
val digits: Long = matcher.group(3).toLong()
val nextChars: String = matcher.group(4).uppercase()
if (digits !in 0..1000L) {
val previousChars = matcher.group(2).uppercase()
val digits = matcher.group(3).toLong()
val nextChars = matcher.group(4).uppercase()
if (digits !in 0..MAX_DIGIT) {
return notSupportedToRouteId?.invoke(rsn)
?: throw MTLog.Fatal("Unexpected route short name digits '$digits' in short name '$rsn' to convert to route ID!")
}
var routeId: Long = digits
var routeId = digits
routeId += endsWithLetter(nextChars) ?: run {
nextCharsToLong?.invoke(nextChars)
?: notSupportedToRouteId?.invoke(rsn)
Expand All @@ -155,7 +156,7 @@ object MRouteSNToIDConverter {
}

@JvmStatic
fun startsWithLetter(previousChars: String): Long? = when (previousChars) {
fun startsWithLetter(previousChars: String) = when (previousChars) {
"" -> startsWith(Letters.NONE_)
"A" -> startsWith(Letters.A)
"B" -> startsWith(Letters.B)
Expand Down Expand Up @@ -219,17 +220,11 @@ object MRouteSNToIDConverter {
}

@JvmStatic
fun startsWith(digit: Long): Long {
return digit * PREVIOUS
}
fun startsWith(digit: Int) = digit * PREVIOUS

@JvmStatic
fun endsWith(digit: Long): Long {
return digit * NEXT
}
fun endsWith(digit: Int) = digit * NEXT

@JvmStatic
fun other(digit: Long): Long {
return Letters.OTHER_MIN_ + digit
}
}
fun other(digit: Int) = (Letters.OTHER_MIN_ + digit).toLong()
}
Loading