From bee766dc8674977e5637b0a5e4dff9264f84bdb9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 5 Oct 2019 16:40:51 +0600 Subject: [PATCH] replacing of internal usages --- .../krontab/CronDateTimeScheduler.kt | 37 +++++++++ .../insanusmokrassar/krontab/CronParser.kt | 81 ------------------- .../krontab/{ => internal}/CronDateTime.kt | 0 .../krontab/internal/Parser.kt | 48 +++++++++++ .../Ranges.kt} | 0 5 files changed, 85 insertions(+), 81 deletions(-) delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronParser.kt rename src/commonMain/kotlin/com/github/insanusmokrassar/krontab/{ => internal}/CronDateTime.kt (100%) create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Parser.kt rename src/commonMain/kotlin/com/github/insanusmokrassar/krontab/{InternalCronRanges.kt => internal/Ranges.kt} (100%) diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt index 8d093af..3290e86 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTimeScheduler.kt @@ -7,6 +7,8 @@ private val anyCronDateTime by lazy { CronDateTime() } +val AnyTimeScheduler = CronDateTimeScheduler(listOf(anyCronDateTime)) + data class CronDateTimeScheduler internal constructor( internal val cronDateTimes: List ) @@ -20,3 +22,38 @@ suspend fun CronDateTimeScheduler.doInLoop(block: suspend () -> Boolean) { delay(next().unixMillisLong - DateTime.now().unixMillisLong) } while (block()) } + + +fun createCronDateTimeScheduler(incoming: String): CronDateTimeScheduler { + val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ") + + val secondsParsed = parseSeconds(secondsSource) + val minutesParsed = parseMinutes(minutesSource) + val hoursParsed = parseHours(hoursSource) + val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource) + val monthParsed = parseMonths(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 CronDateTimeScheduler(resultCronDateTimes.toList()) +} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronParser.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronParser.kt deleted file mode 100644 index a8be850..0000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronParser.kt +++ /dev/null @@ -1,81 +0,0 @@ -package com.github.insanusmokrassar.krontab - -import com.github.insanusmokrassar.krontab.utils.* - -private fun createCronDateTimeScheduler(from: String, dataRange: IntRange): Array? { - 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) = createCronDateTimeScheduler(from, monthRange) -private fun parseDaysOfMonth(from: String) = createCronDateTimeScheduler(from, dayOfMonthRange) -private fun parseHours(from: String) = createCronDateTimeScheduler(from, hoursRange) -private fun parseMinutes(from: String) = createCronDateTimeScheduler(from, minutesRange) -private fun parseSeconds(from: String) = createCronDateTimeScheduler(from, secondsRange) - -private fun Array.fillWith( - whereToPut: MutableList, - createFactory: (CronDateTime, Byte) -> CronDateTime -) { - val previousValues = whereToPut.toList() - - whereToPut.clear() - - previousValues.forEach { previousValue -> - forEach { - whereToPut.add(createFactory(previousValue, it)) - } - } -} - -fun createCronDateTimeScheduler(incoming: String): CronDateTimeScheduler { - val (secondsSource, minutesSource, hoursSource, dayOfMonthSource, monthSource) = incoming.split(" ") - - val secondsParsed = parseSeconds(secondsSource) - val minutesParsed = parseMinutes(minutesSource) - val hoursParsed = parseHours(hoursSource) - val dayOfMonthParsed = parseDaysOfMonth(dayOfMonthSource) - val monthParsed = parseMonths(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 CronDateTimeScheduler(resultCronDateTimes.toList()) -} diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/CronDateTime.kt similarity index 100% rename from src/commonMain/kotlin/com/github/insanusmokrassar/krontab/CronDateTime.kt rename to src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/CronDateTime.kt diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Parser.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Parser.kt new file mode 100644 index 0000000..c6122fc --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Parser.kt @@ -0,0 +1,48 @@ +package com.github.insanusmokrassar.krontab + +import com.github.insanusmokrassar.krontab.utils.* + +private fun createCronDateTimeScheduler(from: String, dataRange: IntRange): Array? { + 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() +} + +internal fun parseMonths(from: String) = createCronDateTimeScheduler(from, monthRange) +internal fun parseDaysOfMonth(from: String) = createCronDateTimeScheduler(from, dayOfMonthRange) +internal fun parseHours(from: String) = createCronDateTimeScheduler(from, hoursRange) +internal fun parseMinutes(from: String) = createCronDateTimeScheduler(from, minutesRange) +internal fun parseSeconds(from: String) = createCronDateTimeScheduler(from, secondsRange) + +internal fun Array.fillWith( + whereToPut: MutableList, + createFactory: (CronDateTime, Byte) -> CronDateTime +) { + val previousValues = whereToPut.toList() + + whereToPut.clear() + + previousValues.forEach { previousValue -> + forEach { + whereToPut.add(createFactory(previousValue, it)) + } + } +} + diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/InternalCronRanges.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Ranges.kt similarity index 100% rename from src/commonMain/kotlin/com/github/insanusmokrassar/krontab/InternalCronRanges.kt rename to src/commonMain/kotlin/com/github/insanusmokrassar/krontab/internal/Ranges.kt