com.github.kagkarlsson.shaded.cronutils.model.RebootCron Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db-scheduler Show documentation
Show all versions of db-scheduler Show documentation
Simple persistent scheduler for scheduled tasks, recurring or ad-hoc.
package com.github.kagkarlsson.shaded.cronutils.model;
import com.github.kagkarlsson.shaded.cronutils.mapper.CronMapper;
import com.github.kagkarlsson.shaded.cronutils.model.definition.CronConstraint;
import com.github.kagkarlsson.shaded.cronutils.model.definition.CronDefinition;
import com.github.kagkarlsson.shaded.cronutils.model.field.CronField;
import com.github.kagkarlsson.shaded.cronutils.model.field.CronFieldName;
import com.github.kagkarlsson.shaded.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor;
import com.github.kagkarlsson.shaded.cronutils.utils.Preconditions;
import java.util.*;
public class RebootCron implements Cron {
private static final long serialVersionUID = 7487370826825439099L;
private final CronDefinition cronDefinition;
/**
* Creates a Cron with the given cron definition and the given fields.
* @param cronDefinition the definition to use for this Cron
*/
public RebootCron(final CronDefinition cronDefinition) {
this.cronDefinition = Preconditions.checkNotNull(cronDefinition, "CronDefinition must not be null");
}
/**
* Retrieve value for cron field.
*
* @param name - cron field name.
* If null, a NullPointerException will be raised.
* @return CronField that corresponds to given CronFieldName
*/
public CronField retrieve(final CronFieldName name) {
Preconditions.checkNotNull(name, "CronFieldName must not be null");
return null;
}
/**
* Retrieve all cron field values as map.
*
* @return unmodifiable Map with key CronFieldName and values CronField, never null
*/
public Map retrieveFieldsAsMap() {
return Collections.unmodifiableMap(new HashMap<>());
}
public String asString() {
return "@reboot";
}
public CronDefinition getCronDefinition() {
return cronDefinition;
}
/**
* 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 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;
}
/**
* Provides means to compare if two cron expressions are equivalent.
*
* @param cronMapper - maps 'cron' parameter to this instance definition;
* @param cron - any cron instance, never null
* @return boolean - true if equivalent; false otherwise.
*/
public boolean equivalent(final CronMapper cronMapper, final Cron cron) {
return asString().equals(cronMapper.map(cron).asString());
}
/**
* Provides means to compare if two cron expressions are equivalent.
* Assumes same cron definition.
*
* @param cron - any cron instance, never null
* @return boolean - true if equivalent; false otherwise.
*/
public boolean equivalent(final Cron cron) {
return asString().equals(cron.asString());
}
}