Search in sources :

Example 1 with FieldDefinition

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

the class DescriptionStrategyFactory method daysOfWeekInstance.

/**
 * Creates description strategy for days of week.
 *
 * @param bundle     - locale
 * @param expression - CronFieldExpression
 * @return - DescriptionStrategy instance, never null
 */
public static DescriptionStrategy daysOfWeekInstance(final ResourceBundle bundle, final FieldExpression expression, final FieldDefinition definition) {
    final Function<Integer, String> nominal = integer -> {
        final int diff = definition instanceof DayOfWeekFieldDefinition ? DayOfWeek.MONDAY.getValue() - ((DayOfWeekFieldDefinition) definition).getMondayDoWValue().getMondayDoWValue() : 0;
        return DayOfWeek.of(integer + diff < 1 ? 7 : integer + diff).getDisplayName(TextStyle.FULL, bundle.getLocale());
    };
    final NominalDescriptionStrategy dow = new NominalDescriptionStrategy(bundle, nominal, expression);
    dow.addDescription(fieldExpression -> {
        if (fieldExpression instanceof On) {
            final On on = (On) fieldExpression;
            switch(on.getSpecialChar().getValue()) {
                case HASH:
                    return String.format("%s %s %s ", nominal.apply(on.getTime().getValue()), on.getNth(), bundle.getString("of_every_month"));
                case L:
                    return String.format("%s %s %s ", bundle.getString("last"), nominal.apply(on.getTime().getValue()), bundle.getString("of_every_month"));
                default:
                    return "";
            }
        }
        return "";
    });
    return dow;
}
Also used : Every(com.cronutils.model.field.expression.Every) FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) DayOfWeekFieldDefinition(com.cronutils.model.field.definition.DayOfWeekFieldDefinition) ResourceBundle(java.util.ResourceBundle) Month(java.time.Month) TextStyle(java.time.format.TextStyle) DayOfWeek(java.time.DayOfWeek) FieldExpression(com.cronutils.model.field.expression.FieldExpression) On(com.cronutils.model.field.expression.On) Function(com.cronutils.Function) MessageFormat(java.text.MessageFormat) DayOfWeekFieldDefinition(com.cronutils.model.field.definition.DayOfWeekFieldDefinition) On(com.cronutils.model.field.expression.On)

Example 2 with FieldDefinition

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

the class CronMapper method buildMappings.

/**
 * Builds functions that map the fields from source CronDefinition to target.
 *
 * @param from - source CronDefinition
 * @param to   - target CronDefinition
 */
private void buildMappings(final CronDefinition from, final CronDefinition to) {
    final Map<CronFieldName, FieldDefinition> sourceFieldDefinitions = getFieldDefinitions(from);
    final Map<CronFieldName, FieldDefinition> destFieldDefinitions = getFieldDefinitions(to);
    boolean startedDestMapping = false;
    boolean startedSourceMapping = false;
    for (final CronFieldName name : CronFieldName.values()) {
        final FieldDefinition destinationFieldDefinition = destFieldDefinitions.get(name);
        final FieldDefinition sourceFieldDefinition = sourceFieldDefinitions.get(name);
        if (destinationFieldDefinition != null) {
            startedDestMapping = true;
        }
        if (sourceFieldDefinition != null) {
            startedSourceMapping = true;
        }
        if (startedDestMapping && destinationFieldDefinition == null) {
            break;
        }
        // destination has fields before source definition starts. We default them to zero.
        if (!startedSourceMapping && destinationFieldDefinition != null) {
            mappings.put(name, returnOnZeroExpression(name));
        }
        // destination has fields after source definition was processed. We default them to always.
        if (startedSourceMapping && sourceFieldDefinition == null && destinationFieldDefinition != null) {
            mappings.put(name, returnAlwaysExpression(name));
        }
        if (sourceFieldDefinition == null || destinationFieldDefinition == null) {
            continue;
        }
        if (CronFieldName.DAY_OF_WEEK.equals(name)) {
            mappings.put(name, dayOfWeekMapping((DayOfWeekFieldDefinition) sourceFieldDefinition, (DayOfWeekFieldDefinition) destinationFieldDefinition));
        } else if (CronFieldName.DAY_OF_MONTH.equals(name)) {
            mappings.put(name, dayOfMonthMapping(sourceFieldDefinition, destinationFieldDefinition));
        } else {
            mappings.put(name, returnSameExpression());
        }
    }
}
Also used : CronFieldName(com.cronutils.model.field.CronFieldName) DayOfWeekFieldDefinition(com.cronutils.model.field.definition.DayOfWeekFieldDefinition) FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) DayOfWeekFieldDefinition(com.cronutils.model.field.definition.DayOfWeekFieldDefinition)

