📄 crontabentrybean.java
字号:
/** Description getter
* @return the Description of this CrontabBean
*/
public String getDescription(){
return description;
}
/**
* Parses a string describing this time table entry and sets the
* neded variables in order to build a CrontabEntry.
* Crontab Line usually something similar to:
* * * * * * org.jcrontab.jcrontab
* @param entry the line to parse
* @throws CrontabEntryException Error parsing the string
*/
public void setLine(String entry) throws CrontabEntryException {
this.entry = entry;
StringTokenizer tokenizer = new StringTokenizer(entry);
int numTokens = tokenizer.countTokens();
for(int i = 0; tokenizer.hasMoreElements(); i++) {
String token = tokenizer.nextToken();
switch(i) {
case 0: // Minutes
minutes = token;
parseToken(token,bMinutes,false);
break;
case 1: // Hours
hours = token;
parseToken(token,bHours,false);
break;
case 2: // Days of month
daysOfMonth = token;
parseToken(token,bDaysOfMonth,true);
break;
case 3: // Months
months = token;
parseToken(token,bMonths,true);
break;
case 4: // Days of week
daysOfWeek = token;
parseToken(token,bDaysOfWeek,false);
break;
case 5: // Name of the class
try {
int index = token.indexOf("#");
if(index > 0) {
StringTokenizer tokenize = new StringTokenizer(token, "#");
className = tokenize.nextToken();
methodName = tokenize.nextToken();
break;
} else {
className=token;
//methodName="NULL";
break;
}
} catch (Exception e) {
throw new CrontabEntryException(entry);
} finally{
break;
}
case 6: // Extra Information
extraInfo = new String[numTokens - 6];
bextraInfo = true;
for(extraInfo[i - 6] = token; tokenizer.hasMoreElements();
extraInfo[i - 6] = tokenizer.nextToken()) {
i++;
}
for (int y = 0; y < extraInfo.length ; y++) {
}
break;
default:
break;
}
}
// At least 6 token
if(numTokens<6) {
throw new CrontabEntryException("The number of items is < 6 at " + entry);
}
}
/**
* Parses a string describing this time table entry
* @return String describing the time table entry usuarlly something like:
* * * * * * org.jcrontab.jcrontab
* @throws CrontabEntryException Error parsing the string
*/
public String getLine() throws CrontabEntryException {
final StringBuffer sb = new StringBuffer();
sb.append(minutes + " ");
sb.append(hours + " ");
sb.append(daysOfMonth + " ");
sb.append(months + " ");
sb.append(daysOfWeek + " ");
//if (methodName.equals("NULL")){
if ("".equals(methodName)) {
sb.append(className + " ");
} else {
sb.append(className + "#" + methodName + " ");
}
if (bextraInfo) {
for (int i = 0; i < extraInfo.length ; i++) {
sb.append(extraInfo[i] + " ");
}
}
return sb.toString();
}
/**
* Parses a token and fills the array of booleans that represents this
* CrontabEntryBean
* @param token String to parser usually smth like [ * , 2-3 , 2,3,4 ,4/5 ]
* @param arrayBool this array is the most efficient way to compare entries
* @bBeginInOne says if the array begins in 0 or in 1
* @throws CrontabEntryException Error parsing the string
*/
private void parseToken(String token, boolean[] arrayBool,
boolean bBeginInOne)
throws CrontabEntryException {
int i;
try
{
if(token.equals("*")) {
for(i=0; i<arrayBool.length; i++) {
arrayBool[i] = true;
}
return;
}
int index = token.indexOf(",");
if(index > 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<arrayBool.length; j+= each)
arrayBool[j] = true;
return;
}
else
{
int iValue = Integer.parseInt(token);
if(bBeginInOne) {
iValue--;
}
arrayBool[iValue] = true;
return;
}
}
catch(Exception e)
{
throw new CrontabEntryException( "Smth was wrong with " + token );
}
}
/** The simplest representation of a CrontabBean in a String
* @return the resulting String
*/
public String toString() {
try {
return getLine();
} catch (Exception e) {
return e.toString();
}
}
/** Represents the CrotnabEntryBean in XML format
* @return the returning XML
*/
public String toXML(){
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter, true);
toXML(printWriter);
return stringWriter.toString();
}
/** Returns the XML that represents this Crontab EntryBean
* @param pw The printWritter to write the XML
*/
public void toXML(PrintWriter pw) {
pw.println("<crontabentry>");
pw.println("<id>" + id + "</id> ");
pw.println("<hours>" + hours + "</hours> ");
pw.println("<minutes>" + minutes + "</minutes> ");
pw.println("<month>" + months + "</month> ");
pw.println("<daysofweek>" + daysOfWeek + "</daysofweek> ");
pw.println("<daysofmonth>" + daysOfMonth + "</daysofmonth> ");
pw.println("<classname>" + className + "</classname> ");
pw.println("<methodname>" + methodName + "</methodname> ");
if (bextraInfo) {
for (int i = 0; i < extraInfo.length ; i++) {
pw.println("<extrainfo parameter = \"" + i + "\" >");
pw.println(extraInfo[i] + " </extrainfo>");
}
}
pw.println("<description>" + description + "</description> ");
pw.println("</crontabentry>");
}
/**
* Returns true if the time table entry matchs with the calendar given
* @param cal Calendar to compare with the time table entry
* @return true if the time table entry matchs with the calendar given
*/
public boolean equals(Calendar cal) {
// IMPORTANT: Day of week and day of month in Calendar begin in
// 1, not in 0. Thats why we decrement them
return ( bHours[cal.get(Calendar.HOUR_OF_DAY)] &&
bMinutes[cal.get(Calendar.MINUTE)] &&
bMonths[cal.get(Calendar.MONTH)] &&
bDaysOfWeek[cal.get(Calendar.DAY_OF_WEEK)-1] &&
bDaysOfMonth[cal.get(Calendar.DAY_OF_MONTH)-1]);
}
/**
* Returns true if the CrontabEntryBean equals the given
* @param ceb CrontabEntryBean to compare with the CrontabEntryBean
* @return true if the CrontabEntryBean entry equals the CrontabEntryBean given
*/
/**
this.months == ceb.getMonths() &&
this.daysOfWeek == ceb.getDaysOfWeek() &&
this.daysOfMonth == ceb.getDaysOfMonth() &&
this.className == ceb.getClassName() &&
this.extraInfo.length == ceb.getExtraInfo().length) {
checker = true;
String[] theOther = ceb.getExtraInfo();
for (int i = 0 ; i < theOther.length ; i++) {
if (this.extraInfo[i] == theOther[i])
checker = true;
else
return false;
}
*/
public boolean equals(CrontabEntryBean ceb) {
boolean checker = false;
if (this.id == ceb.getId()){
return true;
} else {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -