Search in sources :

Example 1 with CronConstraint

use of com.cronutils.model.definition.CronConstraint 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)

Aggregations

CronConstraint (com.cronutils.model.definition.CronConstraint)1 CronField (com.cronutils.model.field.CronField)1 CronFieldName (com.cronutils.model.field.CronFieldName)1 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)1