Search in sources :

Example 6 with SingleCron

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

the class CronDescriptorTest method testDescribeEveryXTimeUnits.

@Test
public void testDescribeEveryXTimeUnits() {
    final int time = 3;
    final Every expression = new Every(new IntegerFieldValue(time));
    assertEquals(String.format("every %s seconds", time), descriptor.describe(new SingleCron(mockDefinition, Collections.singletonList(new CronField(CronFieldName.SECOND, expression, nullFieldConstraints)))));
    assertEquals(String.format("every %s minutes", time), descriptor.describe(new SingleCron(mockDefinition, Collections.singletonList(new CronField(CronFieldName.MINUTE, expression, nullFieldConstraints)))));
    final List<CronField> params = new ArrayList<>();
    params.add(new CronField(CronFieldName.HOUR, expression, nullFieldConstraints));
    params.add(new CronField(CronFieldName.MINUTE, new On(new IntegerFieldValue(time)), nullFieldConstraints));
    assertEquals(String.format("every %s hours at minute %s", time, time), descriptor.describe(new SingleCron(mockDefinition, params)));
}
Also used : Every(com.cronutils.model.field.expression.Every) 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 7 with SingleCron

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

the class CronParser method parse.

/**
 * Parse string with cron expression.
 *
 * @param expression - cron expression, never null
 * @return Cron instance, corresponding to cron expression received
 * @throws java.lang.IllegalArgumentException if expression does not match cron definition
 */
public Cron parse(final String expression) {
    Preconditions.checkNotNull(expression, "Expression must not be null");
    final String replaced = expression.replaceAll("\\s+", " ").trim();
    if (StringUtils.isEmpty(replaced)) {
        throw new IllegalArgumentException("Empty expression!");
    }
    if (expression.contains("||")) {
        List<Cron> crons = Arrays.stream(expression.split("\\|\\|")).map(this::parse).collect(Collectors.toList());
        return new CompositeCron(crons);
    }
    if (expression.contains("|")) {
        List<String> crons = new ArrayList<>();
        int cronscount = Arrays.stream(expression.split("\\s+")).mapToInt(s -> s.split("\\|").length).max().orElse(0);
        for (int j = 0; j < cronscount; j++) {
            StringBuilder builder = new StringBuilder();
            for (String s : expression.split("\\s+")) {
                if (s.contains("|")) {
                    builder.append(String.format("%s ", s.split("\\|")[j]));
                } else {
                    builder.append(String.format("%s ", s));
                }
            }
            crons.add(builder.toString().trim());
        }
        return new CompositeCron(crons.stream().map(this::parse).collect(Collectors.toList()));
    } else {
        final String[] expressionParts = replaced.toUpperCase().split(" ");
        final int expressionLength = expressionParts.length;
        String fieldWithTrailingCommas = Arrays.stream(expressionParts).filter(x -> x.endsWith(",")).findAny().orElse(null);
        if (fieldWithTrailingCommas != null) {
            throw new IllegalArgumentException(String.format("Invalid field value! Trailing commas not permitted! '%s'", fieldWithTrailingCommas));
        }
        final List<CronParserField> fields = expressions.get(expressionLength);
        if (fields == null) {
            throw new IllegalArgumentException(String.format("Cron expression contains %s parts but we expect one of %s", expressionLength, expressions.keySet()));
        }
        try {
            final int size = expressionParts.length;
            final List<CronField> results = new ArrayList<>(size + 1);
            for (int j = 0; j < size; j++) {
                results.add(fields.get(j).parse(expressionParts[j]));
            }
            return new SingleCron(cronDefinition, results).validate();
        } catch (final IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Failed to parse cron expression. %s", e.getMessage()), e);
        }
    }
}
Also used : CompositeCron(com.cronutils.model.CompositeCron) SingleCron(com.cronutils.model.SingleCron) CompositeCron(com.cronutils.model.CompositeCron) SingleCron(com.cronutils.model.SingleCron) Cron(com.cronutils.model.Cron) CronField(com.cronutils.model.field.CronField)

Example 8 with SingleCron

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

the class ExecutionTime method forCron.

/**
 * Creates execution time for given Cron.
 *
 * @param cron - Cron instance
 * @return ExecutionTime instance
 */
public static ExecutionTime forCron(final Cron cron) {
    if (cron instanceof SingleCron) {
        final Map<CronFieldName, CronField> fields = cron.retrieveFieldsAsMap();
        final ExecutionTimeBuilder executionTimeBuilder = new ExecutionTimeBuilder(cron);
        for (final CronFieldName name : CronFieldName.values()) {
            if (fields.get(name) != null) {
                switch(name) {
                    case SECOND:
                        executionTimeBuilder.forSecondsMatching(fields.get(name));
                        break;
                    case MINUTE:
                        executionTimeBuilder.forMinutesMatching(fields.get(name));
                        break;
                    case HOUR:
                        executionTimeBuilder.forHoursMatching(fields.get(name));
                        break;
                    case DAY_OF_WEEK:
                        executionTimeBuilder.forDaysOfWeekMatching(fields.get(name));
                        break;
                    case DAY_OF_MONTH:
                        executionTimeBuilder.forDaysOfMonthMatching(fields.get(name));
                        break;
                    case MONTH:
                        executionTimeBuilder.forMonthsMatching(fields.get(name));
                        break;
                    case YEAR:
                        executionTimeBuilder.forYearsMatching(fields.get(name));
                        break;
                    case DAY_OF_YEAR:
                        executionTimeBuilder.forDaysOfYearMatching(fields.get(name));
                        break;
                    default:
                        break;
                }
            }
        }
        return executionTimeBuilder.build();
    } else {
        return new CompositeExecutionTime(((CompositeCron) cron).getCrons().parallelStream().map(ExecutionTime::forCron).collect(Collectors.toList()));
    }
}
Also used : CronFieldName(com.cronutils.model.field.CronFieldName) CompositeCron(com.cronutils.model.CompositeCron) SingleCron(com.cronutils.model.SingleCron) CronField(com.cronutils.model.field.CronField)

Example 9 with SingleCron

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

the class CronDescriptorTest method testEverySecondInMonth.

@Test
public void testEverySecondInMonth() {
    final int month = 2;
    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.SECOND, FieldExpression.always(), nullFieldConstraints));
    results.add(new CronField(CronFieldName.MONTH, new On(new IntegerFieldValue(month)), nullFieldConstraints));
    assertEquals("every second at February month", 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 10 with SingleCron

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

the class CronDescriptorTest method testDescribeEveryXMinutesBetweenTime.

@Test
public void testDescribeEveryXMinutesBetweenTime() {
    final int hour = 11;
    final int start = 0;
    final int end = 10;
    final Between expression = new Between(new IntegerFieldValue(start), new IntegerFieldValue(end));
    final List<CronField> results = new ArrayList<>();
    results.add(new CronField(CronFieldName.MINUTE, expression, nullFieldConstraints));
    results.add(new CronField(CronFieldName.HOUR, new On(new IntegerFieldValue(hour)), nullFieldConstraints));
    assertEquals(String.format("every minute between %s:%02d and %s:%02d", hour, start, hour, end), 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) 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