2020-11-21 08:58:19 +00:00
|
|
|
package dev.inmo.krontab.internal
|
2019-10-05 10:40:51 +00:00
|
|
|
|
2021-01-02 15:35:08 +00:00
|
|
|
typealias Converter<T> = (Int) -> T
|
|
|
|
|
|
|
|
internal val intToByteConverter: Converter<Byte> = { it: Int -> it.toByte() }
|
2022-04-29 15:57:10 +00:00
|
|
|
internal val intToShortConverter: Converter<Short> = { it: Int -> it.toShort() }
|
2021-01-02 15:35:08 +00:00
|
|
|
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 {
|
2021-05-30 11:15:32 +00:00
|
|
|
val currentToken = it.lowercase().replace(
|
2020-10-10 15:12:32 +00:00
|
|
|
"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("-")
|
2021-04-22 05:58:19 +00:00
|
|
|
(splitted.first().toInt().coerceIn(dataRange) .. splitted[1].toInt().coerceIn(dataRange)).toList()
|
2020-07-24 07:16:56 +00:00
|
|
|
}
|
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()
|
2021-04-22 05:58:19 +00:00
|
|
|
}).coerceIn(dataRange)
|
|
|
|
val stepNum = step.toInt().coerceIn(dataRange)
|
2019-10-05 10:40:51 +00:00
|
|
|
(startNum .. dataRange.last step stepNum).map { it }
|
|
|
|
}
|
2020-10-10 16:17:48 +00:00
|
|
|
currentToken == "*" -> return null
|
2021-04-22 05:58:19 +00:00
|
|
|
else -> listOf(currentToken.toInt().coerceIn(dataRange))
|
2019-10-05 10:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:35:08 +00:00
|
|
|
return results.map(dataConverter)
|
2019-10-05 10:40:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 05:58:19 +00:00
|
|
|
internal fun parseWeekDay(from: String?) = from ?.let { if (it.endsWith("w")) createSimpleScheduler(it.removeSuffix("w"), dayOfWeekRange, intToByteConverter) ?.toTypedArray() else null }
|
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 }
|
2021-01-02 15:35:08 +00:00
|
|
|
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()
|
2022-04-29 15:57:10 +00:00
|
|
|
internal fun parseMilliseconds(from: String?) = from ?.let { if (it.endsWith("ms")) createSimpleScheduler(from.removeSuffix("ms"), millisecondsRange, intToShortConverter) ?.toTypedArray() else null }
|
2019-10-05 10:40:51 +00:00
|
|
|
|
2021-01-02 15:35:08 +00:00
|
|
|
internal fun <T> Array<T>.fillWith(
|
2019-10-05 10:40:51 +00:00
|
|
|
whereToPut: MutableList<CronDateTime>,
|
2021-01-02 15:35:08 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|