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 class  CronSchedule.AbstractTimeValue
    Base class for timing values.
    static class  CronSchedule.NeverValue
    Never matches any time.
    static class  CronSchedule.SingleTimeValue
    Represents a single time value, e.g.
    static class  CronSchedule.TimeAll
    Represents the ALL time, *.
    static class  CronSchedule.TimeRange
    Represents a time range, e.g.
    static class  CronSchedule.TimeSteps
    Represents a time interval, e.g.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static CronSchedule NEVER
    A schedule that never matches.
    static java.lang.String NEVER_MARKER
    The schedule string that never matches.
    protected static int[] TYPES
    Types 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
    boolean equals​(java.lang.Object obj)
    java.lang.String get​(int type)
    Returns the cron-like definition string for the given time value
    protected java.util.Calendar getCalendar​(long timeStamp)
    Creates the calendar for a timestamp.
    java.lang.String getCronString()
    Returns the cron-like definition of the schedule.
    protected static int getIndex​(int type)
    Returns the index for the specified Calendar type.
    protected static int getType​(int index)
    Returns the type at the specified index
    protected CronSchedule.AbstractTimeValue[] getValues​(int type)
    Returns the values for a specific time type
    int hashCode()
    boolean isDay​(long timestamp)
    Checks whether given timestamp matches with defined day schedule.
    boolean isDay​(java.util.Calendar cal)
    Checks whether given calendar date matches with defined day schedule.
    boolean isHour​(long timestamp)
    Checks whether given timestamp matches with defined hour schedule.
    boolean isHour​(java.util.Calendar cal)
    Checks whether given calendar date matches with defined hour schedule.
    boolean isMinute​(long timeStamp)
    Checks whether given timestamp matches with defined schedule.
    boolean isMinute​(java.util.Calendar cal)
    Checks whether given calendar date matches with defined schedule.
    protected boolean matches​(int type, java.util.Calendar calendar)
    Checks whether specific schedule definition matches against the given calendar date.
    boolean matches​(long timeStamp)
    Checks whether given timestamp matches with defined schedule.
    boolean matches​(java.util.Calendar cal)
    Checks whether given timestamp matches with defined schedule.
    void set​(int type, java.lang.String values)
    Sets the time values accordingly
    protected void set​(int type, CronSchedule.AbstractTimeValue[] values)
    Sets the values for a specific type
    java.lang.String set​(java.lang.String schedule)
    Sets the cron schedule.
    java.lang.String toString()
    Returns the cron-like definition of the schedule.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • NEVER_MARKER

      public static final java.lang.String NEVER_MARKER
      The schedule string that never matches.
      See Also:
      Constant Field Values
    • TYPES

      protected static int[] TYPES
      Types being used. This array defines the types and their indices.
    • NEVER

      public static final CronSchedule 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 definition
      hour - - hour definition
      dom - - day of month definition
      mon - - month definition
      dow - - 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 set
      values - - comma-separated list of definitions for that type
    • set

      protected void set​(int type, CronSchedule.AbstractTimeValue[] values)
      Sets the values for a specific type
      Parameters:
      type - - Calendar constant defining the time type
      values - - values to be set
    • getValues

      protected CronSchedule.AbstractTimeValue[] getValues​(int type)
      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:
      toString in class java.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 for
      calendar - - 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:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.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