All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kagkarlsson.shaded.cronutils.model.time.CompositeExecutionTime Maven / Gradle / Ivy

There is a newer version: 15.0.0
Show 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy