Search in sources :

Example 6 with VisibleForTesting

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

the class TimeNode method getNearestForwardValue.

/**
 * We return same reference value if matches or next one if does not match.
 * Then we start applying shifts.
 * This way we ensure same value is returned if no shift is requested.
 *
 * @param reference     - reference value
 * @param shiftsToApply - shifts to apply
 * @return NearestValue instance, never null. Holds information on nearest (forward) value and shifts performed.
 */
@VisibleForTesting
NearestValue getNearestForwardValue(final int reference, int shiftsToApply) {
    final List<Integer> temporaryValues = new ArrayList<>(this.values);
    int index = 0;
    boolean foundGreater = false;
    final AtomicInteger shift = new AtomicInteger(0);
    if (!temporaryValues.contains(reference)) {
        for (final Integer value : temporaryValues) {
            if (value > reference) {
                index = temporaryValues.indexOf(value);
                // we just moved a position!
                shiftsToApply--;
                foundGreater = true;
                break;
            }
        }
        if (!foundGreater) {
            shift.incrementAndGet();
        }
    } else {
        index = temporaryValues.indexOf(reference);
    }
    int value = temporaryValues.get(index);
    for (int j = 0; j < shiftsToApply; j++) {
        value = getValueFromList(temporaryValues, index + 1, shift);
        index = temporaryValues.indexOf(value);
    }
    return new NearestValue(value, shift.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Example 7 with VisibleForTesting

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

the class FieldParser method parseOnWithLW.

@VisibleForTesting
protected On parseOnWithLW(final String exp) {
    final SpecialCharFieldValue specialChar = new SpecialCharFieldValue(LW);
    final String lwExpression = exp.replace(LW_STRING, EMPTY_STRING);
    if (EMPTY_STRING.equals(lwExpression)) {
        return new On(new IntegerFieldValue(-1), specialChar, new IntegerFieldValue(-1));
    } else {
        throw new IllegalArgumentException(String.format("Expected: LW, found: %s", lwExpression));
    }
}
Also used : SpecialCharFieldValue(com.cronutils.model.field.value.SpecialCharFieldValue) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Example 8 with VisibleForTesting

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

the class CronBuilder method addField.

@VisibleForTesting
CronBuilder addField(final CronFieldName name, final FieldExpression expression) {
    checkState(definition != null, "CronBuilder not initialized.");
    final FieldDefinition fieldDefinition = definition.getFieldDefinition(name);
    checkState(fieldDefinition != null, "Cron field definition does not exist: %s", name);
    final FieldConstraints constraints = fieldDefinition.getConstraints();
    expression.accept(new ValidationFieldExpressionVisitor(constraints));
    fields.put(name, new CronField(name, expression, constraints));
    return this;
}
Also used : FieldDefinition(com.cronutils.model.field.definition.FieldDefinition) ValidationFieldExpressionVisitor(com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor) FieldConstraints(com.cronutils.model.field.constraint.FieldConstraints) CronField(com.cronutils.model.field.CronField) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Aggregations

VisibleForTesting (com.cronutils.utils.VisibleForTesting)8 IntegerFieldValue (com.cronutils.model.field.value.IntegerFieldValue)3 SpecialCharFieldValue (com.cronutils.model.field.value.SpecialCharFieldValue)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CronField (com.cronutils.model.field.CronField)1 FieldConstraints (com.cronutils.model.field.constraint.FieldConstraints)1 FieldDefinition (com.cronutils.model.field.definition.FieldDefinition)1 ValidationFieldExpressionVisitor (com.cronutils.model.field.expression.visitor.ValidationFieldExpressionVisitor)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Instant (java.time.Instant)1 Matcher (java.util.regex.Matcher)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 ResourceAlreadyExistsException (org.opensearch.ResourceAlreadyExistsException)1 ActionListener (org.opensearch.action.ActionListener)1