CollectionKronScheduler

This commit is contained in:
InsanusMokrassar 2020-12-06 01:00:57 +06:00
parent a38c233bbf
commit 3c0818cabf
3 changed files with 68 additions and 0 deletions

View File

@ -2,6 +2,8 @@
## 0.4.1 ## 0.4.1
* Add `CollectionKronScheduler`. It will give opportunity to unite several schedulers in one
## 0.4.0 ## 0.4.0
**BREAKING CHANGES** **BREAKING CHANGES**

View File

@ -0,0 +1,25 @@
package dev.inmo.krontab.collection
import dev.inmo.krontab.KronScheduler
@Suppress("NOTHING_TO_INLINE")
inline fun CollectionKronScheduler.includeAll(kronSchedulers: List<KronScheduler>) {
kronSchedulers.forEach {
include(it)
}
}
@Suppress("NOTHING_TO_INLINE")
inline fun CollectionKronScheduler.includeAll(vararg kronSchedulers: KronScheduler) {
includeAll(kronSchedulers.toList())
}
operator fun KronScheduler.plus(kronScheduler: KronScheduler): CollectionKronScheduler {
return CollectionKronScheduler().apply {
includeAll(this, kronScheduler)
}
}
operator fun CollectionKronScheduler.plusAssign(kronScheduler: KronScheduler) {
include(kronScheduler)
}

View File

@ -0,0 +1,41 @@
package dev.inmo.krontab.collection
import com.soywiz.klock.DateTime
import dev.inmo.krontab.KronScheduler
import dev.inmo.krontab.anyCronDateTime
import dev.inmo.krontab.internal.CronDateTimeScheduler
import dev.inmo.krontab.internal.toNearDateTime
data class CollectionKronScheduler private constructor(
internal val schedulers: MutableList<KronScheduler>
) : KronScheduler {
internal constructor(schedulers: List<KronScheduler>) : this(schedulers.toMutableList())
internal constructor() : this(mutableListOf())
fun include(kronScheduler: KronScheduler) {
when (kronScheduler) {
is CronDateTimeScheduler -> {
val resultCronDateTimes = kronScheduler.cronDateTimes.toMutableList()
schedulers.removeAll {
if (it is CronDateTimeScheduler) {
resultCronDateTimes.addAll(it.cronDateTimes)
true
} else {
false
}
}
schedulers.add(
CronDateTimeScheduler(resultCronDateTimes.distinct())
)
}
is CollectionKronScheduler -> kronScheduler.schedulers.forEach {
include(it)
}
else -> schedulers.add(kronScheduler)
}
}
override suspend fun next(relatively: DateTime): DateTime {
return schedulers.minOfOrNull { it.next(relatively) } ?: anyCronDateTime.toNearDateTime(relatively)
}
}