krontab/src/commonMain/kotlin/dev/inmo/krontab/KronSchedulersMerging.kt

47 lines
1.6 KiB
Kotlin
Raw Normal View History

package dev.inmo.krontab
import dev.inmo.krontab.collection.CollectionKronScheduler
2021-04-09 18:28:08 +00:00
import dev.inmo.krontab.collection.includeAll
import dev.inmo.krontab.internal.*
/**
* Create new one [CollectionKronScheduler] to include all [KronScheduler]s of [this] [Iterator]
*
* @see CollectionKronScheduler
* @see CollectionKronScheduler.include
*/
2021-04-09 18:28:08 +00:00
fun Iterator<KronScheduler>.merge(): CollectionKronScheduler {
val cronDateTimes = mutableListOf<CronDateTime>()
2021-04-09 18:28:08 +00:00
val timezonedCronDateTimes = mutableListOf<CronDateTimeSchedulerTz>()
val collectionScheduler = CollectionKronScheduler()
forEach {
when (it) {
is CronDateTimeScheduler -> cronDateTimes.add(it.cronDateTime)
2021-04-09 18:28:08 +00:00
is CronDateTimeSchedulerTz -> timezonedCronDateTimes.add(it)
else -> collectionScheduler.include(it)
}
}
if (cronDateTimes.isNotEmpty()) {
collectionScheduler.include(CronDateTimeScheduler(cronDateTimes.merge()))
}
2021-04-09 18:28:08 +00:00
if (timezonedCronDateTimes.isNotEmpty()) {
collectionScheduler.includeAll(mergeCronDateTimeSchedulers(timezonedCronDateTimes))
}
return collectionScheduler
}
/**
* Create new one [CollectionKronScheduler] to include all [KronScheduler]s of [this] [Iterator]
*
* @see CollectionKronScheduler
* @see CollectionKronScheduler.include
*/
@Suppress("NOTHING_TO_INLINE")
2021-04-09 18:28:08 +00:00
inline fun Iterable<KronScheduler>.merge(): CollectionKronScheduler = iterator().merge()
/**
* @return Vararg shortcut for [merge]
*/
@Suppress("NOTHING_TO_INLINE")
2021-04-09 18:28:08 +00:00
inline fun merge(vararg kronDateTimeSchedulers: KronScheduler): CollectionKronScheduler = kronDateTimeSchedulers.iterator().merge()