|
| 1 | +package com.cronutils.model; |
| 2 | + |
| 3 | +import com.cronutils.mapper.CronMapper; |
| 4 | +import com.cronutils.model.definition.CronConstraint; |
| 5 | +import com.cronutils.model.definition.CronDefinition; |
| 6 | +import com.cronutils.model.field.CronField; |
| 7 | +import com.cronutils.model.field.CronFieldName; |
| 8 | +import com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor; |
| 9 | +import com.cronutils.utils.Preconditions; |
| 10 | + |
| 11 | +import java.util.*; |
| 12 | + |
| 13 | +public class RebootCron implements Cron { |
| 14 | + private static final long serialVersionUID = 7487370826825439099L; |
| 15 | + private final CronDefinition cronDefinition; |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a Cron with the given cron definition and the given fields. |
| 19 | + * @param cronDefinition the definition to use for this Cron |
| 20 | + */ |
| 21 | + public RebootCron(final CronDefinition cronDefinition) { |
| 22 | + this.cronDefinition = Preconditions.checkNotNull(cronDefinition, "CronDefinition must not be null"); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Retrieve value for cron field. |
| 27 | + * |
| 28 | + * @param name - cron field name. |
| 29 | + * If null, a NullPointerException will be raised. |
| 30 | + * @return CronField that corresponds to given CronFieldName |
| 31 | + */ |
| 32 | + public CronField retrieve(final CronFieldName name) { |
| 33 | + Preconditions.checkNotNull(name, "CronFieldName must not be null"); |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Retrieve all cron field values as map. |
| 39 | + * |
| 40 | + * @return unmodifiable Map with key CronFieldName and values CronField, never null |
| 41 | + */ |
| 42 | + public Map<CronFieldName, CronField> retrieveFieldsAsMap() { |
| 43 | + return Collections.unmodifiableMap(new HashMap<>()); |
| 44 | + } |
| 45 | + |
| 46 | + public String asString() { |
| 47 | + return "@reboot"; |
| 48 | + } |
| 49 | + |
| 50 | + public CronDefinition getCronDefinition() { |
| 51 | + return cronDefinition; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Validates this Cron instance by validating its cron expression. |
| 56 | + * |
| 57 | + * @return this Cron instance |
| 58 | + * @throws IllegalArgumentException if the cron expression is invalid |
| 59 | + */ |
| 60 | + public Cron validate() { |
| 61 | + for (final Map.Entry<CronFieldName, CronField> field : retrieveFieldsAsMap().entrySet()) { |
| 62 | + final CronFieldName fieldName = field.getKey(); |
| 63 | + field.getValue().getExpression().accept( |
| 64 | + new ValidationFieldExpressionVisitor(getCronDefinition().getFieldDefinition(fieldName).getConstraints()) |
| 65 | + ); |
| 66 | + } |
| 67 | + for (final CronConstraint constraint : getCronDefinition().getCronConstraints()) { |
| 68 | + if (!constraint.validate(this)) { |
| 69 | + throw new IllegalArgumentException(String.format("Invalid cron expression: %s. %s", asString(), constraint.getDescription())); |
| 70 | + } |
| 71 | + } |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Provides means to compare if two cron expressions are equivalent. |
| 77 | + * |
| 78 | + * @param cronMapper - maps 'cron' parameter to this instance definition; |
| 79 | + * @param cron - any cron instance, never null |
| 80 | + * @return boolean - true if equivalent; false otherwise. |
| 81 | + */ |
| 82 | + public boolean equivalent(final CronMapper cronMapper, final Cron cron) { |
| 83 | + return asString().equals(cronMapper.map(cron).asString()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Provides means to compare if two cron expressions are equivalent. |
| 88 | + * Assumes same cron definition. |
| 89 | + * |
| 90 | + * @param cron - any cron instance, never null |
| 91 | + * @return boolean - true if equivalent; false otherwise. |
| 92 | + */ |
| 93 | + public boolean equivalent(final Cron cron) { |
| 94 | + return asString().equals(cron.asString()); |
| 95 | + } |
| 96 | +} |
0 commit comments