Search in sources :

Example 1 with FieldExpression

use of com.cronutils.model.field.expression.FieldExpression 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 FieldExpression

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

the class CronTest method testAsString.

@Test
public void testAsString() {
    final String expressionString = "somestring";
    final FieldExpression mockFieldExpression = mock(FieldExpression.class);
    when(mockField.getExpression()).thenReturn(mockFieldExpression);
    when(mockFieldExpression.asString()).thenReturn(expressionString);
    assertEquals(expressionString, cron.asString());
}
Also used : FieldExpression(com.cronutils.model.field.expression.FieldExpression) Test(org.junit.Test)

Example 3 with FieldExpression

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

the class ValueMappingFieldExpressionVisitorTest method testVisitQuestionMark.

@Test
public void testVisitQuestionMark() {
    final FieldExpression param = FieldExpression.questionMark();
    final QuestionMark questionMark = (QuestionMark) param.accept(valueMappingFieldExpressionVisitor);
    // always the same cause of singleton pattern
    assertTrue(param == questionMark);
}
Also used : QuestionMark(com.cronutils.model.field.expression.QuestionMark) FieldExpression(com.cronutils.model.field.expression.FieldExpression) Test(org.junit.Test)

Example 4 with FieldExpression

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

the class AndDayOfWeekValueGenerator method generateCandidatesNotIncludingIntervalExtremes.

@Override
protected List<Integer> generateCandidatesNotIncludingIntervalExtremes(final int start, final int end) {
    final List<Integer> values = new ArrayList<>();
    final And and = (And) cronField.getExpression();
    for (final FieldExpression expression : and.getExpressions()) {
        final CronField cronField = new CronField(CronFieldName.DAY_OF_WEEK, expression, this.cronField.getConstraints());
        final List<Integer> candidatesList = FieldValueGeneratorFactory.createDayOfWeekValueGeneratorInstance(cronField, year, month, mondayDoWValue).generateCandidates(start, end);
        // add them to the master list
        if (candidatesList != null) {
            values.addAll(candidatesList);
        }
    }
    return values;
}
Also used : And(com.cronutils.model.field.expression.And) ArrayList(java.util.ArrayList) FieldExpression(com.cronutils.model.field.expression.FieldExpression) CronField(com.cronutils.model.field.CronField)

Aggregations

FieldExpression (com.cronutils.model.field.expression.FieldExpression)4 Test (org.junit.Test)2 Function (com.cronutils.Function)1 CronField (com.cronutils.model.field.CronField)1 DayOfWeekFieldDefinition (com.cronutils.model.field.definition.DayOfWeekFieldDefinition)1 FieldDefinition (com.cronutils.model.field.definition.FieldDefinition)1 And (com.cronutils.model.field.expression.And)1 Every (com.cronutils.model.field.expression.Every)1 On (com.cronutils.model.field.expression.On)1 QuestionMark (com.cronutils.model.field.expression.QuestionMark)1 MessageFormat (java.text.MessageFormat)1 DayOfWeek (java.time.DayOfWeek)1 Month (java.time.Month)1 TextStyle (java.time.format.TextStyle)1 ArrayList (java.util.ArrayList)1 ResourceBundle (java.util.ResourceBundle)1