Search in sources :

Example 1 with SingleCron

use of com.cronutils.model.SingleCron 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 SingleCron

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

Example 3 with SingleCron

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

the class CronDescriptorTest method testDescribeAtXHours.

@Test
public void testDescribeAtXHours() {
    final int hour = 11;
    final List<CronField> results = new ArrayList<>();
    results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MINUTE, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.SECOND, FieldExpression.always(), nullFieldConstraints));
    assertEquals(String.format("at %s:00", hour), descriptor.describe(new SingleCron(mockDefinition, results)));
}
Also used : 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)

Example 4 with SingleCron

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

the class CronDescriptorTest method testEveryMinuteBetweenMonths.

@Test
public void testEveryMinuteBetweenMonths() {
    final int monthStart = 2;
    final int monthEnd = 3;
    final List<CronField> results = new ArrayList<>();
    results.add(new CronField(CronFieldName.HOUR, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MINUTE, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MONTH, new Between(new IntegerFieldValue(monthStart), new IntegerFieldValue(monthEnd)), nullFieldConstraints));
    assertEquals("every minute every month between February and March", descriptor.describe(new SingleCron(mockDefinition, results)));
}
Also used : Between(com.cronutils.model.field.expression.Between) ArrayList(java.util.ArrayList) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField) Test(org.junit.Test)

Example 5 with SingleCron

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

the class CronDescriptorTest method testNearestWeekdayToNthOfMonth.

@Test
public void testNearestWeekdayToNthOfMonth() {
    final int dayOfMonth = 22;
    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 IntegerFieldValue(dayOfMonth), new SpecialCharFieldValue(SpecialChar.W)), nullFieldConstraints));
    assertEquals(String.format("at %s:%s the nearest weekday to the %s of the month", hour, minute, dayOfMonth), 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

SingleCron (com.cronutils.model.SingleCron)13 CronField (com.cronutils.model.field.CronField)13 ArrayList (java.util.ArrayList)11 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)10 Test (org.junit.Test)10 On (com.cronutils.model.field.expression.On)9 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)4 Between (com.cronutils.model.field.expression.Between)3 CompositeCron (com.cronutils.model.CompositeCron)2 CronFieldName (com.cronutils.model.field.CronFieldName)2 Cron (com.cronutils.model.Cron)1 Every (com.cronutils.model.field.expression.Every)1