Search in sources :

Example 26 with CronField

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

the class CronConstraintsFactory method ensureEitherDayOfWeekOrDayOfMonth.

public static CronConstraint ensureEitherDayOfWeekOrDayOfMonth() {
    // both a day-of-week AND a day-of-month parameter should fail for QUARTZ
    return new CronConstraint("Both, a day-of-week AND a day-of-month parameter, are not supported.") {

        private static final long serialVersionUID = -4423693913868081656L;

        @Override
        public boolean validate(Cron cron) {
            CronField dayOfYearField = cron.retrieve(CronFieldName.DAY_OF_YEAR);
            CronField dayOfMonthField = cron.retrieve(CronFieldName.DAY_OF_MONTH);
            CronField dayOfWeekField = cron.retrieve(CronFieldName.DAY_OF_WEEK);
            if (dayOfYearField == null || dayOfYearField.getExpression() instanceof QuestionMark) {
                if (dayOfMonthField != null && !(dayOfMonthField.getExpression() instanceof QuestionMark)) {
                    return dayOfWeekField != null && dayOfWeekField.getExpression() instanceof QuestionMark;
                } else {
                    return dayOfWeekField != null && !(dayOfWeekField.getExpression() instanceof QuestionMark);
                }
            }
            return true;
        }
    };
}
Also used : QuestionMark(com.cronutils.model.field.expression.QuestionMark) Cron(com.cronutils.model.Cron) CronField(com.cronutils.model.field.CronField)

Example 27 with CronField

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

the class CronBuilder method addField.

@VisibleForTesting
CronBuilder addField(final CronFieldName name, final FieldExpression expression) {
    checkState(definition != null, "CronBuilder not initialized.");
    final FieldDefinition fieldDefinition = definition.getFieldDefinition(name);
    checkState(fieldDefinition != null, "Cron field definition does not exist: %s", name);
    final FieldConstraints constraints = fieldDefinition.getConstraints();
    expression.accept(new ValidationFieldExpressionVisitor(constraints));
    fields.put(name, new CronField(name, expression, constraints));
    return this;
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) ValidationFieldExpressionVisitor(com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) CronField(com.cronutils.model.field.CronField) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Example 28 with CronField

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

the class BetweenDayOfWeekValueGeneratorTest method testGenerateCandidatesNotIncludingIntervalExtremes.

@Test
public void testGenerateCandidatesNotIncludingIntervalExtremes() {
    // cron expression: DoW 1-5,
    // on February 2016: 1-5, 8-12, 15-19, 22-26, 29
    final CronField cronField = new CronField(CronFieldName.DAY_OF_WEEK, new Between(new IntegerFieldValue(1), new IntegerFieldValue(5)), FieldConstraintsBuilder.instance().createConstraintsInstance());
    final BetweenDayOfWeekValueGenerator generator = new BetweenDayOfWeekValueGenerator(cronField, 2016, 2, new WeekDay(1, true));
    final List<Integer> values = generator.generateCandidates(1, 29);
    validateInterval(1, 5, values);
    validateInterval(8, 12, values);
    validateInterval(15, 19, values);
    validateInterval(22, 26, values);
    assertTrue(values.contains(29));
}
Also used : WeekDay(com.cronutils.mapper.WeekDay) Between(com.cronutils.model.field.expression.Between) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) CronField(com.cronutils.model.field.CronField) Test(org.junit.Test)

Example 29 with CronField

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

the class BetweenFieldValueGeneratorTest method setUp.

@Before
public void setUp() {
    constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    fieldValueGenerator = new BetweenFieldValueGenerator(new CronField(CronFieldName.HOUR, new Between(new IntegerFieldValue(FROM), new IntegerFieldValue(TO)), constraints));
}
Also used : Between(com.cronutils.model.field.expression.Between) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) CronField(com.cronutils.model.field.CronField) Before(org.junit.Before)

Example 30 with CronField

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

the class OnDayOfMonthValueGeneratorTest method setUp.

@Before
public void setUp() {
    constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    fieldValueGenerator = new OnDayOfMonthValueGenerator(new CronField(CronFieldName.DAY_OF_MONTH, new On(new IntegerFieldValue(3)), constraints), YEAR, MONTH);
}
Also used : IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Before(org.junit.Before)

Aggregations

CronField (com.cronutils.model.field.CronField)34 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)18 Test (org.junit.Test)16 On (com.cronutils.model.field.expression.On)15 SingleCron (com.cronutils.model.SingleCron)13 ArrayList (java.util.ArrayList)13 Before (org.junit.Before)7 Between (com.cronutils.model.field.expression.Between)6 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)5 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)4 WeekDay (com.cronutils.mapper.WeekDay)3 Cron (com.cronutils.model.Cron)3 CronFieldName (com.cronutils.model.field.CronFieldName)3 Every (com.cronutils.model.field.expression.Every)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CompositeCron (com.cronutils.model.CompositeCron)2 And (com.cronutils.model.field.expression.And)2 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)2 CronConstraint (com.cronutils.model.definition.CronConstraint)1 FieldConstraintsBuilder (com.cronutils.model.field.constraint.FieldConstraintsBuilder)1