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:
Alexander Reelsen 2018-08-28 09:06:43 +02:00 committed by GitHub
parent 2cc611604f
commit 19ef41ee82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 13 deletions

View File

@ -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() {