Example usage for org.joda.time Interval Interval

List of usage examples for org.joda.time Interval Interval

Introduction

In this page you can find the example usage for org.joda.time Interval Interval.

Prototype

public Interval(Object interval, Chronology chronology) 

Source Link

Document

Constructs a time interval by converting or copying from another object, overriding the chronology.

Usage

From source file:cz.cesnet.shongo.controller.booking.room.RoomEndpoint.java

/**
 * @param slot/*from   w ww.  j  ava2 s . c om*/
 * @param slotBefore
 * @param slotAfter
 * @return original slot from given {@code slot} and {@code slotBefore} and {@code slotAfter}
 */
public static Interval getOriginalSlot(Interval slot, Period slotBefore, Period slotAfter) {
    DateTime start = slot.getStart().plus(slotBefore);
    DateTime end = slot.getEnd().minus(slotAfter);
    if (end.isBefore(start)) {
        end = start;
    }
    return new Interval(start, end);
}

From source file:cz.cesnet.shongo.controller.booking.room.RoomReservationTask.java

/**
 * Constructor.//from   w ww .  j  ava2 s  .  c  om
 *
 * @param schedulerContext  sets the {@link #schedulerContext}
 * @param slot              sets the {@link #slot}
 * @param slotMinutesBefore sets the {@link #slotMinutesBefore}
 * @param slotMinutesAfter  sets the {@link #slotMinutesAfter}
 */
public RoomReservationTask(SchedulerContext schedulerContext, Interval slot, int slotMinutesBefore,
        int slotMinutesAfter) {
    super(schedulerContext, new Interval(slot.getStart().minusMinutes(slotMinutesBefore),
            slot.getEnd().plusMinutes(slotMinutesAfter)));
    this.slotMinutesBefore = slotMinutesBefore;
    this.slotMinutesAfter = slotMinutesAfter;
}

From source file:cz.PA165.vozovyPark.controller.DriveController.java

public boolean isIntervalFree(DriveDTO drive) {
    Interval range = new Interval(drive.getDateFrom(), drive.getDateTo());
    VehicleDTO vehicle = drive.getVehicle();
    for (DriveDTO d : driveService.findAll()) {
        Interval i = new Interval(d.getDateFrom(), d.getDateTo());
        if (range.overlaps(i) && d.getVehicle().equals(vehicle)) {
            return false;
        }/*from w w w  .j av  a2s.c  o m*/
    }
    return true;
}

From source file:de.avanux.smartapplianceenabler.appliance.ConsecutiveDaysTimeframe.java

License:Open Source License

public List<TimeframeInterval> getIntervals(LocalDateTime now) {
    if (start != null && end != null) {
        LocalDateTime earliestStartNextOccurrence = start.toNextOccurrence(now);
        LocalDateTime latestEndNextOccurrence = end.toNextOccurrence(now);
        LocalDateTime earliestStartDateTime = earliestStartNextOccurrence;
        if (latestEndNextOccurrence.isBefore(earliestStartNextOccurrence)
                && now.isBefore(latestEndNextOccurrence)) {
            earliestStartDateTime = start.toLastOccurrence(now);
        }/*ww  w .j  av a2 s . c  om*/
        LocalDateTime latestEndDateTime = end.toNextOccurrence(earliestStartDateTime);
        Interval interval = new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime())
                .withChronology(ISOChronology.getInstance());
        TimeframeInterval timeframeInterval = new TimeframeInterval(this, interval);
        return Collections.singletonList(timeframeInterval);
    }
    return null;
}

From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java

License:Open Source License

