This commit is contained in:
Looly 2022-03-20 23:36:14 +08:00
parent b1920b6fa2
commit be72eab9a0
8 changed files with 55 additions and 41 deletions

View File

@ -152,7 +152,6 @@ public class CronPattern {
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
*/
public boolean match(GregorianCalendar calendar, boolean isMatchSecond) {
final int second = calendar.get(Calendar.SECOND);
final int minute = calendar.get(Calendar.MINUTE);
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
@ -162,7 +161,7 @@ public class CronPattern {
boolean eval;
for (int i = 0; i < matcherSize; i++) {
eval = ((false == isMatchSecond) || secondMatchers.get(i).match(second)) // 匹配秒非秒匹配模式下始终返回true
eval = ((false == isMatchSecond) || secondMatchers.get(i).match(calendar.get(Calendar.SECOND))) // 匹配秒非秒匹配模式下始终返回true
&& minuteMatchers.get(i).match(minute)// 匹配分
&& hourMatchers.get(i).match(hour)// 匹配时
&& isMatchDayOfMonth(dayOfMonthMatchers.get(i), dayOfMonth, month, calendar.isLeapYear(year))// 匹配日

View File

@ -4,20 +4,22 @@ import cn.hutool.cron.CronException;
/**
* 星期值处理<br>
* 1表示星期一2表示星期二依次类推0和7都可以表示星期日
*
* @author Looly
* 1表示星期一2表示星期二依次类推0和7都可以表示星期日<br>
* {@code L}表示周六
*
* @author Looly
*/
public class DayOfWeekValueParser extends SimpleValueParser {
/** Weeks aliases. */
private static final String[] ALIASES = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
public class DayOfWeekValueParser extends SimpleValueParser {
/**
* Weeks aliases.
*/
private static final String[] ALIASES = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
public DayOfWeekValueParser() {
super(0, 7);
}
/**
* 对于星期提供转换<br>
* 1表示星期一2表示星期二依次类推0和7都可以表示星期日
@ -33,16 +35,17 @@ public class DayOfWeekValueParser extends SimpleValueParser {
/**
* 解析别名
*
* @param value 别名值
* @return 月份int值
* @throws CronException 无效别名抛出此异常
*/
private int parseAlias(String value) throws CronException {
if("L".equalsIgnoreCase(value)){
if ("L".equalsIgnoreCase(value)) {
//最后一天为星期六
return ALIASES.length - 1;
}
for (int i = 0; i < ALIASES.length; i++) {
if (ALIASES[i].equalsIgnoreCase(value)) {
return i;

View File

@ -1,12 +1,13 @@
package cn.hutool.cron.pattern.parser;
/**
* 小时值处理
* @author Looly
* 小时值处理<br>
* 小时被限定在0-23
*
* @author Looly
*/
public class HourValueParser extends SimpleValueParser{
public class HourValueParser extends SimpleValueParser {
public HourValueParser() {
super(0, 23);
}

View File

@ -1,12 +1,13 @@
package cn.hutool.cron.pattern.parser;
/**
* 分钟值处理
* @author Looly
* 分钟值处理<br>
* 限定于0-59
*
* @author Looly
*/
public class MinuteValueParser extends SimpleValueParser{
public class MinuteValueParser extends SimpleValueParser {
public MinuteValueParser() {
super(0, 59);
}

View File

@ -3,20 +3,22 @@ package cn.hutool.cron.pattern.parser;
import cn.hutool.cron.CronException;
/**
* 月份值处理
*
* @author Looly
* 月份值处理<br>
* 限定于1-121表示一月支持别名如一月是{@code jan}
*
* @author Looly
*/
public class MonthValueParser extends SimpleValueParser {
/** Months aliases. */
private static final String[] ALIASES = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
/**
* Months aliases.
*/
private static final String[] ALIASES = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
public MonthValueParser() {
super(1, 12);
}
@Override
public int parse(String value) throws CronException {
try {
@ -28,6 +30,7 @@ public class MonthValueParser extends SimpleValueParser {
/**
* 解析别名
*
* @param value 别名值
* @return 月份int值
* @throws CronException 无效月别名抛出此异常

View File

@ -1,9 +1,10 @@
package cn.hutool.cron.pattern.parser;
/**
* 秒值处理
* @author Looly
* 秒值处理<br>
* 限定于0-59
*
* @author Looly
*/
public class SecondValueParser extends MinuteValueParser{
public class SecondValueParser extends MinuteValueParser {
}

View File

@ -3,15 +3,20 @@ package cn.hutool.cron.pattern.parser;
import cn.hutool.cron.CronException;
/**
* 简易值转换器将给定String值转为int
* @author Looly
* 简易值转换器将给定String值转为int并限定最大值和最小值<br>
* 此类同时识别{@code L} 为最大值
*
* @author Looly
*/
public class SimpleValueParser implements ValueParser {
/** 最小值(包括) */
/**
* 最小值包括
*/
protected int min;
/** 最大值(包括) */
/**
* 最大值包括
*/
protected int max;
/**
@ -21,10 +26,10 @@ public class SimpleValueParser implements ValueParser {
* @param max 最大值包括
*/
public SimpleValueParser(int min, int max) {
if(min > max){
if (min > max) {
this.min = max;
this.max = min;
}else{
} else {
this.min = min;
this.max = max;
}
@ -32,7 +37,7 @@ public class SimpleValueParser implements ValueParser {
@Override
public int parse(String value) throws CronException {
if("L".equalsIgnoreCase(value)){
if ("L".equalsIgnoreCase(value)) {
// L表示最大值
return max;
}

View File

@ -1,12 +1,13 @@
package cn.hutool.cron.pattern.parser;
/**
* 年值处理
* @author Looly
* 年值处理<br>
* 年的限定在1970-2099年
*
* @author Looly
*/
public class YearValueParser extends SimpleValueParser{
public class YearValueParser extends SimpleValueParser {
public YearValueParser() {
super(1970, 2099);
}