Skip to content

Commit 74f3ba9

Browse files
Sort PulseSchedule values a single time (#156)
1 parent a3cf356 commit 74f3ba9

4 files changed

Lines changed: 85 additions & 65 deletions

File tree

api/cardiologist.api

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public final class io/github/kevincianfarini/cardiologist/PulseBackpressureStrat
2828
public final class io/github/kevincianfarini/cardiologist/PulseSchedule {
2929
public fun <init> (Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;)V
3030
public fun equals (Ljava/lang/Object;)Z
31-
public final fun getAtHours ()Ljava/util/Set;
32-
public final fun getAtMinutes ()Ljava/util/Set;
33-
public final fun getAtSeconds ()Ljava/util/Set;
34-
public final fun getInMonths ()Ljava/util/Set;
35-
public final fun getOnDaysOfMonth ()Ljava/util/Set;
36-
public final fun getOnDaysOfWeek ()Ljava/util/Set;
31+
public final fun getAtHours ()Ljava/util/List;
32+
public final fun getAtMinutes ()Ljava/util/List;
33+
public final fun getAtSeconds ()Ljava/util/List;
34+
public final fun getInMonths ()Ljava/util/List;
35+
public final fun getOnDaysOfMonth ()Ljava/util/List;
36+
public final fun getOnDaysOfWeek ()Ljava/util/List;
3737
public fun hashCode ()I
3838
public fun toString ()Ljava/lang/String;
3939
}

api/cardiologist.klib.api

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ final class io.github.kevincianfarini.cardiologist/PulseSchedule { // io.github.
2323
constructor <init>(kotlin.collections/Set<kotlin/Int>, kotlin.collections/Set<kotlin/Int>, kotlin.collections/Set<kotlin/Int>, kotlin.collections/Set<kotlin/Int>, kotlin.collections/Set<kotlinx.datetime/Month>, kotlin.collections/Set<kotlinx.datetime/DayOfWeek>) // io.github.kevincianfarini.cardiologist/PulseSchedule.<init>|<init>(kotlin.collections.Set<kotlin.Int>;kotlin.collections.Set<kotlin.Int>;kotlin.collections.Set<kotlin.Int>;kotlin.collections.Set<kotlin.Int>;kotlin.collections.Set<kotlinx.datetime.Month>;kotlin.collections.Set<kotlinx.datetime.DayOfWeek>){}[0]
2424

2525
final val atHours // io.github.kevincianfarini.cardiologist/PulseSchedule.atHours|{}atHours[0]
26-
final fun <get-atHours>(): kotlin.collections/Set<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atHours.<get-atHours>|<get-atHours>(){}[0]
26+
final fun <get-atHours>(): kotlin.collections/List<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atHours.<get-atHours>|<get-atHours>(){}[0]
2727
final val atMinutes // io.github.kevincianfarini.cardiologist/PulseSchedule.atMinutes|{}atMinutes[0]
28-
final fun <get-atMinutes>(): kotlin.collections/Set<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atMinutes.<get-atMinutes>|<get-atMinutes>(){}[0]
28+
final fun <get-atMinutes>(): kotlin.collections/List<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atMinutes.<get-atMinutes>|<get-atMinutes>(){}[0]
2929
final val atSeconds // io.github.kevincianfarini.cardiologist/PulseSchedule.atSeconds|{}atSeconds[0]
30-
final fun <get-atSeconds>(): kotlin.collections/Set<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atSeconds.<get-atSeconds>|<get-atSeconds>(){}[0]
30+
final fun <get-atSeconds>(): kotlin.collections/List<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.atSeconds.<get-atSeconds>|<get-atSeconds>(){}[0]
3131
final val inMonths // io.github.kevincianfarini.cardiologist/PulseSchedule.inMonths|{}inMonths[0]
32-
final fun <get-inMonths>(): kotlin.collections/Set<kotlinx.datetime/Month> // io.github.kevincianfarini.cardiologist/PulseSchedule.inMonths.<get-inMonths>|<get-inMonths>(){}[0]
32+
final fun <get-inMonths>(): kotlin.collections/List<kotlinx.datetime/Month> // io.github.kevincianfarini.cardiologist/PulseSchedule.inMonths.<get-inMonths>|<get-inMonths>(){}[0]
3333
final val onDaysOfMonth // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfMonth|{}onDaysOfMonth[0]
34-
final fun <get-onDaysOfMonth>(): kotlin.collections/Set<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfMonth.<get-onDaysOfMonth>|<get-onDaysOfMonth>(){}[0]
34+
final fun <get-onDaysOfMonth>(): kotlin.collections/List<kotlin/Int> // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfMonth.<get-onDaysOfMonth>|<get-onDaysOfMonth>(){}[0]
3535
final val onDaysOfWeek // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfWeek|{}onDaysOfWeek[0]
36-
final fun <get-onDaysOfWeek>(): kotlin.collections/Set<kotlinx.datetime/DayOfWeek> // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfWeek.<get-onDaysOfWeek>|<get-onDaysOfWeek>(){}[0]
36+
final fun <get-onDaysOfWeek>(): kotlin.collections/List<kotlinx.datetime/DayOfWeek> // io.github.kevincianfarini.cardiologist/PulseSchedule.onDaysOfWeek.<get-onDaysOfWeek>|<get-onDaysOfWeek>(){}[0]
3737

3838
final fun equals(kotlin/Any?): kotlin/Boolean // io.github.kevincianfarini.cardiologist/PulseSchedule.equals|equals(kotlin.Any?){}[0]
3939
final fun hashCode(): kotlin/Int // io.github.kevincianfarini.cardiologist/PulseSchedule.hashCode|hashCode(){}[0]

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,41 @@ import kotlinx.datetime.number
88

99
/**
1010
* A [Pulse] schedule that can be used with [schedulePulse] to define complex schedules.
11-
*
12-
* @constructor Creates a new PulseSchedule. Second values must be in range 0..59, minute values must be in range 0..59,
13-
* hour values must be in range 0..23, and day of month values must be in range 1..31. This constructor also
14-
* requires that seconds, minutes, hours, days of month, and months cannot be empty sets.
15-
* @throws IllegalArgumentException if the constructor is called with any of our bounds value or an improperly empty set.
1611
*/
1712
@Poko
18-
public class PulseSchedule(
19-
public val atSeconds: Set<Int>,
20-
public val atMinutes: Set<Int>,
21-
public val atHours: Set<Int>,
22-
public val onDaysOfMonth: Set<Int>,
23-
public val inMonths: Set<Month>,
24-
public val onDaysOfWeek: Set<DayOfWeek>,
13+
public class PulseSchedule internal constructor(
14+
public val atSeconds: List<Int>,
15+
public val atMinutes: List<Int>,
16+
public val atHours: List<Int>,
17+
public val onDaysOfMonth: List<Int>,
18+
public val inMonths: List<Month>,
19+
public val onDaysOfWeek: List<DayOfWeek>,
2520
) {
21+
22+
/**
23+
* Creates a new PulseSchedule. Second values must be in range 0..59, minute values must be in range 0..59,
24+
* hour values must be in range 0..23, and day of month values must be in range 1..31. This constructor also
25+
* requires that seconds, minutes, hours, days of month, and months cannot be empty sets.
26+
*
27+
* @throws IllegalArgumentException if the constructor is called with any of our bounds value or an improperly empty
28+
* set.
29+
*/
30+
public constructor(
31+
atSeconds: Set<Int>,
32+
atMinutes: Set<Int>,
33+
atHours: Set<Int>,
34+
onDaysOfMonth: Set<Int>,
35+
inMonths: Set<Month>,
36+
onDaysOfWeek: Set<DayOfWeek>,
37+
) : this(
38+
atSeconds = atSeconds.sorted(),
39+
atMinutes = atMinutes.sorted(),
40+
atHours = atHours.sorted(),
41+
onDaysOfMonth = onDaysOfMonth.sorted(),
42+
inMonths = inMonths.sorted(),
43+
onDaysOfWeek = onDaysOfWeek.sorted()
44+
)
45+
2646
init {
2747
require(!atSeconds.any { it !in 0..59 }) { "Seconds has an out of bound value: $atSeconds" }
2848
require(atSeconds.isNotEmpty()) { "Seconds cannot be empty!" }

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ internal fun LocalDateTime.nextMatch(schedule: PulseSchedule): LocalDateTime {
1818
val time = if (matches(schedule)) copy(nanosecond = 1) else this
1919
return with(schedule) {
2020
time.nextMonth(inMonths)
21-
.nextDay(onDaysOfMonth, onDaysOfWeek,inMonths)
21+
.nextDay(onDaysOfMonth, onDaysOfWeek, inMonths)
2222
.nextHour(atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
2323
.nextMinute(atMinutes, atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
2424
.nextSecond(atSeconds, atMinutes, atHours, onDaysOfMonth, onDaysOfWeek, inMonths)
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)