protected Interval buildMidnightAdjustedInterval(LocalDateTime now) {
    if (start != null && end != null) {
        LocalDateTime earliestStartDateTime = new LocalDate(now).toLocalDateTime(start.toLocalTime());
        LocalDateTime latestEndDateTime = new LocalDate(now).toLocalDateTime(end.toLocalTime());
        if (isOverMidnight(earliestStartDateTime, latestEndDateTime)) {
            if (now.toLocalTime().isAfter(start.toLocalTime())) {
                // before midnight
                latestEndDateTime = latestEndDateTime.plusHours(24);
            } else if (now.toLocalTime().isBefore(end.toLocalTime())) {
                // after midnight, before end
                earliestStartDateTime = earliestStartDateTime.minusHours(24);
            } else {
                // after midnight, after end
                latestEndDateTime = latestEndDateTime.plusHours(24);
            }//from  w  w w .  j  a v  a 2 s.  c  o m
        }
        return new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime())
                .withChronology(ISOChronology.getInstance());
    }
    return null;
}

From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java

License:Open Source License

@Override
public List<TimeframeInterval> getIntervals(LocalDateTime now) {
    List<TimeframeInterval> intervals = new ArrayList<>();
    if (start != null && end != null) {
        Interval interval = buildMidnightAdjustedInterval(now);
        List<Integer> dowValues = getDaysOfWeekValues();
        // if today's interval already ended we ignore today
        int dayOffset = (interval.getEnd().isBefore(now.toDateTime()) ? 1 : 0);
        for (int i = dayOffset; i < 7 + dayOffset; i++) {
            DateTime timeFrameStart = interval.getStart().plusDays(i);
            DateTime timeFrameEnd = interval.getEnd().plusDays(i);
            if (dowValues != null) {
                int dow = timeFrameStart.getDayOfWeek();
                if (dowValues.contains(DOW_HOLIDAYS) && isHoliday(timeFrameStart.toLocalDate())) {
                    dow = DOW_HOLIDAYS;/*from   w  w  w .j  ava  2 s  .c o  m*/
                }
                if (dowValues.contains(dow)) {
                    intervals.add(new TimeframeInterval(this, new Interval(timeFrameStart, timeFrameEnd)));
                }
            } else {
                intervals.add(new TimeframeInterval(this, new Interval(timeFrameStart, timeFrameEnd)));
            }
        }
    }
    return intervals;
}

From source file:de.avanux.smartapplianceenabler.appliance.RunningTimeMonitor.java

License:Open Source License

/**
 * Updates remainingMinRunningTime for the given instant. The value may become negative!
 * Subsequent calls to this method within one second are omitted.
 * @param now//from   w  ww . j  av  a  2s  . co m
 */
protected void update(LocalDateTime now) {
    // update not more than once per second in order to avoid spamming the log
    if (lastUpdate == null || now.isBefore(lastUpdate)
            || new Interval(lastUpdate.toDateTime(), now.toDateTime()).toDurationMillis() > 1000) {
        activateTimeframeInterval(now, schedules);
        deactivateExpiredTimeframeInterval(now);
        logger.debug("activeTimeframeInterval=" + activeTimeframeInterval + " statusChangedAt="
                + statusChangedAt + " intervalBegin=" + intervalBegin + " running=" + running);

        Interval interval = null;
        if (running) {
            // running
            if (intervalBegin == null) {
                // running was set to true after interval begin
                interval = new Interval(statusChangedAt.toDateTime(), now.toDateTime());
            } else {
                // no status change in interval
                interval = new Interval(intervalBegin.toDateTime(), now.toDateTime());
            }
            intervalBegin = now;
        } else if (intervalBegin != null && statusChangedAt != null) {
            // running was set to false after interval begin
            interval = new Interval(intervalBegin.toDateTime(), statusChangedAt.toDateTime());
            intervalBegin = null;
            statusChangedAt = null;
        }
        if (interval != null && remainingMinRunningTime != null && remainingMaxRunningTime != null) {
            int intervalSeconds = Double.valueOf(interval.toDuration().getMillis() / 1000).intValue();
            remainingMinRunningTime = remainingMinRunningTime - intervalSeconds;
            remainingMaxRunningTime = remainingMaxRunningTime - intervalSeconds;
        }
        lastUpdate = now;
    }
}

