All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebeaninternal.server.lib.cron.CronSchedule Maven / Gradle / Ivy

/**
 *  Copyright (C) 2006  Robin Bygrave
 *  
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
package com.avaje.ebeaninternal.server.lib.cron;

import java.util.Calendar;
import java.util.StringTokenizer;

/**
 * A cron like syntax for scheduling.
 * 
 * 

It uses 5 values to indicate the schedule in terms of Hours, Minutes, * Months, DaysOfWeek and DaysOfMonth. * Each of the 5 values can include characters '*', ',' and '-' where

*
    *
  • * - indicates to fire on all Hours, Minutes, Months etc *
  • 0,5,10 - indicates to fire on the 0th, 5th and 10th Hour, Minute, Month etc *
  • 6-8,12 - indicates to fire on the 6th through to 8th and 12th Hour, Minute, Month etc *
*

* e.g. 59 23 * * * == fire every day at 23:59
* e.g. 0-5,15,45 12,13 * * * == fire in the hours of 12 and 13, and minutes 0 thru 5, 15 and 45.
*

*

Hours are between 0 and 23, Minutes are between 0 and 59, Months are between 0 and 11, * DaysOfWeek are between 0 and 6 starting with Sunday as 0, DaysOfMonth are between 0 and 30. *

*/ public class CronSchedule { private String schedule; // boolean arrays to help the schedule evaluation. private boolean[] bHours; private boolean[] bMinutes; private boolean[] bMonths; private boolean[] bDaysOfWeek; private boolean[] bDaysOfMonth; /** * Create the CronSchedule specifying the schedule in cron like format. * * @param scheduleLine the cron like schedule. Example 23 59 * * *. */ public CronSchedule(String scheduleLine) { setSchedule(scheduleLine); } public boolean equals(Object obj) { if (obj == null){ return false; } if (obj instanceof CronSchedule){ CronSchedule cs = (CronSchedule)obj; if (schedule.equals(cs.getSchedule())){ return true; } } return false; } public int hashCode() { int hc = CronSchedule.class.getName().hashCode(); hc = hc*31+schedule.hashCode(); return hc; } private void initBooleanArrays() { bHours = new boolean[24]; bMinutes = new boolean[60]; bMonths = new boolean[12]; bDaysOfWeek = new boolean[7]; bDaysOfMonth = new boolean[31]; for(int i=0; i<60; i++) { if(i<24) bHours[i] = false; if(i<60) bMinutes[i] = false; if(i<12) bMonths[i] = false; if(i<7) bDaysOfWeek[i] = false; if(i<31) bDaysOfMonth[i] = false; } } /** * Set the schedule in cron like format. */ public void setSchedule(String schedule) { this.schedule = schedule; initBooleanArrays(); StringTokenizer tokenizer = new StringTokenizer(schedule); int numTokens = tokenizer.countTokens(); for(int i = 0; tokenizer.hasMoreElements(); i++) { String token = tokenizer.nextToken(); switch(i) { case 0: // Minutes parseToken(token,bMinutes,false); break; case 1: // Hours parseToken(token,bHours,false); break; case 2: // Days of month parseToken(token,bDaysOfMonth,true); break; case 3: // Months parseToken(token,bMonths,true); break; case 4: // Days of week parseToken(token,bDaysOfWeek,false); break; case 5: // Name of the class //dp("not expecting 5th token"); //className=token; break; case 6: // Extra Information //dp("not expecting 6th token"); break; default: break; } } // At least 6 token if(numTokens<5) { String msg = "The schedule["+schedule+"] did not contain enough tokens (5 required) ["+numTokens+"]."; throw new RuntimeException(msg); } } private void parseToken(String token, boolean[] arrayBool, boolean bBeginInOne) { int i; try { if(token.equals("*")) { for(i=0; i 0) { StringTokenizer tokenizer = new StringTokenizer(token, ","); while(tokenizer.hasMoreTokens()) { parseToken(tokenizer.nextToken(), arrayBool, bBeginInOne); } return; } index = token.indexOf("-"); if(index > 0) { int start = Integer.parseInt(token.substring(0, index)); int end = Integer.parseInt(token.substring(index + 1)); if(bBeginInOne) { start--; end--; } for(int j=start; j<=end; j++) arrayBool[j] = true; return; } index = token.indexOf("/"); if(index > 0) { int each = Integer.parseInt(token.substring(index + 1)); for(int j=0; j




© 2015 - 2025 Weber Informatics LLC | Privacy Policy