krontab/src/commonMain/kotlin/dev/inmo/krontab/internal/Parser.kt

77 lines
2.9 KiB
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab.internal
2019-10-05 10:40:51 +00:00
2020-11-21 08:58:19 +00:00
import dev.inmo.krontab.utils.clamp
2019-10-05 10:40:51 +00:00
typealias Converter<T> = (Int) -> T
internal val intToByteConverter: Converter<Byte> = { it: Int -> it.toByte() }
internal val intToIntConverter: Converter<Int> = { it: Int -> it }
private fun <T> createSimpleScheduler(from: String, dataRange: IntRange, dataConverter: Converter<T>): List<T>? {
2019-10-05 10:40:51 +00:00
val things = from.split(",")
val results = things.flatMap {
2020-10-10 15:12:32 +00:00
val currentToken = it.toLowerCase().replace(
"f", dataRange.first.toString()
).replace(
"l", dataRange.last.toString()
)
2019-10-05 10:40:51 +00:00
when {
2020-10-10 15:12:32 +00:00
currentToken.contains("-") -> {
val splitted = currentToken.split("-")
2020-07-24 07:16:56 +00:00
(splitted.first().toInt().clamp(dataRange) .. splitted[1].toInt().clamp(dataRange)).toList()
}
2020-10-10 15:12:32 +00:00
currentToken.contains("/") -> {
val (start, step) = currentToken.split("/")
2019-10-05 10:40:51 +00:00
val startNum = (if (start.isEmpty() || start == "*") {
0
} else {
start.toInt()
}).clamp(dataRange)
val stepNum = step.toInt().clamp(dataRange)
(startNum .. dataRange.last step stepNum).map { it }
}
2020-10-10 16:17:48 +00:00
currentToken == "*" -> return null
else -> listOf(currentToken.toInt().clamp(dataRange))
2019-10-05 10:40:51 +00:00
}
}
return results.map(dataConverter)
2019-10-05 10:40:51 +00:00
}
2021-03-30 08:07:39 +00:00
internal fun parseOffset(from: String?) = from ?.let { if (it.endsWith("o")) it.removeSuffix("o").toIntOrNull() else null }
internal fun parseYears(from: String?) = from ?.let { createSimpleScheduler(from, yearRange, intToIntConverter) ?.toTypedArray() }
internal fun parseMonths(from: String) = createSimpleScheduler(from, monthRange, intToByteConverter) ?.toTypedArray()
internal fun parseDaysOfMonth(from: String) = createSimpleScheduler(from, dayOfMonthRange, intToByteConverter) ?.toTypedArray()
internal fun parseHours(from: String) = createSimpleScheduler(from, hoursRange, intToByteConverter) ?.toTypedArray()
internal fun parseMinutes(from: String) = createSimpleScheduler(from, minutesRange, intToByteConverter) ?.toTypedArray()
internal fun parseSeconds(from: String) = createSimpleScheduler(from, secondsRange, intToByteConverter) ?.toTypedArray()
2019-10-05 10:40:51 +00:00
internal fun <T> Array<T>.fillWith(
2019-10-05 10:40:51 +00:00
whereToPut: MutableList<CronDateTime>,
createFactory: (CronDateTime, T) -> CronDateTime
2019-10-05 10:40:51 +00:00
) {
val previousValues = whereToPut.toList()
whereToPut.clear()
previousValues.forEach { previousValue ->
forEach {
whereToPut.add(createFactory(previousValue, it))
}
}
}
2021-03-30 08:07:39 +00:00
internal fun <T> T.fillWith(
whereToPut: MutableList<CronDateTime>,
createFactory: (CronDateTime, T) -> CronDateTime
) {
val previousValues = whereToPut.toList()
whereToPut.clear()
previousValues.forEach { previousValue ->
whereToPut.add(createFactory(previousValue, this))
}
}