Search in sources :

Example 6 with FieldConstraints

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

the class ExecutionTimeBuilder method build.

protected ExecutionTime build() {
    boolean lowestAssigned = false;
    if (seconds == null) {
        seconds = timeNodeLowest(CronFieldName.SECOND, 0, 59);
    } else {
        lowestAssigned = true;
    }
    if (minutes == null) {
        minutes = lowestAssigned ? timeNodeAlways(CronFieldName.MINUTE, 0, 59) : timeNodeLowest(CronFieldName.MINUTE, 0, 59);
    } else {
        lowestAssigned = true;
    }
    if (hours == null) {
        hours = lowestAssigned ? timeNodeAlways(CronFieldName.HOUR, 0, 23) : timeNodeLowest(CronFieldName.HOUR, 0, 23);
    } else {
        lowestAssigned = true;
    }
    if (daysOfMonthCronField == null) {
        final FieldConstraints constraints = getConstraint(CronFieldName.DAY_OF_MONTH);
        daysOfMonthCronField = lowestAssigned ? new CronField(CronFieldName.DAY_OF_MONTH, always(), constraints) : new CronField(CronFieldName.DAY_OF_MONTH, new On(new IntegerFieldValue(1)), constraints);
    } else {
        lowestAssigned = true;
    }
    if (daysOfWeekCronField == null) {
        final FieldConstraints constraints = getConstraint(CronFieldName.DAY_OF_WEEK);
        daysOfWeekCronField = lowestAssigned ? new CronField(CronFieldName.DAY_OF_WEEK, always(), constraints) : new CronField(CronFieldName.DAY_OF_WEEK, new On(new IntegerFieldValue(1)), constraints);
    } else {
        lowestAssigned = true;
    }
    if (months == null) {
        months = lowestAssigned ? timeNodeAlways(CronFieldName.MONTH, 1, 12) : timeNodeLowest(CronFieldName.MONTH, 1, 12);
    }
    if (yearsValueGenerator == null) {
        yearsValueGenerator = FieldValueGeneratorFactory.forCronField(new CronField(CronFieldName.YEAR, always(), getConstraint(CronFieldName.YEAR)));
    }
    if (daysOfYearCronField == null) {
        final FieldConstraints constraints = getConstraint(CronFieldName.DAY_OF_YEAR);
        daysOfYearCronField = new CronField(CronFieldName.DAY_OF_YEAR, lowestAssigned ? FieldExpression.questionMark() : always(), constraints);
    }
    return new SingleExecutionTime(this.cron.getCronDefinition(), this.cron.retrieve(CronFieldName.YEAR), daysOfWeekCronField, daysOfMonthCronField, daysOfYearCronField, months, hours, minutes, seconds);
}
Also used : IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On)

Example 7 with FieldConstraints

use of com.cronutils.model.field.constraint.FieldConstraints 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 8 with FieldConstraints

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

the class CronDefinitionBuilderTest method testRegister.

@Test
public void testRegister() {
    final FieldDefinition testFieldDefinition = new FieldDefinition(CronFieldName.SECOND, new FieldConstraints(Collections.emptyMap(), Collections.emptyMap(), Collections.emptySet(), 0, 1, true));
    builder.register(testFieldDefinition);
    final Set<FieldDefinition> definitions = builder.instance().getFieldDefinitions();
    assertNotNull(definitions);
    assertEquals(1, definitions.size());
    assertEquals(testFieldDefinition, definitions.iterator().next());
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) Test(org.junit.Test)

Example 9 with FieldConstraints

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

the class ValidationFieldExpressionVisitorTest method testVisitBetweenOOOrderStrict.

@Test(expected = IllegalArgumentException.class)
public void testVisitBetweenOOOrderStrict() {
    final int startRange = 0;
    final int endRange = 59;
    fieldConstraints = new FieldConstraints(Collections.emptyMap(), Collections.emptyMap(), Collections.emptySet(), startRange, endRange, true);
    visitor = new ValidationFieldExpressionVisitor(fieldConstraints, stringValidations);
    final Between between = new Between(new IntegerFieldValue(HIGH), new IntegerFieldValue(LOW));
    assertEquals(between, between.accept(visitor));
}
Also used : IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) Test(org.junit.Test)

Aggregations

FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)9 CronField (com.cronutils.model.field.CronField)4 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)4 Test (org.junit.Test)4 FieldDefinition (com.cronutils.model.field.definition.FieldDefinition)3 Before (org.junit.Before)3 On (com.cronutils.model.field.expression.On)2 WeekDay (com.cronutils.mapper.WeekDay)1 Between (com.cronutils.model.field.expression.Between)1 Every (com.cronutils.model.field.expression.Every)1 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)1 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)1 VisibleForTesting (com.cronutils.utils.VisibleForTesting)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1