Skip to content

Commit

Permalink
Watcher: Simplify finding next date in cron schedule (#33015)
Browse files Browse the repository at this point in the history
The code introduced in 3fa3680 to fix
an issue with crons always returning -1 was not very readable. This
implementation uses streams to improve readability.
  • Loading branch information
spinscale committed Aug 28, 2018
1 parent 6797c92 commit 01a3b66
Showing 1 changed file with 8 additions and 13 deletions.
Expand Up @@ -30,19 +30,14 @@ private CronnableSchedule(Cron... crons) {
@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() {
Expand Down

0 comments on commit 01a3b66

Please sign in to comment.