2019-10-08 17:17:51 +00:00
|
|
|
package com.insanusmokrassar.krontab.builder
|
2019-10-08 16:06:26 +00:00
|
|
|
|
2019-10-10 10:43:52 +00:00
|
|
|
import com.insanusmokrassar.krontab.KronScheduler
|
2020-06-03 15:21:52 +00:00
|
|
|
import com.insanusmokrassar.krontab.internal.*
|
2019-10-08 17:17:51 +00:00
|
|
|
import com.insanusmokrassar.krontab.internal.CronDateTime
|
2020-06-03 15:21:52 +00:00
|
|
|
import com.insanusmokrassar.krontab.internal.CronDateTimeScheduler
|
2019-10-08 17:17:51 +00:00
|
|
|
import com.insanusmokrassar.krontab.internal.fillWith
|
2019-10-08 16:06:26 +00:00
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Will help to create an instance of [KronScheduler]
|
|
|
|
*
|
|
|
|
* @see com.insanusmokrassar.krontab.createSimpleScheduler
|
|
|
|
*/
|
2019-10-10 10:43:52 +00:00
|
|
|
fun buildSchedule(settingsBlock: SchedulerBuilder.() -> Unit): KronScheduler {
|
2019-10-08 16:06:26 +00:00
|
|
|
val builder = SchedulerBuilder()
|
|
|
|
|
|
|
|
builder.settingsBlock()
|
|
|
|
|
|
|
|
return builder.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
private fun <T : TimeBuilder> callAndReturn(
|
|
|
|
initial: Array<Byte>?,
|
|
|
|
builder: T,
|
|
|
|
block: T.() -> Unit
|
|
|
|
): Array<Byte>? {
|
|
|
|
builder.block()
|
|
|
|
|
|
|
|
val builderValue = builder.build()
|
|
|
|
|
|
|
|
return initial ?.let {
|
|
|
|
builderValue ?.let { _ ->
|
|
|
|
(it + builderValue).distinct().toTypedArray()
|
|
|
|
} ?: builderValue
|
|
|
|
} ?: builderValue
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* @return Completely built and independent [KronScheduler]
|
|
|
|
*
|
|
|
|
* @see com.insanusmokrassar.krontab.createSimpleScheduler
|
|
|
|
* @see com.insanusmokrassar.krontab.internal.createKronScheduler
|
|
|
|
*/
|
|
|
|
fun build(): KronScheduler = createKronScheduler(seconds, minutes, hours, dayOfMonth, month)
|
2019-10-08 16:06:26 +00:00
|
|
|
}
|