Search in sources :

Example 1 with CronField

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

the class CronMapper method map.

/**
 * Maps given cron to target cron definition.
 *
 * @param cron - Instance to be mapped;
 *             if null a NullPointerException will be raised
 * @return new Cron instance, never null;
 */
public Cron map(final Cron cron) {
    Preconditions.checkNotNull(cron, "Cron must not be null");
    final List<CronField> fields = new ArrayList<>();
    for (final CronFieldName name : CronFieldName.values()) {
        if (mappings.containsKey(name)) {
            final CronField field = mappings.get(name).apply(cron.retrieve(name));
            if (field != null) {
                fields.add(field);
            }
        }
    }
    return cronRules.apply(new SingleCron(to, fields)).validate();
}
Also used : CronFieldName(com.cronutils.model.field.CronFieldName) ArrayList(java.util.ArrayList) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField)

Example 2 with CronField

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

the class EveryDayOfWeekValueGeneratorTest method setUp.

@Before
public void setUp() {
    FieldConstraints constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    // every 2 days between 1-5
    final CronField cronField = new CronField(CronFieldName.DAY_OF_WEEK, new Every(new Between(new IntegerFieldValue(1), new IntegerFieldValue(5)), new IntegerFieldValue(2)), constraints);
    // Using monday = 1
    WeekDay mondayDoWValue = new WeekDay(1, false);
    // so MON-FRI/2, which should translate to MON,WED,FRI
    validDow = EnumSet.of(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.FRIDAY);
    fieldValueGenerator = new EveryDayOfWeekValueGenerator(cronField, year, month, mondayDoWValue);
}
Also used : WeekDay(com.cronutils.mapper.WeekDay) Between(com.cronutils.model.field.expression.Between) Every(com.cronutils.model.field.expression.Every) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) CronField(com.cronutils.model.field.CronField) Before(org.junit.Before)

Example 3 with CronField

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

the class EveryFieldValueGeneratorTest method setUp.

@Before
public void setUp() {
    constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    fieldValueGenerator = new EveryFieldValueGenerator(new CronField(CronFieldName.HOUR, new Every(new IntegerFieldValue(TIME)), constraints));
}
Also used : Every(com.cronutils.model.field.expression.Every) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) CronField(com.cronutils.model.field.CronField) Before(org.junit.Before)

Example 4 with CronField

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

the class AndFieldValueGeneratorTest method setUp.

@Before
public void setUp() {
    constraints = FieldConstraintsBuilder.instance().createConstraintsInstance();
    fieldValueGenerator = new AndFieldValueGenerator(new CronField(CronFieldName.MONTH, new And().and(new On(new IntegerFieldValue(VALUE0))).and(new On(new IntegerFieldValue(VALUE1))).and(new On(new IntegerFieldValue(VALUE2))), constraints));
}
Also used : And(com.cronutils.model.field.expression.And) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Before(org.junit.Before)

Example 5 with CronField

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

the class CronDescriptorTest method testLastDayOfMonth.

@Test
public void testLastDayOfMonth() {
    final int hour = 10;
    final int minute = 15;
    final List<CronField> results = new ArrayList<>();
    results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MINUTE, new On(new IntegerFieldValue(minute)), nullFieldConstraints));
    results.add(new CronField(CronFieldName.DAY_OF_MONTH, new On(new SpecialCharFieldValue(SpecialChar.L)), nullFieldConstraints));
    assertEquals(String.format("at %s:%s last day of month", hour, minute), descriptor.describe(new SingleCron(mockDefinition, results)));
}
Also used : SpecialCharFieldValue(com.cronutils.model.field.value.SpecialCharFieldValue) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) On(com.cronutils.model.field.expression.On) Test(org.junit.Test)

Aggregations

CronField (com.cronutils.model.field.CronField)34 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)18 Test (org.junit.Test)16 On (com.cronutils.model.field.expression.On)15 SingleCron (com.cronutils.model.SingleCron)13 ArrayList (java.util.ArrayList)13 Before (org.junit.Before)7 Between (com.cronutils.model.field.expression.Between)6 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)5 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)4 WeekDay (com.cronutils.mapper.WeekDay)3 Cron (com.cronutils.model.Cron)3 CronFieldName (com.cronutils.model.field.CronFieldName)3 Every (com.cronutils.model.field.expression.Every)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 CompositeCron (com.cronutils.model.CompositeCron)2 And (com.cronutils.model.field.expression.And)2 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)2 CronConstraint (com.cronutils.model.definition.CronConstraint)1 FieldConstraintsBuilder (com.cronutils.model.field.constraint.FieldConstraintsBuilder)1