KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > Millisecond


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------
27  * Millisecond.java
28  * ----------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: Millisecond.java,v 1.5 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-Oct-2001 : Version 1 (DG);
39  * 19-Dec-2001 : Added new constructors as suggested by Paul English (DG);
40  * 26-Feb-2002 : Added new getStart() and getEnd() methods (DG);
41  * 29-Mar-2002 : Fixed bug in getStart(), getEnd() and compareTo() methods (DG);
42  * 10-Sep-2002 : Added getSerialIndex() method (DG);
43  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
44  * 10-Jan-2003 : Changed base class and method names (DG);
45  * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
46  * Serializable (DG);
47  * 21-Oct-2003 : Added hashCode() method (DG);
48  *
49  */

50
51 package org.jfree.data.time;
52
53 import java.io.Serializable JavaDoc;
54 import java.util.Calendar JavaDoc;
55 import java.util.Date JavaDoc;
56 import java.util.TimeZone JavaDoc;
57
58 /**
59  * Represents a millisecond. This class is immutable, which is a requirement
60  * for all {@link RegularTimePeriod} subclasses.
61  */

62 public class Millisecond extends RegularTimePeriod implements Serializable JavaDoc {
63
64     /** For serialization. */
65     static final long serialVersionUID = -5316836467277638485L;
66     
67     /** A constant for the first millisecond in a second. */
68     public static final int FIRST_MILLISECOND_IN_SECOND = 0;
69
70     /** A constant for the last millisecond in a second. */
71     public static final int LAST_MILLISECOND_IN_SECOND = 999;
72
73     /** The millisecond. */
74     private int millisecond;
75
76     /** The second. */
77     private Second second;
78
79     /**
80      * Constructs a millisecond based on the current system time.
81      */

82     public Millisecond() {
83         this(new Date JavaDoc());
84     }
85
86     /**
87      * Constructs a millisecond.
88      *
89      * @param millisecond the millisecond (0-999).
90      * @param second the second.
91      */

92     public Millisecond(int millisecond, Second second) {
93         this.millisecond = millisecond;
94         this.second = second;
95     }
96
97     /**
98      * Creates a new millisecond.
99      *
100      * @param millisecond the millisecond (0-999).
101      * @param second the second (0-59).
102      * @param minute the minute (0-59).
103      * @param hour the hour (0-23).
104      * @param day the day (1-31).
105      * @param month the month (1-12).
106      * @param year the year (1900-9999).
107      */

108     public Millisecond(int millisecond, int second, int minute, int hour,
109                        int day, int month, int year) {
110                            
111         this(millisecond, new Second(second, minute, hour, day, month, year));
112     
113     }
114
115     /**
116      * Constructs a millisecond.
117      *
118      * @param time the time.
119      */

120     public Millisecond(Date JavaDoc time) {
121         this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
122     }
123
124     /**
125      * Creates a millisecond.
126      *
127      * @param time the instant in time.
128      * @param zone the time zone.
129      */

130     public Millisecond(Date JavaDoc time, TimeZone JavaDoc zone) {
131
132         this.second = new Second(time, zone);
133         Calendar JavaDoc calendar = Calendar.getInstance(zone);
134         calendar.setTime(time);
135         this.millisecond = calendar.get(Calendar.MILLISECOND);
136
137     }
138
139     /**
140      * Returns the second.
141      *
142      * @return The second.
143      */

144     public Second getSecond() {
145         return this.second;
146     }
147
148     /**
149      * Returns the millisecond.
150      *
151      * @return The millisecond.
152      */

153     public long getMillisecond() {
154         return this.millisecond;
155     }
156
157     /**
158      * Returns the millisecond preceding this one.
159      *
160      * @return The millisecond preceding this one.
161      */

162     public RegularTimePeriod previous() {
163
164         RegularTimePeriod result = null;
165
166         if (this.millisecond != FIRST_MILLISECOND_IN_SECOND) {
167             result = new Millisecond(this.millisecond - 1, this.second);
168         }
169         else {
170             Second previous = (Second) this.second.previous();
171             if (previous != null) {
172                 result = new Millisecond(LAST_MILLISECOND_IN_SECOND, previous);
173             }
174         }
175         return result;
176
177     }
178
179     /**
180      * Returns the millisecond following this one.
181      *
182      * @return The millisecond following this one.
183      */

184     public RegularTimePeriod next() {
185
186         RegularTimePeriod result = null;
187         if (this.millisecond != LAST_MILLISECOND_IN_SECOND) {
188             result = new Millisecond(this.millisecond + 1, this.second);
189         }
190         else {
191             Second next = (Second) this.second.next();
192             if (next != null) {
193                 result = new Millisecond(FIRST_MILLISECOND_IN_SECOND, next);
194             }
195         }
196         return result;
197
198     }
199
200     /**
201      * Returns a serial index number for the millisecond.
202      *
203      * @return The serial index number.
204      */

205     public long getSerialIndex() {
206         return this.second.getSerialIndex() * 1000L + this.millisecond;
207     }
208
209     /**
210      * Tests the equality of this object against an arbitrary Object.
211      * <P>
212      * This method will return true ONLY if the object is a Millisecond object
213      * representing the same millisecond as this instance.
214      *
215      * @param obj the object to compare
216      *
217      * @return <code>true</code> if milliseconds and seconds of this and object
218      * are the same.
219      */

220     public boolean equals(Object JavaDoc obj) {
221
222         if (obj instanceof Millisecond) {
223             Millisecond m = (Millisecond) obj;
224             return ((this.millisecond == m.getMillisecond())
225                     && (this.second.equals(m.getSecond())));
226         }
227         else {
228             return false;
229         }
230
231     }
232
233     /**
234      * Returns a hash code for this object instance. The approach described by
235      * Joshua Bloch in "Effective Java" has been used here:
236      * <p>
237      * <code>http://developer.java.sun.com/developer/Books/effectivejava
238      * /Chapter3.pdf</code>
239      *
240      * @return A hashcode.
241      */

242     public int hashCode() {
243         int result = 17;
244         result = 37 * result + this.millisecond;
245         result = 37 * result + this.second.hashCode();
246         return result;
247     }
248
249     /**
250      * Returns an integer indicating the order of this Millisecond object
251      * relative to the specified object:
252      *
253      * negative == before, zero == same, positive == after.
254      *
255      * @param obj the object to compare
256      *
257      * @return negative == before, zero == same, positive == after.
258      */

259     public int compareTo(Object JavaDoc obj) {
260
261         int result;
262         long difference;
263
264         // CASE 1 : Comparing to another Second object
265
// -------------------------------------------
266
if (obj instanceof Millisecond) {
267             Millisecond ms = (Millisecond) obj;
268             difference = getFirstMillisecond() - ms.getFirstMillisecond();
269             if (difference > 0) {
270                 result = 1;
271             }
272             else {
273                 if (difference < 0) {
274                     result = -1;
275                 }
276                 else {
277                     result = 0;
278                 }
279             }
280         }
281
282         // CASE 2 : Comparing to another TimePeriod object
283
// -----------------------------------------------
284
else if (obj instanceof RegularTimePeriod) {
285             // more difficult case - evaluate later...
286
result = 0;
287         }
288
289         // CASE 3 : Comparing to a non-TimePeriod object
290
// ---------------------------------------------
291
else {
292             // consider time periods to be ordered after general objects
293
result = 1;
294         }
295
296         return result;
297
298     }
299
300     /**
301      * Returns the first millisecond of the time period.
302      *
303      * @return The first millisecond of the time period.
304      */

305     public long getFirstMillisecond() {
306         return this.second.getFirstMillisecond() + this.millisecond;
307     }
308
309     /**
310      * Returns the first millisecond of the time period.
311      *
312      * @param calendar the calendar.
313      *
314      * @return The first millisecond of the time period.
315      */

316     public long getFirstMillisecond(Calendar JavaDoc calendar) {
317         return this.second.getFirstMillisecond(calendar) + this.millisecond;
318     }
319
320     /**
321      * Returns the last millisecond of the time period.
322      *
323      * @return The last millisecond of the time period.
324      */

325     public long getLastMillisecond() {
326         return this.second.getFirstMillisecond() + this.millisecond;
327     }
328
329     /**
330      * Returns the last millisecond of the time period.
331      *
332      * @param calendar the calendar.
333      *
334      * @return The last millisecond of the time period.
335      */

336     public long getLastMillisecond(Calendar JavaDoc calendar) {
337         return this.second.getFirstMillisecond(calendar) + this.millisecond;
338     }
339
340 }
341
Popular Tags