Skip to content

Commit 6365411

Browse files
Sort PulseSchedule values a single time
1 parent a3cf356 commit 6365411

2 files changed

Lines changed: 52 additions & 45 deletions

File tree

src/commonMain/kotlin/io/github/kevincianfarini/cardiologist/PulseSchedule.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public class PulseSchedule(
3636
// Don't check if onDaysOfWeek is empty because an empty set is equivalent to the wildcard `*` value in cron
3737
// expressions.
3838
}
39+
40+
internal val sortedSeconds = atSeconds.sorted()
41+
internal val sortedMinutes = atMinutes.sorted()
42+
internal val sortedHours = atHours.sorted()
43+
internal val sortedDaysOfMonth = onDaysOfMonth.sorted()
44+
internal val sortedMonths = inMonths.sorted()
45+
internal val sortedDaysOfWeek = onDaysOfWeek.sorted()
3946
}
4047

4148
public class PulseScheduleBuilder internal constructor() {

src/commonMain/kotlin/io/github/kevincianfarini/cardiologist/impl/LocalDateTime.kt

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ internal fun LocalDateTime.nextMatch(schedule: PulseSchedule): LocalDateTime {
1717
// nanosecond component by one to ensure that we produce a match that's distinct from this value.
1818
val time = if (matches(schedule)) copy(nanosecond = 1) else this
1919
return with(schedule) {
20-
time.nextMonth(inMonths)
21-
.nextDay(onDaysOfMonth, onDaysOfWeek,inMonths)
22-
.nextHour(atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
23-
.nextMinute(atMinutes, atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
24-
.nextSecond(atSeconds, atMinutes, atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
20+
time.nextMonth(sortedMonths)
21+
.nextDay(sortedDaysOfMonth, sortedDaysOfWeek, sortedMonths)
22+
.nextHour(sortedHours, sortedDaysOfMonth, sortedDaysOfWeek, sortedMonths)
23+
.nextMinute(sortedMinutes, sortedHours, sortedDaysOfMonth, sortedDaysOfWeek, sortedMonths)
24+
.nextSecond(sortedSeconds, sortedMinutes, sortedHours, sortedDaysOfMonth, sortedDaysOfWeek, sortedMonths)
2525
}
2626
}
2727

2828
private fun LocalDateTime.nextMonth(
29-
inMonths: Set<Month>,
29+
inMonths: List<Month>,
3030
increment: Boolean = false,
3131
): LocalDateTime {
3232
val incrementedMonth = if (increment) month.inc() else month
33-
val minMonth = inMonths.minOrNull()!!
34-
val maxMonth = inMonths.maxOrNull()!!
33+
val minMonth = inMonths.first()
34+
val maxMonth = inMonths.last()
3535
return when {
3636
incrementedMonth < month -> copy(year = year + 1, month = incrementedMonth.number)
3737
incrementedMonth in inMonths -> copy(month = incrementedMonth.number)
@@ -53,7 +53,7 @@ private fun LocalDateTime.nextMonth(
5353
nanosecond = 0
5454
)
5555
else -> copy(
56-
month = inMonths.sorted().first { it > incrementedMonth }.number,
56+
month = inMonths.first { it > incrementedMonth }.number,
5757
day = 1,
5858
hour = 0,
5959
minute = 0,
@@ -64,9 +64,9 @@ private fun LocalDateTime.nextMonth(
6464
}
6565

6666
private fun LocalDateTime.nextDay(
67-
onDaysOfMonth: Set<Int>,
68-
onDaysOfWeek: Set<DayOfWeek>,
69-
inMonths: Set<Month>,
67+
onDaysOfMonth: List<Int>,
68+
onDaysOfWeek: List<DayOfWeek>,
69+
inMonths: List<Month>,
7070
increment: Boolean = false,
7171
): LocalDateTime = when {
7272
onDaysOfMonth != WILDCARD_DAYS_OF_MONTH && onDaysOfWeek.isNotEmpty() -> {
@@ -83,12 +83,12 @@ private fun LocalDateTime.nextDay(
8383
}
8484

8585
private fun LocalDateTime.nextDayOfMonth(
86-
onDaysOfMonth: Set<Int>,
87-
inMonths: Set<Month>,
86+
onDaysOfMonth: List<Int>,
87+
inMonths: List<Month>,
8888
increment: Boolean = false,
8989
): LocalDateTime {
90-
val minDayOfMonth = onDaysOfMonth.minOrNull()!!
91-
val maxDayOfMonth = onDaysOfMonth.maxOrNull()!!
90+
val minDayOfMonth = onDaysOfMonth.first()
91+
val maxDayOfMonth = onDaysOfMonth.last()
9292
val incrementedDay = when {
9393
increment && day + 1 <= month.numberOfDays(year) -> day + 1
9494
increment -> 1
@@ -108,7 +108,7 @@ private fun LocalDateTime.nextDayOfMonth(
108108
nextMonth(inMonths, increment = true).nextDayOfMonth(onDaysOfMonth, inMonths)
109109
}
110110
else -> copy(
111-
day = onDaysOfMonth.sorted().first { it > incrementedDay },
111+
day = onDaysOfMonth.first { it > incrementedDay },
112112
hour = 0,
113113
minute = 0,
114114
second = 0,
@@ -118,8 +118,8 @@ private fun LocalDateTime.nextDayOfMonth(
118118
}
119119

120120
private fun LocalDateTime.nextDayOfWeek(
121-
onDaysOfWeek: Set<DayOfWeek>,
122-
inMonths: Set<Month>,
121+
onDaysOfWeek: List<DayOfWeek>,
122+
inMonths: List<Month>,
123123
increment: Boolean = false,
124124
): LocalDateTime {
125125
return when {
@@ -153,14 +153,14 @@ private fun LocalDateTime.nextDayOfWeek(
153153
}
154154

155155
private fun LocalDateTime.nextHour(
156-
atHours: Set<Int>,
157-
onDaysOfMonth: Set<Int>,
158-
onDaysOfWeek: Set<DayOfWeek>,
159-
inMonths: Set<Month>,
156+
atHours: List<Int>,
157+
onDaysOfMonth: List<Int>,
158+
onDaysOfWeek: List<DayOfWeek>,
159+
inMonths: List<Month>,
160160
increment: Boolean = false,
161161
): LocalDateTime {
162-
val minHour = atHours.minOrNull()!!
163-
val maxHour = atHours.maxOrNull()!!
162+
val minHour = atHours.first()
163+
val maxHour = atHours.last()
164164
val incrementedHour = if (increment) (hour + 1) % 24 else hour
165165
return when {
166166
incrementedHour < hour -> nextDay(onDaysOfMonth, onDaysOfWeek, inMonths, increment = true).copy(hour = incrementedHour)
@@ -173,7 +173,7 @@ private fun LocalDateTime.nextHour(
173173
nanosecond = 0,
174174
)
175175
else -> copy(
176-
hour = atHours.sorted().first { it > incrementedHour },
176+
hour = atHours.first { it > incrementedHour },
177177
minute = 0,
178178
second = 0,
179179
nanosecond = 0,
@@ -182,15 +182,15 @@ private fun LocalDateTime.nextHour(
182182
}
183183

184184
private fun LocalDateTime.nextMinute(
185-
atMinutes: Set<Int>,
186-
atHours: Set<Int>,
187-
onDaysOfMonth: Set<Int>,
188-
onDaysOfWeek: Set<DayOfWeek>,
189-
inMonths: Set<Month>,
185+
atMinutes: List<Int>,
186+
atHours: List<Int>,
187+
onDaysOfMonth: List<Int>,
188+
onDaysOfWeek: List<DayOfWeek>,
189+
inMonths: List<Month>,
190190
increment: Boolean = false,
191191
): LocalDateTime {
192-
val minMinute = atMinutes.minOrNull()!!
193-
val maxMinute = atMinutes.maxOrNull()!!
192+
val minMinute = atMinutes.first()
193+
val maxMinute = atMinutes.last()
194194
val incrementedMinute = if (increment) (minute + 1) % 60 else minute
195195
return when {
196196
incrementedMinute < minute -> nextHour(atHours, onDaysOfMonth, onDaysOfWeek, inMonths, increment = true).copy(
@@ -204,23 +204,23 @@ private fun LocalDateTime.nextMinute(
204204
nanosecond = 0,
205205
)
206206
else -> copy(
207-
minute = atMinutes.sorted().first { it > incrementedMinute },
207+
minute = atMinutes.first { it > incrementedMinute },
208208
second = 0,
209209
nanosecond = 0,
210210
)
211211
}
212212
}
213213

214214
private fun LocalDateTime.nextSecond(
215-
atSeconds: Set<Int>,
216-
atMinutes: Set<Int>,
217-
atHours: Set<Int>,
218-
onDaysOfMonth: Set<Int>,
219-
onDaysOfWeek: Set<DayOfWeek>,
220-
inMonths: Set<Month>,
215+
atSeconds: List<Int>,
216+
atMinutes: List<Int>,
217+
atHours: List<Int>,
218+
onDaysOfMonth: List<Int>,
219+
onDaysOfWeek: List<DayOfWeek>,
220+
inMonths: List<Month>,
221221
): LocalDateTime {
222-
val minSecond = atSeconds.minOrNull()!!
223-
val maxSeconds = atSeconds.maxOrNull()!!
222+
val minSecond = atSeconds.first()
223+
val maxSeconds = atSeconds.last()
224224
val incrementedSecond = if (nanosecond > 0) (second + 1) % 60 else second
225225
return when {
226226
incrementedSecond < second -> nextMinute(atMinutes, atHours, onDaysOfMonth, onDaysOfWeek, inMonths, increment = true).copy(
@@ -236,7 +236,7 @@ private fun LocalDateTime.nextSecond(
236236
inMonths,
237237
increment = true,
238238
).copy(second = minSecond)
239-
else -> copy(second = atSeconds.sorted().first { it > incrementedSecond })
239+
else -> copy(second = atSeconds.first { it > incrementedSecond })
240240
}.copy(nanosecond = 0)
241241
}
242242

@@ -299,11 +299,11 @@ private fun DayOfWeek.daysUntil(other: DayOfWeek): Int = when {
299299
else -> 7 - (ordinal - other.ordinal)
300300
}
301301

302-
private fun DayOfWeek.incrementUntilMatch(matches: Set<DayOfWeek>): DayOfWeek {
302+
private fun DayOfWeek.incrementUntilMatch(matches: List<DayOfWeek>): DayOfWeek {
303303
var day = this
304304
while (day !in matches) { day++ }
305305
return day
306306
}
307307

308308
private val DISTANT_LOCAL_FUTURE = LocalDateTime(100_000, 1, 1, 0, 0)
309-
private val WILDCARD_DAYS_OF_MONTH: Set<Int> = (1..31).toSet()
309+
private val WILDCARD_DAYS_OF_MONTH: List<Int> = (1..31).toList()

0 commit comments

Comments
 (0)