Search in sources :

Example 1 with VisibleForTesting

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

the class TimeNode method getNearestBackwardValue.

/**
 * We return same reference value if matches or previous 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 (backward) value and shifts performed.
 */
@VisibleForTesting
NearestValue getNearestBackwardValue(final int reference, int shiftsToApply) {
    final List<Integer> temporaryValues = new ArrayList<>(this.values);
    Collections.reverse(temporaryValues);
    int index = 0;
    boolean foundSmaller = 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--;
                foundSmaller = true;
                break;
            }
        }
        if (!foundSmaller) {
            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 2 with VisibleForTesting

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

the class FieldParser method parseOnWithQuestionMark.

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

Example 3 with VisibleForTesting

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

the class FieldParser method parseOnWithHash.

@VisibleForTesting
protected On parseOnWithHash(final String exp) {
    if (!fieldConstraints.getSpecialChars().contains(HASH)) {
        throw new IllegalArgumentException("Invalid expression: " + exp);
    }
    final SpecialCharFieldValue specialChar = new SpecialCharFieldValue(HASH);
    final String[] array = exp.split(HASH_TAG);
    if (array.length == 0) {
        throw new IllegalArgumentException("Invalid Position of # Character!");
    }
    final IntegerFieldValue nth = mapToIntegerFieldValue(array[1]);
    if (array[0].isEmpty()) {
        throw new IllegalArgumentException("Time should be specified!");
    }
    return new On(mapToIntegerFieldValue(array[0]), specialChar, nth);
}
Also used : SpecialCharFieldValue(com.cronutils.model.field.value.SpecialCharFieldValue) IntegerFieldValue(com.cronutils.model.field.value.IntegerFieldValue) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Example 4 with VisibleForTesting

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

the class StringValidations method removeValidChars.

@VisibleForTesting
public String removeValidChars(final String exp) {
    final Matcher numsAndCharsMatcher = NUMS_AND_CHARS_PATTERN.matcher(exp.toUpperCase());
    final Matcher stringToIntKeysMatcher = stringToIntKeysPattern.matcher(numsAndCharsMatcher.replaceAll(""));
    final Matcher specialWordsMatcher = lwPattern.matcher(stringToIntKeysMatcher.replaceAll(""));
    return specialWordsMatcher.replaceAll("").replaceAll("\\s+", "").replaceAll(",", "").replaceAll("-", "");
}
Also used : Matcher(java.util.regex.Matcher) VisibleForTesting(com.cronutils.utils.VisibleForTesting)

Example 5 with VisibleForTesting

use of com.cronutils.utils.VisibleForTesting in project job-scheduler by opensearch-project.

the class LockService method createLockIndex.

@VisibleForTesting
void createLockIndex(ActionListener<Boolean> listener) {
    if (lockIndexExist()) {
        listener.onResponse(true);
    } else {
        final CreateIndexRequest request = new CreateIndexRequest(LOCK_INDEX_NAME).mapping(lockMapping());
        client.admin().indices().create(request, ActionListener.wrap(response -> listener.onResponse(response.isAcknowledged()), exception -> {
            if (exception instanceof ResourceAlreadyExistsException || exception.getCause() instanceof ResourceAlreadyExistsException) {
                listener.onResponse(true);
            } else {
                listener.onFailure(exception);
            }
        }));
    }
}
Also used : SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) ToXContent(org.opensearch.common.xcontent.ToXContent) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ActionListener(org.opensearch.action.ActionListener) LockModel(org.opensearch.jobscheduler.spi.LockModel) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Client(org.opensearch.client.Client) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) IOException(java.io.IOException) DocumentMissingException(org.opensearch.index.engine.DocumentMissingException) Instant(java.time.Instant) ScheduledJobParameter(org.opensearch.jobscheduler.spi.ScheduledJobParameter) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) Logger(org.apache.logging.log4j.Logger) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) VisibleForTesting(com.cronutils.utils.VisibleForTesting) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) JobExecutionContext(org.opensearch.jobscheduler.spi.JobExecutionContext) BufferedReader(java.io.BufferedReader) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) 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