KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > scheduler > event > DiaryPeriod


1 /*
2  * Copyright (C) 2001 - 2005 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

22 package com.scalagent.scheduler.event;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Calendar JavaDoc;
26
27 /**
28  * A <code>DiaryPeriod</code> object defines a time period with a choice of
29  * user meaningful time units. The available time units are the relevant
30  * constants in class <code>Calendar</code>.
31  */

32 public class DiaryPeriod implements Serializable JavaDoc {
33   /**
34    * Time unit for this period, as a <code>Calendar</code> constant.
35    * Available constants are <code>YEAR</code>, <code>MONTH</code>,
36    * <code>DAY_OF_MONTH</code>, <code>HOUR_OF_DAY</code>, <code>MINUTE</code>,
37    * <code>SECOND</code>, and <code>MILLISECOND</code>.
38    * <p>
39    * Defaults to <code>MILLISECOND</code>.
40    */

41   public int unit = Calendar.MILLISECOND;
42
43   /**
44    * Period length as a number of <code>unit</code>s.
45    * <p>
46    * Defaults to <code>0</code>.
47    */

48   public int value = 0;
49
50   /**
51    * Default constructor.
52    * Creates a null length period.
53    */

54   public DiaryPeriod() {}
55
56   /**
57    * Constructor setting the period unit and length.
58    *
59    * @param unit period time unit as a <code>Calendar</code> constant
60    * @param value period length as a number of <code>unit</code>s
61    */

62   public DiaryPeriod(int unit, int value) {
63     switch (unit) {
64     case Calendar.YEAR:
65     case Calendar.MONTH:
66     case Calendar.DAY_OF_MONTH:
67     case Calendar.HOUR_OF_DAY:
68     case Calendar.MINUTE:
69     case Calendar.SECOND:
70     case Calendar.MILLISECOND:
71       break;
72     default:
73       throw new IllegalArgumentException JavaDoc("unknown diary period unit");
74     }
75     this.unit = unit;
76     this.value = value;
77   }
78
79   /**
80    * Constructor inferring the period unit and length from a number of
81    * milliseconds. Recognized units are the second (1000), the minute (60000),
82    * the hour (3600000), the day (86400000), the month (2592000000), and the
83    * year (31536000000).
84    *
85    * @param value period length as a number of milliseconds
86    */

87   public DiaryPeriod(long value) {
88     final long MS_SECOND = 1000;
89     final long MS_MINUTE = 60 * MS_SECOND;
90     final long MS_HOUR = 60 * MS_MINUTE;
91     final long MS_DAY = 24 * MS_HOUR;
92     final long MS_MONTH = 30 * MS_DAY;
93     final long MS_YEAR = 365 * MS_DAY;
94
95     if (value == 0) {
96       // uses default values
97
} else if ((value % MS_YEAR) == 0) {
98       this.unit = Calendar.YEAR;
99       this.value = (int) (value / MS_YEAR);
100     } else if ((value % MS_MONTH) == 0) {
101       this.unit = Calendar.MONTH;
102       this.value = (int) (value / MS_MONTH);
103     } else if ((value % MS_DAY) == 0) {
104       this.unit = Calendar.DAY_OF_MONTH;
105       this.value = (int) (value / MS_DAY);
106     } else if ((value % MS_HOUR) == 0) {
107       this.unit = Calendar.HOUR_OF_DAY;
108       this.value = (int) (value / MS_HOUR);
109     } else if ((value % MS_MINUTE) == 0) {
110       this.unit = Calendar.MINUTE;
111       this.value = (int) (value / MS_MINUTE);
112     } else if ((value % MS_SECOND) == 0) {
113       this.unit = Calendar.SECOND;
114       this.value = (int) (value / MS_SECOND);
115     } else {
116       this.unit = Calendar.MILLISECOND;
117       this.value = (int) (value);
118     }
119   }
120
121   /**
122    * Provides a string image for this object.
123    *
124    * @return string image for this object
125    */

126   public String JavaDoc toString() {
127     StringBuffer JavaDoc output = new StringBuffer JavaDoc();
128     output.append("(");
129     output.append(super.toString());
130     output.append(",unit=");
131     output.append(unit);
132     output.append(",value=");
133     output.append(value);
134     output.append(")");
135     return output.toString();
136   }
137 }
138
Popular Tags