add parsing of strings with values

This commit is contained in:
InsanusMokrassar 2019-10-05 10:55:01 +06:00
parent 8a596b2e47
commit 6f48b4f29a
3 changed files with 106 additions and 14 deletions

View File

@ -9,20 +9,6 @@ private val incomeHourRange = 0 .. 23
private val incomeMinuteRange = 0 .. 59 private val incomeMinuteRange = 0 .. 59
private val incomeSecondRange = 0 .. 59 private val incomeSecondRange = 0 .. 59
fun CronDateTime(
month: Int? = null,
dayOfMonth: Int? = null,
hours: Int? = null,
minutes: Int? = null,
seconds: Int? = null
) = CronDateTime(
month ?.clamp(incomeMonthRange) ?.toByte(),
dayOfMonth ?.clamp(incomeDayOfMonthRange) ?.toByte(),
hours ?.clamp(incomeHourRange) ?.toByte(),
minutes ?.clamp(incomeMinuteRange) ?.toByte(),
seconds ?.clamp(incomeSecondRange) ?.toByte()
)
/** /**
* [month] 0-11 * [month] 0-11
* [dayOfMonth] 0-31 * [dayOfMonth] 0-31
@ -46,6 +32,22 @@ data class CronDateTime(
} }
internal val klockDayOfMonth = dayOfMonth ?.plus(1) internal val klockDayOfMonth = dayOfMonth ?.plus(1)
companion object {
fun create(
month: Int? = null,
dayOfMonth: Int? = null,
hours: Int? = null,
minutes: Int? = null,
seconds: Int? = null
) = CronDateTime(
month ?.clamp(incomeMonthRange) ?.toByte(),
dayOfMonth ?.clamp(incomeDayOfMonthRange) ?.toByte(),
hours ?.clamp(incomeHourRange) ?.toByte(),
minutes ?.clamp(incomeMinuteRange) ?.toByte(),
seconds ?.clamp(incomeSecondRange) ?.toByte()
)
}
} }
fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime { fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now()): DateTime {

View File

@ -0,0 +1,84 @@
package com.github.insanusmokrassar.krontab
import com.github.insanusmokrassar.krontab.utils.*
import com.github.insanusmokrassar.krontab.utils.clamp
import com.github.insanusmokrassar.krontab.utils.minutesRange
import com.github.insanusmokrassar.krontab.utils.secondsRange
private fun parse(from: String, dataRange: IntRange): Array<Byte>? {
val things = from.split(",")
val results = things.flatMap {
when {
it.contains("/") -> {
val (start, step) = it.split("/")
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 }
}
it == "*" -> return null
else -> listOf(it.toInt().clamp(dataRange))
}
}
return results.map { it.toByte() }.toTypedArray()
}
private fun parseMonths(from: String) = parse(from, monthRange)
private fun parseDaysOfMonth(from: String) = parse(from, dayOfMonthRange)
private fun parseHours(from: String) = parse(from, hoursRange)
private fun parseMinutes(from: String) = parse(from, minutesRange)
private fun parseSeconds(from: String) = parse(from, secondsRange)
private fun Array<Byte>.fillWith(
whereToPut: MutableList<CronDateTime>,
createFactory: (CronDateTime, Byte) -> CronDateTime
) {
val previousValues = whereToPut.toList()
whereToPut.clear()
previousValues.forEach { previousValue ->
forEach {
whereToPut.add(createFactory(previousValue, it))
}
}
}
internal fun parse(incoming: String): List<CronDateTime> {
val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ")
val secondsParsed = parseSeconds(secondsSource)
val minutesParsed = parseSeconds(minutesSource)
val hoursParsed = parseSeconds(hoursSource)
val dayOfMonthParsed = parseSeconds(dayOfMonthSource)
val monthParsed = parseSeconds(monthSource)
val resultCronDateTimes = mutableListOf(CronDateTime())
secondsParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
previousCronDateTime.copy(seconds = currentTime)
}
minutesParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
previousCronDateTime.copy(minutes = currentTime)
}
hoursParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
previousCronDateTime.copy(hours = currentTime)
}
dayOfMonthParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
previousCronDateTime.copy(dayOfMonth = currentTime)
}
monthParsed ?.fillWith(resultCronDateTimes) { previousCronDateTime: CronDateTime, currentTime: Byte ->
previousCronDateTime.copy(month = currentTime)
}
return resultCronDateTimes.toList()
}

View File

@ -10,3 +10,9 @@ internal val dayOfMonthRegex = Regex("[*]|((${dayOfMonthNumberRegex.pattern})(,(
internal val hoursRegex = Regex("[*]|((${hoursNumberRegex.pattern})(,(${hoursNumberRegex.pattern})){0,23})") internal val hoursRegex = Regex("[*]|((${hoursNumberRegex.pattern})(,(${hoursNumberRegex.pattern})){0,23})")
internal val minutesRegex = Regex("[*]|((${minutesOrSecondsNumberRegex.pattern})(,(${minutesOrSecondsNumberRegex.pattern})){0,59})") internal val minutesRegex = Regex("[*]|((${minutesOrSecondsNumberRegex.pattern})(,(${minutesOrSecondsNumberRegex.pattern})){0,59})")
internal val secondsRegex = minutesRegex internal val secondsRegex = minutesRegex
internal val monthRange = 0 .. 11
internal val dayOfMonthRange = 0 .. 31
internal val hoursRange = 0 .. 23
internal val minutesRange = 0 .. 59
internal val secondsRange = minutesRange