
com.github.stuxuhai.jcron.AsteriskParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcron Show documentation
Show all versions of jcron Show documentation
Cron expression parser for java
The newest version!
/*
* Author: Jayer
* Create Date: 2015-01-13 13:24:45
*/
package com.github.stuxuhai.jcron;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;
import com.google.common.collect.Range;
public class AsteriskParser extends AbstractParser {
private Set set;
private Set resultSet;
private Range range;
private DurationField type;
private static final Pattern ASTERISK_PATTERN = Pattern.compile("(\\d+)#(\\d+)");
protected AsteriskParser(Range range, DurationField type) {
super(range, type);
this.range = range;
this.type = type;
}
@Override
protected boolean matches(String cronFieldExp) throws ParseException {
Matcher m = ASTERISK_PATTERN.matcher(cronFieldExp);
if (m.matches()) {
int dayOfWeek = Integer.parseInt(m.group(1));
int sequence = Integer.parseInt(m.group(2));
if (range.contains(dayOfWeek) && Range.closed(1, 5).contains(sequence)) {
if (set == null) {
set = new HashSet();
}
int[] value = { dayOfWeek, sequence };
set.add(value);
return true;
} else {
throw new ParseException(String.format("Invalid value of %s: %s, out of range.", type.name, cronFieldExp), -1);
}
}
return false;
}
@Override
protected Set parse(DateTime dateTime) {
MutableDateTime mdt = dateTime.dayOfMonth().withMaximumValue().toMutableDateTime();
int maxDayOfMonth = mdt.getDayOfMonth();
mdt.setDayOfMonth(1);
int firstDayOfWeek = mdt.getDayOfWeek();
if (set != null) {
if (resultSet == null) {
resultSet = new HashSet();
}
resultSet.clear();
for (int[] value : set) {
int dayOfWeek = value[0];
int sequence = value[1];
int expectDay = 0;
if (dayOfWeek >= firstDayOfWeek) {
expectDay = dayOfWeek - firstDayOfWeek + 7 * (sequence - 1) + 1;
} else {
expectDay = dayOfWeek - firstDayOfWeek + 7 * sequence + 1;
}
if (expectDay <= maxDayOfMonth) {
resultSet.add(expectDay);
}
}
return resultSet;
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy