Search in sources :

Example 66 with LocalDateTime

use of org.joda.time.LocalDateTime in project SmartApplianceEnabler by camueller.

the class Schedule method getCurrentOrNextTimeframeInterval.

/**
     * Returns the current or next timeframe if the remaining time is greater than maximum running time; otherwise the next timeframe is returned.
     * @param now the time reference
     * @param schedules the list of timeframes to choose from (current timeframe has to be first)
     * @param onlyAlreadyStarted consider only timeframe intervals already started
     * @param onlySufficient if true consider timeframe already started only if time to interval end exceeds min running time
     * @return the next timeframe becoming valid or null
     */
public static TimeframeInterval getCurrentOrNextTimeframeInterval(LocalDateTime now, List<Schedule> schedules, boolean onlyAlreadyStarted, boolean onlySufficient) {
    if (schedules == null || schedules.size() == 0) {
        return null;
    }
    Map<Long, TimeframeInterval> startDelayOfTimeframeInterval = new TreeMap<>();
    for (Schedule schedule : schedules) {
        Timeframe timeframe = schedule.getTimeframe();
        timeframe.setSchedule(schedule);
        List<TimeframeInterval> timeframeIntervals = timeframe.getIntervals(now);
        for (TimeframeInterval timeframeInterval : timeframeIntervals) {
            Interval interval = timeframeInterval.getInterval();
            if (interval.contains(now.toDateTime())) {
                // interval already started ...
                if (onlySufficient) {
                    if (now.plusSeconds(schedule.getMaxRunningTime()).plusSeconds(additionalRunningTime).isBefore(new LocalDateTime(interval.getEnd()))) {
                        // ... with remaining running time sufficient
                        return timeframeInterval;
                    }
                } else {
                    return timeframeInterval;
                }
            } else if (!onlyAlreadyStarted) {
                // interval starts in future
                startDelayOfTimeframeInterval.put(interval.getStartMillis() - now.toDateTime().getMillis(), timeframeInterval);
            }
        }
    }
    if (startDelayOfTimeframeInterval.size() > 0) {
        Long startDelay = startDelayOfTimeframeInterval.keySet().iterator().next();
        return startDelayOfTimeframeInterval.get(startDelay);
    }
    return null;
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) TreeMap(java.util.TreeMap) Interval(org.joda.time.Interval)

Aggregations

LocalDateTime (org.joda.time.LocalDateTime)66 Test (org.junit.Test)35 DateTime (org.joda.time.DateTime)15 Interval (org.joda.time.Interval)12 LocalDate (org.joda.time.LocalDate)11 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 ApplianceLogger (de.avanux.smartapplianceenabler.log.ApplianceLogger)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 DateTimeZone (org.joda.time.DateTimeZone)4 Person (org.qi4j.test.indexing.model.Person)4 HashMap (java.util.HashMap)3 Chronology (org.joda.time.Chronology)3 LocalTime (org.joda.time.LocalTime)3 de.avanux.smartapplianceenabler.appliance (de.avanux.smartapplianceenabler.appliance)2 BuddhistChronology (org.joda.time.chrono.BuddhistChronology)2 GJChronology (org.joda.time.chrono.GJChronology)2 ISOChronology (org.joda.time.chrono.ISOChronology)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2