Example 3 with FieldDefinition

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

the class CronParserTest method parseIncompleteExpression.

private void parseIncompleteExpression(String expression, String expectedMessage) {
    final Set<FieldDefinition> set = Collections.singleton(new FieldDefinition(CronFieldName.SECOND, FieldConstraintsBuilder.instance().createConstraintsInstance()));
    when(definition.getFieldDefinitions()).thenReturn(set);
    parser = new CronParser(definition);
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage(expectedMessage);
    assertNotNull(parser.parse(expression));
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition)

Example 4 with FieldDefinition

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

the class CronParserTest method testParseNoMatchingExpression.

@Test(expected = IllegalArgumentException.class)
public void testParseNoMatchingExpression() {
    final Set<FieldDefinition> set = Collections.singleton(new FieldDefinition(CronFieldName.SECOND, FieldConstraintsBuilder.instance().createConstraintsInstance()));
    when(definition.getFieldDefinitions()).thenReturn(set);
    parser = new CronParser(definition);
    parser.parse("* *");
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) Test(org.junit.Test)

Example 5 with FieldDefinition

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

the class CronParserTest method testMultipleSpacesDoNotHurtParsingExpression.

/**
 * Corresponds to issue#11
 * https://github.com/jmrozanec/cron-utils/issues/11
 * Reported case:
 * when parsing: "* *[triple space here]* * ?"
 * we receive: NumberFormatException with message For input string: ""
 * Expected: ignore multiple spaces, and parse the expression.
 */
@Test
public void testMultipleSpacesDoNotHurtParsingExpression() {
    final FieldDefinition minute = new FieldDefinition(CronFieldName.MINUTE, FieldConstraintsBuilder.instance().createConstraintsInstance());
    final FieldDefinition hour = new FieldDefinition(CronFieldName.HOUR, FieldConstraintsBuilder.instance().createConstraintsInstance());
    final FieldDefinition dom = new FieldDefinition(CronFieldName.DAY_OF_MONTH, FieldConstraintsBuilder.instance().createConstraintsInstance());
    final FieldDefinition month = new FieldDefinition(CronFieldName.MONTH, FieldConstraintsBuilder.instance().createConstraintsInstance());
    final FieldDefinition dow = new FieldDefinition(CronFieldName.DAY_OF_WEEK, FieldConstraintsBuilder.instance().createConstraintsInstance());
    final Set<FieldDefinition> set = new HashSet<>();
    set.add(minute);
    set.add(hour);
    set.add(dom);
    set.add(month);
    set.add(dow);
    when(definition.getFieldDefinitions()).thenReturn(set);
    when(definition.getFieldDefinition(CronFieldName.MINUTE)).thenReturn(minute);
    when(definition.getFieldDefinition(CronFieldName.HOUR)).thenReturn(hour);
    when(definition.getFieldDefinition(CronFieldName.DAY_OF_MONTH)).thenReturn(dom);
    when(definition.getFieldDefinition(CronFieldName.MONTH)).thenReturn(month);
    when(definition.getFieldDefinition(CronFieldName.DAY_OF_WEEK)).thenReturn(dow);
    parser = new CronParser(definition);
    parser.parse("* *   * * *");
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

FieldDefinition (com.cronutils.model.field.definition.FieldDefinition)12 Test (org.junit.Test)7 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)6 CronFieldName (com.cronutils.model.field.CronFieldName)4 DayOfWeekFieldDefinition (com.cronutils.model.field.definition.DayOfWeekFieldDefinition)2 Function (com.cronutils.Function)1 CronField (com.cronutils.model.field.CronField)1 Every (com.cronutils.model.field.expression.Every)1 FieldExpression (com.cronutils.model.field.expression.FieldExpression)1 On (com.cronutils.model.field.expression.On)1 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)1 VisibleForTesting (com.cronutils.utils.VisibleForTesting)1 MessageFormat (java.text.MessageFormat)1 DayOfWeek (java.time.DayOfWeek)1 Month (java.time.Month)1 TextStyle (java.time.format.TextStyle)1 HashSet (java.util.HashSet)1 ResourceBundle (java.util.ResourceBundle)1 Before (org.junit.Before)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1