From source file:de.avanux.smartapplianceenabler.semp.webservice.SempController.java

License:Open Source License

private PlanningRequest createPlanningRequest(ApplianceLogger applianceLogger, LocalDateTime now,
        Appliance appliance) {/*from  w w w. j a  v a 2  s.c om*/
    PlanningRequest planningRequest = null;
    RunningTimeMonitor runningTimeMonitor = appliance.getRunningTimeMonitor();
    if (runningTimeMonitor != null) {
        List<de.avanux.smartapplianceenabler.semp.webservice.Timeframe> sempTimeFrames = new ArrayList<de.avanux.smartapplianceenabler.semp.webservice.Timeframe>();
        List<Schedule> schedules = runningTimeMonitor.getSchedules();
        TimeframeInterval timeframeInterval = runningTimeMonitor.getActiveTimeframeInterval();
        if (schedules != null && schedules.size() > 0) {
            applianceLogger.debug("Active schedules: " + schedules.size());
            TimeframeInterval activeTimeframeInterval = runningTimeMonitor.getActiveTimeframeInterval();
            addSempTimeFrame(applianceLogger, runningTimeMonitor, timeframeInterval, appliance, sempTimeFrames,
                    now);

            Interval considerationInterval = new Interval(now.toDateTime(), now.plusDays(2).toDateTime());
            List<TimeframeInterval> timeFrameIntervals = Schedule.findTimeframeIntervals(now,
                    considerationInterval, runningTimeMonitor.getSchedules());
            for (TimeframeInterval timeframeIntervalOfSchedule : timeFrameIntervals) {
                Schedule schedule = timeframeIntervalOfSchedule.getTimeframe().getSchedule();
                addSempTimeFrame(applianceLogger, appliance, sempTimeFrames, schedule,
                        timeframeIntervalOfSchedule.getInterval(), schedule.getMinRunningTime(),
                        schedule.getMaxRunningTime(), now);
            }
        } else if (timeframeInterval != null) {
            applianceLogger.debug("Active timeframe interval found");
            addSempTimeFrame(applianceLogger, runningTimeMonitor, timeframeInterval, appliance, sempTimeFrames,
                    now);
        } else {
            applianceLogger.debug("No timeframes found");
        }

        if (sempTimeFrames.size() > 0) {
            planningRequest = new PlanningRequest();
            planningRequest.setTimeframes(sempTimeFrames);
        } else {
            applianceLogger.debug("No planning requests created");
            return null;
        }
    }
    return planningRequest;
}

From source file:de.avanux.smartapplianceenabler.semp.webservice.SempController.java

License:Open Source License

protected de.avanux.smartapplianceenabler.semp.webservice.Timeframe createSempTimeFrame(
        ApplianceLogger applianceLogger, String deviceId, Schedule schedule, Interval interval,
        long minRunningTime, long maxRunningTime, LocalDateTime now) {
    Long earliestStart = 0l;//from   w ww.ja  v  a  2s  .co  m
    DateTime start = interval.getStart();
    DateTime end = interval.getEnd();
    if (start.isAfter(now.toDateTime())) {
        earliestStart = Double.valueOf(new Interval(now.toDateTime(), start).toDurationMillis() / 1000)
                .longValue();
    }
    LocalDateTime nowBeforeEnd = new LocalDateTime(now);
    if (now.toDateTime().isAfter(end)) {
        nowBeforeEnd = now.minusHours(24);
    }
    Long latestEnd = Double.valueOf(new Interval(nowBeforeEnd.toDateTime(), end).toDurationMillis() / 1000)
            .longValue();
    return createSempTimeFrame(applianceLogger, deviceId, earliestStart, latestEnd, minRunningTime,
            maxRunningTime);
}

From source file:de.fraunhofer.iosb.ilt.sta.model.ext.TimeInterval.java

License:Open Source License

public static TimeInterval create(long start, long end) {
    return new TimeInterval(new Interval(start, end));
}