Search in sources :

Example 1 with On

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

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

the class OnDayOfWeekValueGenerator method generateNextValue.

@Override
public int generateNextValue(final int reference) throws NoSuchValueException {
    final On on = ((On) cronField.getExpression());
    final int value = generateValue(on, year, month, reference);
    if (value <= reference) {
        throw new NoSuchValueException();
    }
    return value;
}
Also used : On(com.cronutils.model.field.expression.On)

Example 3 with On

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

the class OnDayOfWeekValueGenerator method generatePreviousValue.

@Override
public int generatePreviousValue(final int reference) throws NoSuchValueException {
    final On on = ((On) cronField.getExpression());
    final int value = generateValue(on, year, month, reference);
    if (value >= reference) {
        throw new NoSuchValueException();
    }
    return value;
}
Also used : On(com.cronutils.model.field.expression.On)

Example 4 with On

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

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

On (com.cronutils.model.field.expression.On)21 CronField (com.cronutils.model.field.CronField)15 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)13 Test (org.junit.Test)10 SingleCron (com.cronutils.model.SingleCron)9 ArrayList (java.util.ArrayList)9 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)5 Before (org.junit.Before)4 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)2 Between (com.cronutils.model.field.expression.Between)2 Every (com.cronutils.model.field.expression.Every)2 Function (com.cronutils.Function)1 WeekDay (com.cronutils.mapper.WeekDay)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 FieldExpression (com.cronutils.model.field.expression.FieldExpression)1 MessageFormat (java.text.MessageFormat)1 DayOfWeek (java.time.DayOfWeek)1 Month (java.time.Month)1