com.github.kagkarlsson.shaded.cronutils.model.time.CompositeExecutionTime Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db-scheduler Show documentation
Show all versions of db-scheduler Show documentation
Simple persistent scheduler for scheduled tasks, recurring or ad-hoc.
The newest version!
package com.github.kagkarlsson.shaded.cronutils.model.time;
import com.github.kagkarlsson.shaded.cronutils.utils.Preconditions;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class CompositeExecutionTime implements ExecutionTime {
private List executionTimes;
public CompositeExecutionTime(List executionTimes){
Preconditions.checkNotNullNorEmpty(executionTimes, "ExecutionTime list cannot be null or empty");
this.executionTimes = Collections.unmodifiableList(executionTimes);
}
@Override
public Optional nextExecution(ZonedDateTime date) {
Optional> next = executionTimes.parallelStream().map(e->e.nextExecution(date)).filter(Optional::isPresent).sorted(
(o1, o2) -> {
if(o1.isPresent() && o2.isPresent()){
ZonedDateTime first = o1.get();
ZonedDateTime second = o2.get();
return first.compareTo(second);
}
return 0;
}
).findFirst();
return next.orElseGet(Optional::empty);
}
@Override
public Optional timeToNextExecution(ZonedDateTime date) {
final Optional next = nextExecution(date);
return next.map(zonedDateTime -> Duration.between(date, zonedDateTime));
}
@Override
public Optional lastExecution(ZonedDateTime date) {
Optional> next = executionTimes.parallelStream().map(e->e.lastExecution(date)).filter(Optional::isPresent).sorted(
(o1, o2) -> {
if(o1.isPresent() && o2.isPresent()){
ZonedDateTime first = o1.get();
ZonedDateTime second = o2.get();
return second.compareTo(first);
}
return 0;
}).findFirst();
return next.orElseGet(Optional::empty);
}
@Override
public Optional timeFromLastExecution(ZonedDateTime date) {
return lastExecution(date).map(zonedDateTime -> Duration.between(zonedDateTime, date));
}
@Override
public boolean isMatch(ZonedDateTime date) {
return executionTimes.parallelStream().map(e->e.isMatch(date)).filter(v-> v).count()>0;
}
}