krontab/src/commonMain/kotlin/builder/SchedulerBuilder.kt

197 lines
4.9 KiB
Kotlin
Raw Normal View History

2020-11-21 08:58:19 +00:00
package dev.inmo.krontab.builder
import korlibs.time.TimezoneOffset
import korlibs.time.minutes
2020-11-21 08:58:19 +00:00
import dev.inmo.krontab.KronScheduler
2021-04-09 18:28:08 +00:00
import dev.inmo.krontab.KronSchedulerTz
2022-04-29 15:57:10 +00:00
import dev.inmo.krontab.internal.*
2020-11-21 08:58:19 +00:00
import dev.inmo.krontab.internal.createKronScheduler
2021-04-09 18:28:08 +00:00
import dev.inmo.krontab.internal.createKronSchedulerWithOffset
2021-03-30 08:07:39 +00:00
import dev.inmo.krontab.utils.Minutes
/**
* Will help to create an instance of [KronScheduler]
*
2020-11-21 08:58:19 +00:00
* @see dev.inmo.krontab.createSimpleScheduler
*/
inline fun buildSchedule(settingsBlock: SchedulerBuilder.() -> Unit): KronScheduler {
val builder = SchedulerBuilder()
builder.settingsBlock()
return builder.build()
}
2021-04-09 18:28:08 +00:00
/**
* Will help to create an instance of [KronScheduler]
*
* @see dev.inmo.krontab.createSimpleScheduler
*/
inline fun buildSchedule(
2021-04-09 18:28:08 +00:00
offset: Minutes,
settingsBlock: SchedulerBuilder.() -> Unit
): KronSchedulerTz {
val builder = SchedulerBuilder(offset = offset)
builder.settingsBlock()
return builder.build() as KronSchedulerTz
}
/**
* Creates new [KronScheduler] with [settingsBlock]
*
2023-06-20 04:41:46 +00:00
* Since it is inline function, you may break execution of [settingsBlock]
* at any time
*/
inline operator fun KronScheduler.Companion.invoke(
offset: Minutes,
settingsBlock: SchedulerBuilder.() -> Unit
): KronSchedulerTz = buildSchedule(offset, settingsBlock)
/**
* Creates new [KronScheduler] with [settingsBlock]
*
2023-06-20 04:41:46 +00:00
* Since it is inline function, you may break execution of [settingsBlock]
* at any time
*/
inline operator fun KronScheduler.Companion.invoke(
settingsBlock: SchedulerBuilder.() -> Unit
): KronScheduler = buildSchedule(settingsBlock)
class SchedulerBuilder(
private var seconds: Array<Byte>? = null,
private var minutes: Array<Byte>? = null,
private var hours: Array<Byte>? = null,
private var dayOfMonth: Array<Byte>? = null,
private var month: Array<Byte>? = null,
2021-03-30 08:07:39 +00:00
private var year: Array<Int>? = null,
2021-04-22 05:58:19 +00:00
private var dayOfWeek: Array<Byte>? = null,
2022-04-29 15:57:10 +00:00
private val offset: Minutes? = null,
private var milliseconds: Array<Short>? = null
) {
private fun <I, T : TimeBuilder<I>> callAndReturn(
initial: Array<I>?,
builder: T,
block: T.() -> Unit
): List<I>? {
builder.block()
val builderValue = builder.build()
return initial ?.let {
builderValue ?.let { _ ->
(it + builderValue).distinct()
} ?: builderValue
} ?: builderValue
}
2022-04-29 15:57:10 +00:00
/**
* Starts an milliseconds block
*/
fun milliseconds(block: MillisecondsBuilder.() -> Unit) {
milliseconds = callAndReturn(
milliseconds,
MillisecondsBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an seconds block
*/
fun seconds(block: SecondsBuilder.() -> Unit) {
seconds = callAndReturn(
seconds,
SecondsBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an minutes block
*/
fun minutes(block: MinutesBuilder.() -> Unit) {
minutes = callAndReturn(
minutes,
MinutesBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an hours block
*/
fun hours(block: HoursBuilder.() -> Unit) {
hours = callAndReturn(
hours,
HoursBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an days of month block
*/
fun dayOfMonth(block: DaysOfMonthBuilder.() -> Unit) {
dayOfMonth = callAndReturn(
dayOfMonth,
DaysOfMonthBuilder(),
block
) ?.toTypedArray()
}
2021-04-22 05:58:19 +00:00
/**
* Starts an hours block
*/
fun dayOfWeek(block: WeekDaysBuilder.() -> Unit) {
dayOfWeek = callAndReturn(
dayOfWeek,
WeekDaysBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an months block
*/
fun months(block: MonthsBuilder.() -> Unit) {
month = callAndReturn(
month,
MonthsBuilder(),
block
) ?.toTypedArray()
}
/**
* Starts an year block
*/
fun years(block: YearsBuilder.() -> Unit) {
year = callAndReturn(
year,
YearsBuilder(),
block
) ?.toTypedArray()
}
/**
* @return Completely built and independent [KronScheduler]
*
2020-11-21 08:58:19 +00:00
* @see dev.inmo.krontab.createSimpleScheduler
* @see dev.inmo.krontab.internal.createKronScheduler
*/
2021-04-09 18:28:08 +00:00
fun build(): KronScheduler = offset ?.let {
2022-04-29 15:57:10 +00:00
createKronSchedulerWithOffset(
seconds,
minutes,
hours,
dayOfMonth,
month,
year,
dayOfWeek,
TimezoneOffset(it.minutes),
milliseconds ?: millisecondsArrayDefault
)
} ?: createKronScheduler(seconds, minutes, hours, dayOfMonth, month, year, dayOfWeek, milliseconds ?: millisecondsArrayDefault)
}