2020-11-21 08:58:19 +00:00
|
|
|
package dev.inmo.krontab.builder
|
2019-10-08 16:06:26 +00:00
|
|
|
|
2023-05-25 14:53:10 +00:00
|
|
|
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
|
2019-10-08 16:06:26 +00:00
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Will help to create an instance of [KronScheduler]
|
|
|
|
*
|
2020-11-21 08:58:19 +00:00
|
|
|
* @see dev.inmo.krontab.createSimpleScheduler
|
2020-06-03 15:21:52 +00:00
|
|
|
*/
|
2023-06-20 04:12:32 +00:00
|
|
|
inline fun buildSchedule(settingsBlock: SchedulerBuilder.() -> Unit): KronScheduler {
|
2019-10-08 16:06:26 +00:00
|
|
|
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
|
|
|
|
*/
|
2023-06-20 04:12:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-20 04:12:32 +00:00
|
|
|
/**
|
|
|
|
* Creates new [KronScheduler] with [settingsBlock]
|
|
|
|
*
|
2023-06-20 04:41:46 +00:00
|
|
|
* Since it is inline function, you may break execution of [settingsBlock]
|
2023-06-20 04:12:32 +00:00
|
|
|
* 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]
|
2023-06-20 04:12:32 +00:00
|
|
|
* at any time
|
|
|
|
*/
|
|
|
|
inline operator fun KronScheduler.Companion.invoke(
|
|
|
|
settingsBlock: SchedulerBuilder.() -> Unit
|
|
|
|
): KronScheduler = buildSchedule(settingsBlock)
|
|
|
|
|
2019-10-08 16:06:26 +00:00
|
|
|
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,
|
2021-01-02 15:35:08 +00:00
|
|
|
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
|
2019-10-08 16:06:26 +00:00
|
|
|
) {
|
2021-01-02 15:35:08 +00:00
|
|
|
private fun <I, T : TimeBuilder<I>> callAndReturn(
|
|
|
|
initial: Array<I>?,
|
2019-10-08 16:06:26 +00:00
|
|
|
builder: T,
|
|
|
|
block: T.() -> Unit
|
2021-01-02 15:35:08 +00:00
|
|
|
): List<I>? {
|
2019-10-08 16:06:26 +00:00
|
|
|
builder.block()
|
|
|
|
|
|
|
|
val builderValue = builder.build()
|
|
|
|
|
|
|
|
return initial ?.let {
|
|
|
|
builderValue ?.let { _ ->
|
2021-01-02 15:35:08 +00:00
|
|
|
(it + builderValue).distinct()
|
2019-10-08 16:06:26 +00:00
|
|
|
} ?: 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()
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Starts an seconds block
|
|
|
|
*/
|
2019-10-08 16:06:26 +00:00
|
|
|
fun seconds(block: SecondsBuilder.() -> Unit) {
|
|
|
|
seconds = callAndReturn(
|
|
|
|
seconds,
|
|
|
|
SecondsBuilder(),
|
|
|
|
block
|
2021-01-02 15:35:08 +00:00
|
|
|
) ?.toTypedArray()
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Starts an minutes block
|
|
|
|
*/
|
2019-10-08 16:06:26 +00:00
|
|
|
fun minutes(block: MinutesBuilder.() -> Unit) {
|
|
|
|
minutes = callAndReturn(
|
|
|
|
minutes,
|
|
|
|
MinutesBuilder(),
|
|
|
|
block
|
2021-01-02 15:35:08 +00:00
|
|
|
) ?.toTypedArray()
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Starts an hours block
|
|
|
|
*/
|
2019-10-08 16:06:26 +00:00
|
|
|
fun hours(block: HoursBuilder.() -> Unit) {
|
|
|
|
hours = callAndReturn(
|
|
|
|
hours,
|
|
|
|
HoursBuilder(),
|
|
|
|
block
|
2021-01-02 15:35:08 +00:00
|
|
|
) ?.toTypedArray()
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Starts an days of month block
|
|
|
|
*/
|
2019-10-08 16:06:26 +00:00
|
|
|
fun dayOfMonth(block: DaysOfMonthBuilder.() -> Unit) {
|
|
|
|
dayOfMonth = callAndReturn(
|
|
|
|
dayOfMonth,
|
|
|
|
DaysOfMonthBuilder(),
|
|
|
|
block
|
2021-01-02 15:35:08 +00:00
|
|
|
) ?.toTypedArray()
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 05:58:19 +00:00
|
|
|
/**
|
|
|
|
* Starts an hours block
|
|
|
|
*/
|
|
|
|
fun dayOfWeek(block: WeekDaysBuilder.() -> Unit) {
|
|
|
|
dayOfWeek = callAndReturn(
|
|
|
|
dayOfWeek,
|
|
|
|
WeekDaysBuilder(),
|
|
|
|
block
|
|
|
|
) ?.toTypedArray()
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Starts an months block
|
|
|
|
*/
|
2019-10-08 16:06:26 +00:00
|
|
|
fun months(block: MonthsBuilder.() -> Unit) {
|
|
|
|
month = callAndReturn(
|
|
|
|
month,
|
|
|
|
MonthsBuilder(),
|
|
|
|
block
|
2021-01-02 15:35:08 +00:00
|
|
|
) ?.toTypedArray()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts an year block
|
|
|
|
*/
|
|
|
|
fun years(block: YearsBuilder.() -> Unit) {
|
|
|
|
year = callAndReturn(
|
|
|
|
year,
|
|
|
|
YearsBuilder(),
|
|
|
|
block
|
|
|
|
) ?.toTypedArray()
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* @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
|
2020-06-03 15:21:52 +00:00
|
|
|
*/
|
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)
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|