KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > calendar > Day


1 package org.jbpm.calendar;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.DateFormat JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Calendar JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14  * is a day on a business calendar.
15  */

16 public class Day implements Serializable JavaDoc {
17   
18   private static final long serialVersionUID = 1L;
19   
20   DayPart[] dayParts = null;
21   BusinessCalendar businessCalendar = null;
22
23   public static Day[] parseWeekDays(Properties JavaDoc calendarProperties, BusinessCalendar businessCalendar) {
24     DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc(calendarProperties.getProperty("hour.format"));
25     Day[] weekDays = new Day[8];
26     weekDays[Calendar.MONDAY] = new Day( calendarProperties.getProperty("weekday.monday"), dateFormat, businessCalendar );
27     weekDays[Calendar.TUESDAY] = new Day( calendarProperties.getProperty("weekday.thuesday"), dateFormat, businessCalendar );
28     weekDays[Calendar.WEDNESDAY] = new Day( calendarProperties.getProperty("weekday.wednesday"), dateFormat, businessCalendar );
29     weekDays[Calendar.THURSDAY] = new Day( calendarProperties.getProperty("weekday.thursday"), dateFormat, businessCalendar );
30     weekDays[Calendar.FRIDAY] = new Day( calendarProperties.getProperty("weekday.friday"), dateFormat, businessCalendar );
31     weekDays[Calendar.SATURDAY] = new Day( calendarProperties.getProperty("weekday.saturday"), dateFormat, businessCalendar );
32     weekDays[Calendar.SUNDAY] = new Day( calendarProperties.getProperty("weekday.sunday"), dateFormat, businessCalendar );
33     return weekDays;
34   }
35
36   public Day(String JavaDoc dayPartsText, DateFormat JavaDoc dateFormat, BusinessCalendar businessCalendar) {
37     this.businessCalendar = businessCalendar;
38     
39     List JavaDoc dayPartsList = new ArrayList JavaDoc();
40     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(dayPartsText, "&");
41     while (tokenizer.hasMoreTokens()) {
42       String JavaDoc dayPartText = tokenizer.nextToken().trim();
43       dayPartsList.add(new DayPart(dayPartText, dateFormat, this, dayPartsList.size()));
44     }
45     
46     dayParts = (DayPart[]) dayPartsList.toArray(new DayPart[dayPartsList.size()]);
47   }
48
49   public void findNextDayPartStart(int dayPartIndex, Date JavaDoc date, Object JavaDoc[] result) {
50     // if there is a day part in this day that starts after the given date
51
if (dayPartIndex < dayParts.length) {
52       if (dayParts[dayPartIndex].isStartAfter(date)) {
53         result[0] = dayParts[dayPartIndex].getStartTime(date);
54         result[1] = dayParts[dayPartIndex];
55       } else {
56         findNextDayPartStart(dayPartIndex+1, date, result);
57       }
58     } else {
59       // descend recustively
60
date = businessCalendar.findStartOfNextDay(date);
61       Day nextDay = businessCalendar.findDay(date);
62       nextDay.findNextDayPartStart(0, date, result);
63     }
64   }
65 }
66
Popular Tags