Search in sources :

Example 1 with ValidationFieldExpressionVisitor

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

the class SingleCron method validate.

/**
 * Validates this Cron instance by validating its cron expression.
 *
 * @return this Cron instance
 * @throws IllegalArgumentException if the cron expression is invalid
 */
public Cron validate() {
    for (final Map.Entry<CronFieldName, CronField> field : retrieveFieldsAsMap().entrySet()) {
        final CronFieldName fieldName = field.getKey();
        field.getValue().getExpression().accept(new ValidationFieldExpressionVisitor(getCronDefinition().getFieldDefinition(fieldName).getConstraints()));
    }
    for (final CronConstraint constraint : getCronDefinition().getCronConstraints()) {
        if (!constraint.validate(this)) {
            throw new IllegalArgumentException(String.format("Invalid cron expression: %s. %s", asString(), constraint.getDescription()));
        }
    }
    return this;
}
Also used : CronFieldName(com.cronutils.model.field.CronFieldName) CronConstraint(com.cronutils.model.definition.CronConstraint) ValidationFieldExpressionVisitor(com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor) CronField(com.cronutils.model.field.CronField)

Example 2 with ValidationFieldExpressionVisitor

use of com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor 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)

Aggregations

CronField (com.cronutils.model.field.CronField)2 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)2 CronConstraint (com.cronutils.model.definition.CronConstraint)1 CronFieldName (com.cronutils.model.field.CronFieldName)1 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)1 FieldDefinition (com.cronutils.model.field.definition.FieldDefinition)1 VisibleForTesting (com.cronutils.utils.VisibleForTesting)1