Search in sources :

Example 1 with Between

use of com.cronutils.model.field.expression.Between in project cron-utils by jmrozanec.

the class BetweenFieldValueGenerator method generateNextValue.

@Override
public int generateNextValue(final int reference) throws NoSuchValueException {
    final Between between = (Between) cronField.getExpression();
    // TODO validate from/to logic
    int candidate = reference;
    do {
        ++candidate;
    } while (candidate < map(between.getFrom()));
    if (candidate > map(between.getTo())) {
        throw new NoSuchValueException();
    }
    return candidate;
}
Also used : Between(com.cronutils.model.field.expression.Between)

Example 2 with Between

use of com.cronutils.model.field.expression.Between in project cron-utils by jmrozanec.

the class BetweenFieldValueGenerator method generatePreviousValue.

@Override
public int generatePreviousValue(final int reference) throws NoSuchValueException {
    final Between between = (Between) cronField.getExpression();
    // TODO deal with from/to logic, to ensure correct values are assumed
    int candidate = reference;
    do {
        --candidate;
    } while (candidate > map(between.getTo()));
    if (candidate < map(between.getFrom())) {
        throw new NoSuchValueException();
    }
    return candidate;
}
Also used : Between(com.cronutils.model.field.expression.Between)

Example 3 with Between

use of com.cronutils.model.field.expression.Between in project cron-utils by jmrozanec.

the class EveryDayOfWeekValueGeneratorTest method setUp.

@Before
public void setUp() {
    FieldConstraints constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    // every 2 days between 1-5
    final CronField cronField = new CronField(CronFieldName.DAY_OF_WEEK, new Every(new Between(new IntegerFieldValue(1), new IntegerFieldValue(5)), new IntegerFieldValue(2)), constraints);
    // Using monday = 1
    WeekDay mondayDoWValue = new WeekDay(1, false);
    // so MON-FRI/2, which should translate to MON,WED,FRI
    validDow = EnumSet.of(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY);
    fieldValueGenerator = new EveryDayOfWeekValueGenerator(cronField, year, month, mondayDoWValue);
}
Also used : WeekDay(com.cronutils.mapper.WeekDay) Between(com.cronutils.model.field.expression.Between) Every(com.cronutils.model.field.expression.Every) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) CronField(com.cronutils.model.field.CronField) Before(org.junit.Before)

Example 4 with Between

use of com.cronutils.model.field.expression.Between in project cron-utils by jmrozanec.

the class CronDescriptorTest method testEveryMinuteBetweenMonths.

@Test
public void testEveryMinuteBetweenMonths() {
    final int monthStart = 2;
    final int monthEnd = 3;
    final List<CronField> results = new ArrayList<>();
    results.add(new CronField(CronFieldName.HOUR, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MINUTE, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MONTH, new Between(new IntegerFieldValue(monthStart), new IntegerFieldValue(monthEnd)), nullFieldConstraints));
    assertEquals("every minute every month between February and March", descriptor.describe(new SingleCron(mockDefinition, results)));
}
Also used : Between(com.cronutils.model.field.expression.Between) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) Test(org.junit.Test)

Example 5 with Between

use of com.cronutils.model.field.expression.Between in project cron-utils by jmrozanec.

the class BetweenFieldValueGenerator method generateCandidatesNotIncludingIntervalExtremes.

@Override
protected List<Integer> generateCandidatesNotIncludingIntervalExtremes(final int start, final int end) {
    final List<Integer> values = new ArrayList<>();
    // check overlapping ranges: x1 <= y2 && y1 <= x2
    final Between between = (Between) cronField.getExpression();
    final int expressionStart = map(between.getFrom());
    final int expressionEnd = map(between.getTo());
    int rangestart = start;
    int rangeend = end;
    if (start <= expressionEnd && expressionStart <= end) {
        // ranges overlap
        if (expressionEnd < end) {
            rangeend = expressionEnd;
        }
        if (map(between.getFrom()) > start) {
            rangestart = expressionStart;
        }
        try {
            if (rangestart != start) {
                values.add(rangestart);
            }
            int reference = generateNextValue(rangestart);
            while (reference < rangeend) {
                values.add(reference);
                reference = generateNextValue(reference);
            }
            if (rangeend != end) {
                values.add(reference);
            }
        } catch (final NoSuchValueException e) {
        // TODO: Explain why this exception is ignored
        }
    }
    return values;
}
Also used : Between(com.cronutils.model.field.expression.Between) ArrayList(java.util.ArrayList)

Aggregations

Between (com.cronutils.model.field.expression.Between)9 CronField (com.cronutils.model.field.CronField)6 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)6 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 SingleCron (com.cronutils.model.SingleCron)3 WeekDay (com.cronutils.mapper.WeekDay)2 On (com.cronutils.model.field.expression.On)2 Before (org.junit.Before)2 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)1 Every (com.cronutils.model.field.expression.Every)1