openwfe.org.time.CronLine Maven / Gradle / Ivy
/*
* Copyright (c) 2005-2006, John Mettraux, OpenWFE.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name of the "OpenWFE" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: CronLine.java 2713 2006-06-01 14:38:45Z jmettraux $
*/
//
// CronLine.java
//
// [email protected]
//
// generated with
// jtmpl 1.1.01 2004/05/19 ([email protected])
//
package openwfe.org.time;
import java.util.Calendar;
/**
* A 'cron line' is a line like you find in a crontab file (man 5 crontab).
*
* CVS Info :
*
$Author: jmettraux $
*
$Id: CronLine.java 2713 2006-06-01 14:38:45Z jmettraux $
*
* @author [email protected]
*/
public class CronLine
{
/*
private final static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(CronLine.class.getName());
*/
//
// CONSTANTS & co
//
// FIELDS
private int[] minutes = null;
private int[] hours = null;
private int[] days = null;
private int[] months = null;
private int[] daysOfWeek = null;
//
// CONSTRUCTORS
protected CronLine ()
{
super();
}
//
// METHODS
public boolean matches (final long t)
{
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(t);
//
// minutes
if (noMatch(cal.get(Calendar.MINUTE), minutes))
return false;
//
// hours
if (noMatch(cal.get(Calendar.HOUR), hours))
return false;
//
// days
if (noMatch(cal.get(Calendar.DAY_OF_MONTH), days))
return false;
//
// months
if (noMatch(cal.get(Calendar.MONTH), months))
return false;
//
// days of week
if (noMatch(cal.get(Calendar.DAY_OF_WEEK), daysOfWeek))
return false;
return true;
}
private static boolean noMatch (final int value, final int[] cronValues)
{
if (cronValues == null) return false;
//log.debug("noMatch() "+value+" in "+dumpIntArray(cronValues)+" ?");
for (int i=0; i"+s+"<");
if (s.trim().equals("*")) return null;
if (s.indexOf(",") > -1)
{
return parseList(s, min, max);
}
if (s.indexOf("-") > -1 ||
s.indexOf("*") > -1)
{
return parseRange(s, min, max);
}
int i = Integer.parseInt(s);
if (i < min) i = min;
else if (i > max) i = max;
return new int[] { i };
}
private static int[] parseList
(final String s, final int min, final int max)
{
final String[] ss = s.split(",");
final int[] result = new int[ss.length];
for (int i=0; i max) result[i] = max;
}
return result;
}
private static int[] parseRange
(final String s, final int min, final int max)
{
final int i = s.indexOf("-");
final int j = s.indexOf("/");
int inc = 1;
if (j > -1) inc = Integer.parseInt(s.substring(j+1));
int start = -1;
int end = -1;
if (i > -1)
{
start = Integer.parseInt(s.substring(0, i));
if (j > -1)
end = Integer.parseInt(s.substring(i+1, j));
else
end = Integer.parseInt(s.substring(i+1));
}
else // case : */x
{
start = min;
end = max;
}
if (start < min) start = min;
if (end > max) end = max;
final java.util.List list = new java.util.LinkedList();
//log.debug("parseRange() start : "+start);
//log.debug("parseRange() end : "+end);
//log.debug("parseRange() inc : "+inc);
for (int value = start; value <= end; value = value + inc)
{
//log.debug("parseRange() v : "+value);
list.add(new Integer(value));
}
//
// return result as an int array
int index = 0;
final int[] result = new int[list.size()];
final java.util.Iterator it = list.iterator();
while (it.hasNext())
result[index++] = ((Integer)it.next()).intValue();
return result;
}
}