From 476239faa691eaee4268a07e5e68801547fab384 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 2 Jan 2021 20:13:47 +0600 Subject: [PATCH] CronDateTimeScheduler now is internal (again :( ) --- CHANGELOG.md | 8 ++++ .../dev/inmo/krontab/KronSchedulersMerging.kt | 41 +++++++++++++++++++ .../collection/CollectionKronScheduler.kt | 5 +-- .../krontab/internal/CronDateTimeScheduler.kt | 24 ++++++----- 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 src/commonMain/kotlin/dev/inmo/krontab/KronSchedulersMerging.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 871dd0a..49d604a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 0.5.0 +**BREAKING CHANGES** + +* `CronDateTimeScheduler` has been marked as `internal` and no longer accessible outside of internal functions + * Old methods `merge` and `plus` related to `CronDateTimeScheduler` has been marked as `deprecated` and changed their + parameters types - it is `KronScheduler` now +* New methods `merge` has been added + + ## 0.4.2 * Versions diff --git a/src/commonMain/kotlin/dev/inmo/krontab/KronSchedulersMerging.kt b/src/commonMain/kotlin/dev/inmo/krontab/KronSchedulersMerging.kt new file mode 100644 index 0000000..f5a682f --- /dev/null +++ b/src/commonMain/kotlin/dev/inmo/krontab/KronSchedulersMerging.kt @@ -0,0 +1,41 @@ +package dev.inmo.krontab + +import dev.inmo.krontab.collection.CollectionKronScheduler +import dev.inmo.krontab.internal.CronDateTime +import dev.inmo.krontab.internal.CronDateTimeScheduler + +/** + * Create new one [CollectionKronScheduler] to include all [KronScheduler]s of [this] [Iterator] + * + * @see CollectionKronScheduler + * @see CollectionKronScheduler.include + */ +fun Iterator.merge(): KronScheduler { + val cronDateTimes = mutableListOf() + val collectionScheduler = CollectionKronScheduler() + forEach { + when (it) { + is CronDateTimeScheduler -> cronDateTimes.addAll(it.cronDateTimes) + else -> collectionScheduler.include(it) + } + } + if (cronDateTimes.isNotEmpty()) { + collectionScheduler.include(CronDateTimeScheduler(cronDateTimes)) + } + return collectionScheduler +} + +/** + * Create new one [CollectionKronScheduler] to include all [KronScheduler]s of [this] [Iterator] + * + * @see CollectionKronScheduler + * @see CollectionKronScheduler.include + */ +@Suppress("NOTHING_TO_INLINE") +inline fun Iterable.merge(): KronScheduler = iterator().merge() + +/** + * @return Vararg shortcut for [merge] + */ +@Suppress("NOTHING_TO_INLINE") +inline fun merge(vararg kronDateTimeSchedulers: KronScheduler) = kronDateTimeSchedulers.iterator().merge() diff --git a/src/commonMain/kotlin/dev/inmo/krontab/collection/CollectionKronScheduler.kt b/src/commonMain/kotlin/dev/inmo/krontab/collection/CollectionKronScheduler.kt index f5a4263..978140c 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/collection/CollectionKronScheduler.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/collection/CollectionKronScheduler.kt @@ -5,7 +5,6 @@ import dev.inmo.krontab.KronScheduler import dev.inmo.krontab.anyCronDateTime import dev.inmo.krontab.internal.* import dev.inmo.krontab.internal.CronDateTimeScheduler -import dev.inmo.krontab.internal.merge import dev.inmo.krontab.internal.toNearDateTime /** @@ -20,7 +19,7 @@ data class CollectionKronScheduler internal constructor( * Add [kronScheduler] into its [schedulers] list * * * When [kronScheduler] is [CronDateTimeScheduler] it will merge all [CronDateTimeScheduler]s from [schedulers] list - * and this [kronScheduler] using [merge] function + * and this [kronScheduler] using [mergeCronDateTimeSchedulers] function * * When [kronScheduler] is [CollectionKronScheduler] it this instance will include all [kronScheduler] * [schedulers] * * Otherwise [kronScheduler] will be added to [schedulers] list @@ -37,7 +36,7 @@ data class CollectionKronScheduler internal constructor( } } schedulers.add( - merge(resultCronDateTimes) + mergeCronDateTimeSchedulers(resultCronDateTimes) ) } is CollectionKronScheduler -> kronScheduler.schedulers.forEach { diff --git a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt index 6f8fcd8..485fb3c 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt @@ -3,6 +3,7 @@ package dev.inmo.krontab.internal import com.soywiz.klock.DateTime import dev.inmo.krontab.KronScheduler import dev.inmo.krontab.anyCronDateTime +import dev.inmo.krontab.collection.plus /** * Cron-oriented realisation of [KronScheduler] @@ -17,8 +18,7 @@ import dev.inmo.krontab.anyCronDateTime * @see dev.inmo.krontab.builder.buildSchedule * @see dev.inmo.krontab.builder.SchedulerBuilder */ -@Deprecated("This class will get internal status in future") -data class CronDateTimeScheduler internal constructor( +internal data class CronDateTimeScheduler internal constructor( internal val cronDateTimes: List ) : KronScheduler { /** @@ -31,22 +31,26 @@ data class CronDateTimeScheduler internal constructor( } } +internal fun mergeCronDateTimeSchedulers(schedulers: List) = CronDateTimeScheduler( + schedulers.flatMap { it.cronDateTimes } +) + /** * @return New instance of [CronDateTimeScheduler] with all unique [CronDateTimeScheduler.cronDateTimes] of * [kronDateTimeSchedulers] included */ -@Suppress("NOTHING_TO_INLINE") -fun merge(kronDateTimeSchedulers: List) = CronDateTimeScheduler( - kronDateTimeSchedulers.flatMap { it.cronDateTimes }.distinct() -) +@Deprecated("Will be removed in next major release", ReplaceWith("merge", "dev.inmo.krontab")) +fun merge(kronSchedulers: List) = kronSchedulers.apply { dev.inmo.krontab.merge() } /** - * @return Vararg shortcyut for [merge] + * @return Vararg shortcut for [dev.inmo.krontab.merge] */ @Suppress("NOTHING_TO_INLINE") -inline fun merge(vararg kronDateTimeSchedulers: CronDateTimeScheduler) = merge(kronDateTimeSchedulers.toList()) +@Deprecated("Will be removed in next major release", ReplaceWith("merge", "dev.inmo.krontab")) +inline fun merge(vararg kronDateTimeSchedulers: KronScheduler) = kronDateTimeSchedulers.apply { dev.inmo.krontab.merge() } /** - * Use [merge] operation to internalcreate new [CronDateTimeScheduler] with all [CronDateTimeScheduler.cronDateTimes] + * @return Vararg shortcut for [dev.inmo.krontab.merge] */ @Suppress("NOTHING_TO_INLINE") -inline fun CronDateTimeScheduler.plus(other: CronDateTimeScheduler) = merge(this, other) +@Deprecated("Will be removed in next major release", ReplaceWith("merge", "dev.inmo.krontab")) +inline fun KronScheduler.plus(other: KronScheduler) = this + other