Watcher: Simplify finding next date in cron schedule (#33015)
The code introduced in 3fa36807f8
to fix
an issue with crons always returning -1 was not very readable. This
implementation uses streams to improve readability.
This commit is contained in:
parent
2cc611604f
commit
19ef41ee82
|
@ -30,19 +30,14 @@ public abstract class CronnableSchedule implements Schedule {
|
|||
@Override
|
||||
public long nextScheduledTimeAfter(long startTime, long time) {
|
||||
assert time >= startTime;
|
||||
long nextTime = Long.MAX_VALUE;
|
||||
for (Cron cron : crons) {
|
||||
long nextValidTimeAfter = cron.getNextValidTimeAfter(time);
|
||||
|
||||
boolean previousCronExpired = nextTime == -1;
|
||||
boolean currentCronValid = nextValidTimeAfter > -1;
|
||||
if (previousCronExpired && currentCronValid) {
|
||||
nextTime = nextValidTimeAfter;
|
||||
} else {
|
||||
nextTime = Math.min(nextTime, nextValidTimeAfter);
|
||||
}
|
||||
}
|
||||
return nextTime;
|
||||
return Arrays.stream(crons)
|
||||
.map(cron -> cron.getNextValidTimeAfter(time))
|
||||
// filter out expired dates before sorting
|
||||
.filter(nextValidTime -> nextValidTime > -1)
|
||||
.sorted()
|
||||
.findFirst()
|
||||
// no date in the future found, return -1 to the caller
|
||||
.orElse(-1L);
|
||||
}
|
||||
|
||||
public Cron[] crons() {
|
||||
|
|
Loading…
Reference in New Issue