📄 nthincludeddaytrigger.java
字号:
* * @return a boolean indicator of whether the trigger could potentially fire * again */ public boolean mayFireAgain() { return (getNextFireTime() != null); } /** * Indicates whether <CODE>misfireInstruction</CODE> is a valid misfire * instruction for this <CODE>Trigger</CODE>. * * @return whether <CODE>misfireInstruction</CODE> is valid. */ protected boolean validateMisfireInstruction(int misfireInstruction) { if ((misfireInstruction == MISFIRE_INSTRUCTION_SMART_POLICY) || (misfireInstruction == MISFIRE_INSTRUCTION_DO_NOTHING) || (misfireInstruction == MISFIRE_INSTRUCTION_FIRE_ONCE_NOW)) { return true; } else { return false; } } /** * Updates the <CODE>NthIncludedDayTrigger</CODE>'s state based on the * MISFIRE_INSTRUCTION_XXX that was selected when the * <CODE>NthIncludedDayTrigger</CODE> was created * <P> * If the misfire instruction is set to MISFIRE_INSTRUCTION_SMART_POLICY, * then the instruction will be interpreted as * {@link #MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}. * * @param calendar a new or updated calendar to use for the trigger */ public void updateAfterMisfire(Calendar calendar) { int instruction = getMisfireInstruction(); this.calendar = calendar; if (instruction == MISFIRE_INSTRUCTION_SMART_POLICY) { instruction = MISFIRE_INSTRUCTION_FIRE_ONCE_NOW; } if (instruction == MISFIRE_INSTRUCTION_DO_NOTHING) { this.nextFireTime = getFireTimeAfter(new Date()); } else if (instruction == MISFIRE_INSTRUCTION_FIRE_ONCE_NOW) { this.nextFireTime = new Date(); } } /** * Updates the <CODE>NthIncludedDayTrigger</CODE>'s state based on the * given new version of the associated <CODE>Calendar</CODE>. * * @param calendar a new or updated calendar to use for the trigger * @param misfireThreshold the amount of time (in milliseconds) that must * be between "now" and the time the next * firing of the trigger is supposed to occur. */ public void updateWithNewCalendar(Calendar calendar, long misfireThreshold) { Date now = new Date(); long diff; this.calendar = calendar; this.nextFireTime = getFireTimeAfter(this.previousFireTime); if ((this.nextFireTime != null) && (this.nextFireTime.before(now))) { diff = now.getTime() - this.nextFireTime.getTime(); if (diff >= misfireThreshold) { this.nextFireTime = getFireTimeAfter(this.nextFireTime); } } } /** * Calculates the first time an <CODE>NthIncludedDayTrigger</CODE> with * <CODE>intervalType = {@link #INTERVAL_TYPE_WEEKLY}</CODE> will fire * after the specified date. See {@link #getNextFireTime} for more * information. * * @param afterDate The time after which to find the nearest fire time. * This argument is treated as exclusive — that is, * if afterTime is a valid fire time for the trigger, it * will not be returned as the next fire time. * @return the first time the trigger will fire following the specified * date */ private Date getWeeklyFireTimeAfter(Date afterDate) { int currN = 0; java.util.Calendar afterCal; java.util.Calendar currCal; int currWeek; int weekCount = 0; boolean gotOne = false; afterCal = java.util.Calendar.getInstance(getTimeZone()); afterCal.setTime(afterDate); currCal = java.util.Calendar.getInstance(getTimeZone()); currCal.set(afterCal.get(java.util.Calendar.YEAR), afterCal.get(java.util.Calendar.MONTH), afterCal.get(java.util.Calendar.DAY_OF_MONTH)); //move to the first day of the week (SUNDAY) currCal.add(java.util.Calendar.DAY_OF_MONTH, (afterCal.get(java.util.Calendar.DAY_OF_WEEK) - 1) * -1); currCal.set(java.util.Calendar.HOUR_OF_DAY, this.fireAtHour); currCal.set(java.util.Calendar.MINUTE, this.fireAtMinute); currCal.set(java.util.Calendar.SECOND, this.fireAtSecond); currCal.set(java.util.Calendar.MILLISECOND, 0); currWeek = currCal.get(java.util.Calendar.WEEK_OF_YEAR); while ((!gotOne) && (weekCount < this.nextFireCutoffInterval)) { while ((currN != this.n) && (weekCount < 12)) { //if we move into a new week, reset the current "n" counter if (currCal.get(java.util.Calendar.WEEK_OF_YEAR) != currWeek) { currN = 0; weekCount++; currWeek = currCal.get(java.util.Calendar.WEEK_OF_YEAR); } //treating a null calendar as an all-inclusive calendar, // increment currN if the current date being tested is included // on the calendar if ((calendar == null) || (calendar.isTimeIncluded(currCal.getTime().getTime()))) { currN++; } if (currN != this.n) { currCal.add(java.util.Calendar.DATE, 1); } //if we pass endTime, drop out and return null. if ((this.endTime != null) && (currCal.getTime().after(this.endTime))) { return null; } } //We found an "n" or we've checked the requisite number of weeks. // If we've found an "n", is it the right one? -- that is, we could // be looking at an nth day PRIOR to afterDate if (currN == this.n) { if (afterDate.before(currCal.getTime())) { gotOne = true; } else { //resume checking on the first day of the next week currCal.add(java.util.Calendar.DAY_OF_MONTH, -1 * (currN - 1)); currCal.add(java.util.Calendar.DAY_OF_MONTH, 7); currN = 0; } } } if (weekCount < this.nextFireCutoffInterval) { return currCal.getTime(); } else { return null; } } /** * Calculates the first time an <CODE>NthIncludedDayTrigger</CODE> with * <CODE>intervalType = {@link #INTERVAL_TYPE_MONTHLY}</CODE> will fire * after the specified date. See {@link #getNextFireTime} for more * information. * * @param afterDate The time after which to find the nearest fire time. * This argument is treated as exclusive — that is, * if afterTime is a valid fire time for the trigger, it * will not be returned as the next fire time. * @return the first time the trigger will fire following the specified * date */ private Date getMonthlyFireTimeAfter(Date afterDate) { int currN = 0; java.util.Calendar afterCal; java.util.Calendar currCal; int currMonth; int monthCount = 0; boolean gotOne = false; afterCal = java.util.Calendar.getInstance(getTimeZone()); afterCal.setTime(afterDate); currCal = java.util.Calendar.getInstance(getTimeZone()); currCal.set(afterCal.get(java.util.Calendar.YEAR), afterCal.get(java.util.Calendar.MONTH), 1); currCal.set(java.util.Calendar.HOUR_OF_DAY, this.fireAtHour); currCal.set(java.util.Calendar.MINUTE, this.fireAtMinute); currCal.set(java.util.Calendar.SECOND, this.fireAtSecond); currCal.set(java.util.Calendar.MILLISECOND, 0); currMonth = currCal.get(java.util.Calendar.MONTH); while ((!gotOne) && (monthCount < this.nextFireCutoffInterval)) { while ((currN != this.n) && (monthCount < 12)) { //if we move into a new month, reset the current "n" counter if (currCal.get(java.util.Calendar.MONTH) != currMonth) { currN = 0; monthCount++; currMonth = currCal.get(java.util.Calendar.MONTH); } //treating a null calendar as an all-inclusive calendar, // increment currN if the current date being tested is included // on the calendar if ((calendar == null) || (calendar.isTimeIncluded(currCal.getTime().getTime()))) { currN++; } if (currN != this.n) { currCal.add(java.util.Calendar.DATE, 1); } //if we pass endTime, drop out and return null. if ((this.endTime != null) && (currCal.getTime().after(this.endTime))) { return null; } } //We found an "n" or we've checked the requisite number of months. // If we've found an "n", is it the right one? -- that is, we could // be looking at an nth day PRIOR to afterDate if (currN == this.n) { if (afterDate.before(currCal.getTime())) { gotOne = true; } else { //resume checking on the first day of the next month currCal.set(java.util.Calendar.DAY_OF_MONTH, 1); currCal.add(java.util.Calendar.MONTH, 1); currN = 0; } } } if (monthCount < this.nextFireCutoffInterval) { return currCal.getTime(); } else { return null; } } /** * Calculates the first time an <CODE>NthIncludedDayTrigger</CODE> with * <CODE>intervalType = {@link #INTERVAL_TYPE_YEARLY}</CODE> will fire * after the specified date. See {@link #getNextFireTime} for more * information. * * @param afterDate The time after which to find the nearest fire time. * This argument is treated as exclusive — that is, * if afterTime is a valid fire time for the trigger, it * will not be returned as the next fire time. * @return the first time the trigger will fire following the specified * date */ private Date getYearlyFireTimeAfter(Date afterDate) { int currN = 0; java.util.Calendar afterCal; java.util.Calendar currCal; int currYear; int yearCount = 0; boolean gotOne = false; afterCal = java.util.Calendar.getInstance(getTimeZone()); afterCal.setTime(afterDate); currCal = java.util.Calendar.getInstance(getTimeZone()); currCal.set(afterCal.get(java.util.Calendar.YEAR), java.util.Calendar.JANUARY, 1); currCal.set(java.util.Calendar.HOUR_OF_DAY, this.fireAtHour); currCal.set(java.util.Calendar.MINUTE, this.fireAtMinute); currCal.set(java.util.Calendar.SECOND, this.fireAtSecond); currCal.set(java.util.Calendar.MILLISECOND, 0); currYear = currCal.get(java.util.Calendar.YEAR); while ((!gotOne) && (yearCount < this.nextFireCutoffInterval)) { while ((currN != this.n) && (yearCount < 5)) { //if we move into a new year, reset the current "n" counter if (currCal.get(java.util.Calendar.YEAR) != currYear) { currN = 0; yearCount++; currYear = currCal.get(java.util.Calendar.YEAR); } //treating a null calendar as an all-inclusive calendar, // increment currN if the current date being tested is included // on the calendar if ((calendar == null) || (calendar.isTimeIncluded(currCal.getTime().getTime()))) { currN++; } if (currN != this.n) { currCal.add(java.util.Calendar.DATE, 1); } //if we pass endTime, drop out and return null. if ((this.endTime != null) && (currCal.getTime().after(this.endTime))) { return null; } } //We found an "n" or we've checked the requisite number of years. // If we've found an "n", is it the right one? -- that is, we // could be looking at an nth day PRIOR to afterDate if (currN == this.n) { if (afterDate.before(currCal.getTime())) { gotOne = true; } else { //resume checking on the first day of the next year currCal.set(java.util.Calendar.DAY_OF_MONTH, 1); currCal.set(java.util.Calendar.MONTH, java.util.Calendar.JANUARY); currCal.add(java.util.Calendar.YEAR, 1); currN = 0; } } } if (yearCount < this.nextFireCutoffInterval) { return currCal.getTime(); } else { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -