1 17 18 22 23 package org.quartz.impl.calendar; 24 25 import java.io.Serializable ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.Comparator ; 29 import java.util.Iterator ; 30 import java.util.TimeZone ; 31 32 import org.quartz.Calendar; 33 34 45 public class AnnualCalendar extends BaseCalendar implements Calendar, 46 Serializable { 47 48 static final long serialVersionUID = 7346867105876610961L; 49 50 private ArrayList excludeDays = new ArrayList (); 51 52 private boolean dataSorted = false; 54 55 public AnnualCalendar() { 56 } 57 58 public AnnualCalendar(Calendar baseCalendar) { 59 super(baseCalendar); 60 } 61 62 public AnnualCalendar(TimeZone timeZone) { 63 super(timeZone); 64 } 65 66 public AnnualCalendar(Calendar baseCalendar, TimeZone timeZone) { 67 super(baseCalendar, timeZone); 68 } 69 70 75 public ArrayList getDaysExcluded() { 76 return excludeDays; 77 } 78 79 84 public boolean isDayExcluded(java.util.Calendar day) { 85 if (day == null) { 86 throw new IllegalArgumentException ( 87 "Parameter day must not be null"); 88 } 89 90 int dmonth = day.get(java.util.Calendar.MONTH); 91 int dday = day.get(java.util.Calendar.DAY_OF_MONTH); 92 93 if (dataSorted == false) { 94 Collections.sort(excludeDays, new CalendarComparator()); 95 dataSorted = true; 96 } 97 98 Iterator iter = excludeDays.iterator(); 99 while (iter.hasNext()) { 100 java.util.Calendar cl = (java.util.Calendar ) iter.next(); 101 102 if (dmonth < cl.get(java.util.Calendar.MONTH)) { 104 return false; 105 } 106 107 if (dday != cl.get(java.util.Calendar.DAY_OF_MONTH)) { 108 continue; 109 } 110 111 if (dmonth != cl.get(java.util.Calendar.MONTH)) { 112 continue; 113 } 114 115 return true; 116 } 117 118 return false; 119 } 120 121 127 public void setDaysExcluded(ArrayList days) { 128 if (days == null) { 129 excludeDays = new ArrayList (); 130 } 131 132 excludeDays = days; 133 dataSorted = false; 134 } 135 136 141 public void setDayExcluded(java.util.Calendar day, boolean exclude) { 142 if (isDayExcluded(day)) { 143 return; 144 } 145 146 excludeDays.add(day); 147 dataSorted = false; 148 } 149 150 160 public boolean isTimeIncluded(long timeStamp) { 161 if (super.isTimeIncluded(timeStamp) == false) { return false; } 164 165 java.util.Calendar day = createJavaCalendar(timeStamp); 166 167 return !(isDayExcluded(day)); 168 } 169 170 181 public long getNextIncludedTime(long timeStamp) { 182 long baseTime = super.getNextIncludedTime(timeStamp); 184 if ((baseTime > 0) && (baseTime > timeStamp)) { 185 timeStamp = baseTime; 186 } 187 188 java.util.Calendar day = getStartOfDayJavaCalendar(timeStamp); 190 if (isDayExcluded(day) == false) { 191 return timeStamp; } 193 194 while (isDayExcluded(day) == true) { 195 day.add(java.util.Calendar.DATE, 1); 196 } 197 198 return day.getTime().getTime(); 199 } 200 } 201 202 class CalendarComparator implements Comparator { 203 public CalendarComparator() { 204 } 205 206 209 public int compare(Object arg0, Object arg1) { 210 java.util.Calendar c1 = (java.util.Calendar ) arg0; 211 java.util.Calendar c2 = (java.util.Calendar ) arg1; 212 213 if(c1.before(c2)) { 214 return -1; 215 } else if(c1.after(c2)) { 216 return 1; 217 } else { 218 return 0; 219 } 220 } 221 } 222 | Popular Tags |