Example usage for org.joda.time Minutes dividedBy

List of usage examples for org.joda.time Minutes dividedBy

Introduction

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

Prototype

public Minutes dividedBy(int divisor) 

Source Link

Document

Returns a new instance with the minutes divided by the specified divisor.

Usage

From source file:com.google.android.apps.paco.EsmGenerator2.java

License:Open Source License

public List<DateTime> generateForSchedule(DateTime startDate, SignalSchedule schedule) {
    this.schedule = schedule;
    this.periodStartDate = adjustStartDateToBeginningOfPeriod(startDate);
    times = new ArrayList<DateTime>();

    if (schedule.getEsmFrequency() == null || schedule.getEsmFrequency() == 0) {
        return times;
    }/*from ww  w  .j av  a 2 s  . c o  m*/
    List<Integer> schedulableDays;
    switch (schedule.getEsmPeriodInDays()) {
    case SignalSchedule.ESM_PERIOD_DAY:
        if (!schedule.getEsmWeekends() && TimeUtil.isWeekend(periodStartDate)) {
            return times;
        } else {
            schedulableDays = Arrays.asList(1);
        }
        break;
    case SignalSchedule.ESM_PERIOD_WEEK:
        schedulableDays = getPeriodDaysForWeek();
        break;
    case SignalSchedule.ESM_PERIOD_MONTH:
        schedulableDays = getPeriodDaysForMonthOf(periodStartDate);
        break;
    default:
        throw new IllegalStateException("Cannot get here.");
    }

    Minutes dayLengthIntervalInMinutes = Minutes
            .minutesIn(new Interval(schedule.getEsmStartHour(), schedule.getEsmEndHour()));
    Minutes totalMinutesInPeriod = dayLengthIntervalInMinutes.multipliedBy(schedulableDays.size());
    Minutes sampleBlockTimeInMinutes = totalMinutesInPeriod.dividedBy(schedule.getEsmFrequency());
    Minutes timeoutInMinutes = Minutes.minutes(schedule.getMinimumBuffer());
    Random rand = new Random();
    for (int signal = 0; signal < schedule.getEsmFrequency(); signal++) {

        int candidateTimeInBlock;
        DateTime candidateTime;
        int periodAttempts = 1000;
        do {
            candidateTimeInBlock = rand.nextInt(sampleBlockTimeInMinutes.getMinutes());
            // map candidatePeriod and candidateTime back onto days of period
            // note, sometimes a candidate period will map across days in period
            // because start and end hours make for non-contiguous days
            int totalMinutesToAdd = sampleBlockTimeInMinutes.getMinutes() * signal + candidateTimeInBlock;
            int daysToAdd = totalMinutesToAdd / dayLengthIntervalInMinutes.getMinutes();
            int minutesToAdd = 0;
            if (totalMinutesToAdd <= dayLengthIntervalInMinutes.getMinutes()) { // within one day
                minutesToAdd = totalMinutesToAdd;
            } else {
                minutesToAdd = totalMinutesToAdd % dayLengthIntervalInMinutes.getMinutes();
            }

            DateTime plusDays = periodStartDate.plusDays(schedulableDays.get(daysToAdd) - 1);
            candidateTime = plusDays.withMillisOfDay(schedule.getEsmStartHour().intValue())
                    .plusMinutes(minutesToAdd);
            periodAttempts--;
        } while (periodAttempts > 0
                && (!isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                        || (!schedule.getEsmWeekends() && TimeUtil.isWeekend(candidateTime))));
        if (isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                && (schedule.getEsmWeekends() || !TimeUtil.isWeekend(candidateTime))) {
            times.add(candidateTime);
        }

    }
    return times;
}

From source file:com.pacoapp.paco.shared.scheduling.EsmGenerator2.java

License:Open Source License

public List<DateTime> generateForSchedule(DateTime startDate, Schedule schedule2) {
    this.schedule = schedule2;
    this.periodStartDate = adjustStartDateToBeginningOfPeriod(startDate);
    times = new ArrayList<DateTime>();

    if (schedule2.getEsmFrequency() == null || schedule2.getEsmFrequency() == 0) {
        return times;
    }/*from   w ww .ja  v  a  2s . c  o  m*/
    List<Integer> schedulableDays;
    switch (schedule2.getEsmPeriodInDays()) {
    case Schedule.ESM_PERIOD_DAY:
        if (!schedule2.getEsmWeekends() && TimeUtil.isWeekend(periodStartDate)) {
            return times;
        } else {
            schedulableDays = Arrays.asList(1);
        }
        break;
    case Schedule.ESM_PERIOD_WEEK:
        schedulableDays = getPeriodDaysForWeek();
        break;
    case Schedule.ESM_PERIOD_MONTH:
        schedulableDays = getPeriodDaysForMonthOf(periodStartDate);
        break;
    default:
        throw new IllegalStateException("Cannot get here.");
    }

    Minutes dayLengthIntervalInMinutes = Minutes
            .minutesIn(new Interval(schedule2.getEsmStartHour(), schedule2.getEsmEndHour()));
    Minutes totalMinutesInPeriod = dayLengthIntervalInMinutes.multipliedBy(schedulableDays.size());
    Minutes sampleBlockTimeInMinutes = totalMinutesInPeriod.dividedBy(schedule2.getEsmFrequency());
    Minutes timeoutInMinutes = Minutes.minutes(schedule2.getMinimumBuffer());
    Random rand = new Random();
    for (int signal = 0; signal < schedule2.getEsmFrequency(); signal++) {

        int candidateTimeInBlock;
        DateTime candidateTime;
        int periodAttempts = 1000;
        do {
            candidateTimeInBlock = rand.nextInt(sampleBlockTimeInMinutes.getMinutes());
            // map candidatePeriod and candidateTime back onto days of period
            // note, sometimes a candidate period will map across days in period
            // because start and end hours make for non-contiguous days
            int totalMinutesToAdd = sampleBlockTimeInMinutes.getMinutes() * signal + candidateTimeInBlock;
            int daysToAdd = totalMinutesToAdd / dayLengthIntervalInMinutes.getMinutes();
            int minutesToAdd = 0;
            if (totalMinutesToAdd <= dayLengthIntervalInMinutes.getMinutes()) { // within one day
                minutesToAdd = totalMinutesToAdd;
            } else {
                minutesToAdd = totalMinutesToAdd % dayLengthIntervalInMinutes.getMinutes();
            }

            DateTime plusDays = periodStartDate.plusDays(schedulableDays.get(daysToAdd) - 1);
            candidateTime = plusDays.withMillisOfDay(schedule2.getEsmStartHour().intValue())
                    .plusMinutes(minutesToAdd);
            periodAttempts--;
        } while (periodAttempts > 0
                && (!isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                        || (!schedule2.getEsmWeekends() && TimeUtil.isWeekend(candidateTime))));
        if (isMinimalBufferedDistanceFromOtherTimes(candidateTime, timeoutInMinutes)
                && (schedule2.getEsmWeekends() || !TimeUtil.isWeekend(candidateTime))) {
            times.add(candidateTime);
        }

    }
    return times;
}