KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.calendar;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 /**
9  * interpretes textual descriptions of a duration.
10  * <p>Syntax: &lt;quantity&gt; [business] &lt;unit&gt; <br />
11  * Where
12  * <ul>
13  * <li>&lt;quantity&gt; is a piece of text that is parsable
14  * with <code>Double.parseDouble(quantity)</code>.
15  * </li>
16  * <li>&lt;unit&gt; is one of {second, seconds, minute, minutes,
17  * hour, hours, day, days, week, weeks, month, months, year, years}.
18  * </li>
19  * <li>And adding the optional indication <code>business</code> means that
20  * only business hours should be taken into account for this duration.
21  * </li>
22  * </ul>
23  * </p>
24  */

25 public class Duration implements Serializable JavaDoc {
26
27   private static final long serialVersionUID = 1L;
28   
29   private static final long SECOND = 1000;
30   private static final long MINUTE = 60*SECOND;
31   private static final long HOUR = 60*MINUTE;
32   private static final long DAY = 24*HOUR;
33   private static final long WEEK = 7*DAY;
34   private static final long MONTH = 30*DAY;
35   private static final long YEAR = 365*DAY;
36
37   private static long BUSINESS_DAY = 0;
38   private static long BUSINESS_WEEK = 0;
39   private static long BUSINESS_MONTH = 0;
40   private static long BUSINESS_YEAR = 0;
41   
42   static {
43     Properties JavaDoc businessCalendarProperties = BusinessCalendar.getBusinessCalendarProperties();
44     String JavaDoc businessDayText = businessCalendarProperties.getProperty("business.day.expressed.in.hours");
45     String JavaDoc businessWeekText = businessCalendarProperties.getProperty("business.week.expressed.in.hours");
46     String JavaDoc businessMonthText = businessCalendarProperties.getProperty("business.month.expressed.in.business.days");
47     String JavaDoc businessYearText = businessCalendarProperties.getProperty("business.year.expressed.in.business.days");
48     
49     BUSINESS_DAY = (long) (Double.parseDouble(businessDayText) * HOUR);
50     BUSINESS_WEEK = (long) (Double.parseDouble(businessWeekText) * HOUR);
51     BUSINESS_MONTH = (long) (Double.parseDouble(businessMonthText) * BUSINESS_DAY);
52     BUSINESS_YEAR = (long) (Double.parseDouble(businessYearText) * BUSINESS_DAY);
53   }
54   
55   private static Map JavaDoc units = null;
56   static {
57     units = new HashMap JavaDoc();
58     units.put("second", new Long JavaDoc(SECOND));
59     units.put("seconds", new Long JavaDoc(SECOND));
60     units.put("business second", new Long JavaDoc(SECOND));
61     units.put("business seconds", new Long JavaDoc(SECOND));
62     units.put("minute", new Long JavaDoc(MINUTE));
63     units.put("minutes", new Long JavaDoc(MINUTE));
64     units.put("business minute", new Long JavaDoc(MINUTE));
65     units.put("business minutes", new Long JavaDoc(MINUTE));
66     units.put("hour", new Long JavaDoc(HOUR));
67     units.put("hours", new Long JavaDoc(HOUR));
68     units.put("business hour", new Long JavaDoc(HOUR));
69     units.put("business hours", new Long JavaDoc(HOUR));
70     units.put("day", new Long JavaDoc(DAY));
71     units.put("days", new Long JavaDoc(DAY));
72     units.put("business day", new Long JavaDoc(BUSINESS_DAY));
73     units.put("business days", new Long JavaDoc(BUSINESS_DAY));
74     units.put("week", new Long JavaDoc(WEEK));
75     units.put("weeks", new Long JavaDoc(WEEK));
76     units.put("business week", new Long JavaDoc(BUSINESS_WEEK));
77     units.put("business weeks", new Long JavaDoc(BUSINESS_WEEK));
78     units.put("month", new Long JavaDoc(MONTH));
79     units.put("months", new Long JavaDoc(MONTH));
80     units.put("business month", new Long JavaDoc(BUSINESS_MONTH));
81     units.put("business months", new Long JavaDoc(BUSINESS_MONTH));
82     units.put("year", new Long JavaDoc(YEAR));
83     units.put("years", new Long JavaDoc(YEAR));
84     units.put("business year", new Long JavaDoc(BUSINESS_YEAR));
85     units.put("business years", new Long JavaDoc(BUSINESS_YEAR));
86   }
87   
88   long milliseconds = 0;
89   boolean isBusinessTime = false;
90   
91   Duration() {
92   }
93
94   public Duration(long milliseconds) {
95     this.milliseconds = milliseconds;
96   }
97
98   public Duration(Duration duration) {
99     this.milliseconds = duration.milliseconds;
100     this.isBusinessTime = duration.isBusinessTime;
101   }
102
103   /**
104    * creates a duration from a textual description.
105    * syntax: {number} space {unit}
106    * where number is parsable to a java.lang.Number and
107    * unit is one of
108    * <ul>
109    * <li>second</li>
110    * <li>seconds</li>
111    * <li>minute</li>
112    * <li>minutes</li>
113    * <li>hour</li>
114    * <li>hours</li>
115    * <li>day</li>
116    * <li>days</li>
117    * <li>week</li>
118    * <li>weeks</li>
119    * <li>month (30 days)</li>
120    * <li>months (30 days)</li>
121    * <li>year (355 days)</li>
122    * <li>years (355 days)</li>
123    * </ul>
124    */

125   public Duration(String JavaDoc duration) {
126     if (duration==null) throw new NullPointerException JavaDoc("duration is null");
127     int separatorIndex = duration.indexOf(' ');
128     if (separatorIndex==-1) throw new IllegalArgumentException JavaDoc("improper format of duration '"+duration+"'");
129     String JavaDoc quantityText = duration.substring(0, separatorIndex).trim().toLowerCase();
130     String JavaDoc unitText = duration.substring(separatorIndex+1).trim().toLowerCase();
131     if (unitText.startsWith("business")) {
132       isBusinessTime = true;
133    }
134     double amount = Double.parseDouble(quantityText);
135     Long JavaDoc unit = (Long JavaDoc) units.get(unitText);
136     this.milliseconds = (long) (amount * unit.longValue());
137   }
138 }
139
Popular Tags