Package rs.baselib.util
Class CronSchedule
java.lang.Object
rs.baselib.util.CronSchedule
- All Implemented Interfaces:
java.io.Serializable
public class CronSchedule
extends java.lang.Object
implements java.io.Serializable
Provides cron-like scheduling information.
This class implements cron-like definition of scheduling information.
Various methods can be used to check whether a timestamp matches the
schedule or not. However, there is a slight difference between cron and
this class. Cron describes a match when either the day of month and month
or the day of week are met. This class requires both to be met for a match.
Also note that Calendar defines Sunday through Saturday with 1 through 7 respectively
- Author:
- RalphSchuster
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCronSchedule.AbstractTimeValueBase class for timing values.static classCronSchedule.NeverValueNever matches any time.static classCronSchedule.SingleTimeValueRepresents a single time value, e.g.static classCronSchedule.TimeAllRepresents the ALL time, *.static classCronSchedule.TimeRangeRepresents a time range, e.g.static classCronSchedule.TimeStepsRepresents a time interval, e.g. -
Field Summary
Fields Modifier and Type Field Description static CronScheduleNEVERA schedule that never matches.static java.lang.StringNEVER_MARKERThe schedule string that never matches.protected static int[]TYPESTypes being used. -
Constructor Summary
Constructors Constructor Description CronSchedule()Default constructor Constructor with all terms set to "*".CronSchedule(java.lang.String schedule)Constructor with cron-style string initialization.CronSchedule(java.lang.String min, java.lang.String hour, java.lang.String dom, java.lang.String mon, java.lang.String dow)Constructor with separate initialization values. -
Method Summary
Modifier and Type Method Description booleanequals(java.lang.Object obj)java.lang.Stringget(int type)Returns the cron-like definition string for the given time valueprotected java.util.CalendargetCalendar(long timeStamp)Creates the calendar for a timestamp.java.lang.StringgetCronString()Returns the cron-like definition of the schedule.protected static intgetIndex(int type)Returns the index for the specified Calendar type.protected static intgetType(int index)Returns the type at the specified indexprotected CronSchedule.AbstractTimeValue[]getValues(int type)Returns the values for a specific time typeinthashCode()booleanisDay(long timestamp)Checks whether given timestamp matches with defined day schedule.booleanisDay(java.util.Calendar cal)Checks whether given calendar date matches with defined day schedule.booleanisHour(long timestamp)Checks whether given timestamp matches with defined hour schedule.booleanisHour(java.util.Calendar cal)Checks whether given calendar date matches with defined hour schedule.booleanisMinute(long timeStamp)Checks whether given timestamp matches with defined schedule.booleanisMinute(java.util.Calendar cal)Checks whether given calendar date matches with defined schedule.protected booleanmatches(int type, java.util.Calendar calendar)Checks whether specific schedule definition matches against the given calendar date.booleanmatches(long timeStamp)Checks whether given timestamp matches with defined schedule.booleanmatches(java.util.Calendar cal)Checks whether given timestamp matches with defined schedule.voidset(int type, java.lang.String values)Sets the time values accordinglyprotected voidset(int type, CronSchedule.AbstractTimeValue[] values)Sets the values for a specific typejava.lang.Stringset(java.lang.String schedule)Sets the cron schedule.java.lang.StringtoString()Returns the cron-like definition of the schedule.
-
Field Details
-
NEVER_MARKER
public static final java.lang.String NEVER_MARKERThe schedule string that never matches.- See Also:
- Constant Field Values
-
TYPES
protected static int[] TYPESTypes being used. This array defines the types and their indices. -
NEVER
A schedule that never matches.This instance can be used when it is essential to have a schedule set that is never matches.
-
-
Constructor Details
-
CronSchedule
public CronSchedule()Default constructor Constructor with all terms set to "*". -
CronSchedule
public CronSchedule(java.lang.String schedule)Constructor with cron-style string initialization. The cron style is: $minute $hour $dayOfMonth $month $dayOfWeek- Parameters:
schedule- the crontab schedule
-
CronSchedule
public CronSchedule(java.lang.String min, java.lang.String hour, java.lang.String dom, java.lang.String mon, java.lang.String dow)Constructor with separate initialization values.- Parameters:
min- - minute definitionhour- - hour definitiondom- - day of month definitionmon- - month definitiondow- - day of week definition
-
-
Method Details
-
set
public java.lang.String set(java.lang.String schedule)Sets the cron schedule. The cron style is: $minute $hour $dayOfMonth $month $dayOfWeek The function will return any characters that follow the cron definition- Parameters:
schedule- - cron-like schedule definition- Returns:
- characters following the cron definition.
-
set
public void set(int type, java.lang.String values)Sets the time values accordingly- Parameters:
type- - Calendar constant to define what values will be setvalues- - comma-separated list of definitions for that type
-
set
Sets the values for a specific type- Parameters:
type- - Calendar constant defining the time typevalues- - values to be set
-
getValues
Returns the values for a specific time type- Parameters:
type- - Calendar constant defining the type- Returns:
- time value definitions
-
get
public java.lang.String get(int type)Returns the cron-like definition string for the given time value- Parameters:
type- - Calendar constant defining time type- Returns:
- cron-like definition
-
getCronString
public java.lang.String getCronString()Returns the cron-like definition of the schedule.- Returns:
- the cron-like string
- Since:
- 1.2.9
-
toString
public java.lang.String toString()Returns the cron-like definition of the schedule.- Overrides:
toStringin classjava.lang.Object- Returns:
- the cron-like string
-
matches
public boolean matches(long timeStamp)Checks whether given timestamp matches with defined schedule. This is default check method. All criteria must be met including seconds to be 0.- Parameters:
timeStamp- - time in ms since Epoch time- Returns:
- true when schedule matches
-
matches
public boolean matches(java.util.Calendar cal)Checks whether given timestamp matches with defined schedule. This is default check method. All criteria must be met including seconds to be 0.- Parameters:
cal- - calendar date- Returns:
- true when schedule matches
-
isMinute
public boolean isMinute(long timeStamp)Checks whether given timestamp matches with defined schedule. This method can be used when seconds are not relevant for matching. This is default check method.- Parameters:
timeStamp- - time in ms since Epoch time- Returns:
- true when schedule matches
-
isMinute
public boolean isMinute(java.util.Calendar cal)Checks whether given calendar date matches with defined schedule. This method can be used when seconds are not relevant for matching.- Parameters:
cal- - calendar date- Returns:
- true when schedule matches
-
isHour
public boolean isHour(long timestamp)Checks whether given timestamp matches with defined hour schedule. This method can be used when minute definition is not relevant for matching.- Parameters:
timestamp- - time in ms since Epoch time- Returns:
- true when schedule matches
-
isHour
public boolean isHour(java.util.Calendar cal)Checks whether given calendar date matches with defined hour schedule. This method can be used when minute definition is not relevant for matching.- Parameters:
cal- - calendar date- Returns:
- true when schedule matches
-
isDay
public boolean isDay(long timestamp)Checks whether given timestamp matches with defined day schedule. This method can be used when minute and hour definitions are not relevant for matching.- Parameters:
timestamp- - time in ms since Epoch time- Returns:
- true when schedule matches
-
isDay
public boolean isDay(java.util.Calendar cal)Checks whether given calendar date matches with defined day schedule. This method can be used when minute and hour definitions are not relevant for matching.- Parameters:
cal- - calendar date- Returns:
- true when schedule matches
-
matches
protected boolean matches(int type, java.util.Calendar calendar)Checks whether specific schedule definition matches against the given calendar date.- Parameters:
type- - Calendar constant defining time type to check forcalendar- - calendar representing the date to check- Returns:
- true when definition matches
-
getCalendar
protected java.util.Calendar getCalendar(long timeStamp)Creates the calendar for a timestamp.- Parameters:
timeStamp- - timestamp- Returns:
- calendar
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equalsin classjava.lang.Object
-
getType
protected static int getType(int index)Returns the type at the specified index- Parameters:
index- - index- Returns:
- Calendar constant of type
-
getIndex
protected static int getIndex(int type)Returns the index for the specified Calendar type.- Parameters:
type- - Calendar constant for type- Returns:
- internal index
-