KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Second.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: Second.java,v 1.6 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 11-Oct-2001 : Version 1 (DG);
39  * 18-Dec-2001 : Changed order of parameters in constructor (DG);
40  * 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG);
41  * 14-Feb-2002 : Fixed bug in Second(Date) constructor, and changed start of
42  * range to zero from one (DG);
43  * 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to
44  * evaluate with reference to a particular time zone (DG);
45  * 13-Mar-2002 : Added parseSecond() method (DG);
46  * 10-Sep-2002 : Added getSerialIndex() method (DG);
47  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
48  * 10-Jan-2003 : Changed base class and method names (DG);
49  * 05-Mar-2003 : Fixed bug in getLastMillisecond() picked up in JUnit
50  * tests (DG);
51  * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
52  * Serializable (DG);
53  * 21-Oct-2003 : Added hashCode() method (DG);
54  *
55  */

56
57 package org.jfree.data.time;
58
59 import java.io.Serializable JavaDoc;
60 import java.util.Calendar JavaDoc;
61 import java.util.Date JavaDoc;
62 import java.util.TimeZone JavaDoc;
63
64 /**
65  * Represents a second in a particular day. This class is immutable, which is
66  * a requirement for all {@link RegularTimePeriod} subclasses.
67  */

68 public class Second extends RegularTimePeriod implements Serializable JavaDoc {
69
70     /** For serialization. */
71     private static final long serialVersionUID = -6536564190712383466L;
72     
73     /** Useful constant for the first second in a minute. */
74     public static final int FIRST_SECOND_IN_MINUTE = 0;
75
76     /** Useful constant for the last second in a minute. */
77     public static final int LAST_SECOND_IN_MINUTE = 59;
78
79     /** The minute. */
80     private Minute minute;
81
82     /** The second. */
83     private int second;
84
85     /**
86      * Constructs a new Second, based on the system date/time.
87      */

88     public Second() {
89         this(new Date JavaDoc());
90     }
91
92     /**
93      * Constructs a new Second.
94      *
95      * @param second the second (0 to 24*60*60-1).
96      * @param minute the minute (<code>null</code> not permitted).
97      */

98     public Second(int second, Minute minute) {
99         if (minute == null) {
100             throw new IllegalArgumentException JavaDoc("Null 'minute' argument.");
101         }
102         this.minute = minute;
103         this.second = second;
104     }
105
106     /**
107      * Creates a new second.
108      *
109      * @param second the second (0-59).
110      * @param minute the minute (0-59).
111      * @param hour the hour (0-23).
112      * @param day the day (1-31).
113      * @param month the month (1-12).
114      * @param year the year (1900-9999).
115      */

116     public Second(int second, int minute, int hour,
117                   int day, int month, int year) {
118         this(second, new Minute(minute, hour, day, month, year));
119     }
120     
121     /**
122      * Constructs a second.
123      *
124      * @param time the time.
125      */

126     public Second(Date JavaDoc time) {
127         this(time, RegularTimePeriod.DEFAULT_TIME_ZONE);
128     }
129
130     /**
131      * Creates a new second based on the supplied time and time zone.
132      *
133      * @param time the instant in time.
134      * @param zone the time zone.
135      */

136     public Second(Date JavaDoc time, final TimeZone JavaDoc zone) {
137         this.minute = new Minute(time, zone);
138         Calendar JavaDoc calendar = Calendar.getInstance(zone);
139         calendar.setTime(time);
140         this.second = calendar.get(Calendar.SECOND);
141     }
142
143     /**
144      * Returns the second within the minute.
145      *
146      * @return The second (0 - 59).
147      */

148     public int getSecond() {
149         return this.second;
150     }
151
152     /**
153      * Returns the minute.
154      *
155      * @return The minute (never <code>null</code>).
156      */

157     public Minute getMinute() {
158         return this.minute;
159     }
160
161     /**
162      * Returns the second preceding this one.
163      *
164      * @return The second preceding this one.
165      */

166     public RegularTimePeriod previous() {
167         
168         Second result = null;
169         if (this.second != FIRST_SECOND_IN_MINUTE) {
170             result = new Second(this.second - 1, this.minute);
171         }
172         else {
173             Minute previous = (Minute) this.minute.previous();
174             if (previous != null) {
175                 result = new Second(LAST_SECOND_IN_MINUTE, previous);
176             }
177         }
178         return result;
179         
180     }
181
182     /**
183      * Returns the second following this one.
184      *
185      * @return The second following this one.
186      */

187     public RegularTimePeriod next() {
188         
189         Second result = null;
190         if (this.second != LAST_SECOND_IN_MINUTE) {
191             result = new Second(this.second + 1, this.minute);
192         }
193         else {
194             Minute next = (Minute) this.minute.next();
195             if (next != null) {
196                 result = new Second(FIRST_SECOND_IN_MINUTE, next);
197             }
198         }
199         return result;
200
201     }
202
203     /**
204      * Returns a serial index number for the minute.
205      *
206      * @return The serial index number.
207      */

208     public long getSerialIndex() {
209         return this.minute.getSerialIndex() * 60L + this.second;
210     }
211
212     /**
213      * Returns the first millisecond of the minute.
214      *
215      * @param calendar the calendar/timezone.
216      *
217      * @return The first millisecond.
218      */

219     public long getFirstMillisecond(Calendar JavaDoc calendar) {
220         return this.minute.getFirstMillisecond(calendar) + this.second * 1000L;
221     }
222
223     /**
224      * Returns the last millisecond of the second.
225      *
226      * @param calendar the calendar/timezone.
227      *
228      * @return The last millisecond.
229      */

230     public long getLastMillisecond(Calendar JavaDoc calendar) {
231         return this.minute.getFirstMillisecond(calendar)
232             + this.second * 1000L + 999L;
233     }
234
235     /**
236      * Tests the equality of this object against an arbitrary Object.
237      * <P>
238      * This method will return true ONLY if the object is a Second object
239      * representing the same second as this instance.
240      *
241      * @param obj the object to compare.
242      *
243      * @return <code>true</code> if second and minute of this and the object
244      * are the same.
245      */

246     public boolean equals(Object JavaDoc obj) {
247         if (obj instanceof Second) {
248             Second s = (Second) obj;
249             return ((this.second == s.getSecond())
250                     && (this.minute.equals(s.getMinute())));
251         }
252         else {
253             return false;
254         }
255     }
256
257     /**
258      * Returns a hash code for this object instance. The approach described by
259      * Joshua Bloch in "Effective Java" has been used here:
260      * <p>
261      * <code>http://developer.java.sun.com/developer/Books/effectivejava
262      * /Chapter3.pdf</code>
263      *
264      * @return A hash code.
265      */

266     public int hashCode() {
267         int result = 17;
268         result = 37 * result + this.second;
269         result = 37 * result + this.minute.hashCode();
270         return result;
271     }
272
273     /**
274      * Returns an integer indicating the order of this Second object relative
275      * to the specified
276      * object: negative == before, zero == same, positive == after.
277      *
278      * @param o1 the object to compare.
279      *
280      * @return negative == before, zero == same, positive == after.
281      */

282     public int compareTo(Object JavaDoc o1) {
283
284         int result;
285
286         // CASE 1 : Comparing to another Second object
287
// -------------------------------------------
288
if (o1 instanceof Second) {
289             Second s = (Second) o1;
290             result = this.minute.compareTo(s.minute);
291             if (result == 0) {
292                 result = this.second - s.second;
293             }
294         }
295
296         // CASE 2 : Comparing to another TimePeriod object
297
// -----------------------------------------------
298
else if (o1 instanceof RegularTimePeriod) {
299             // more difficult case - evaluate later...
300
result = 0;
301         }
302
303         // CASE 3 : Comparing to a non-TimePeriod object
304
// ---------------------------------------------
305
else {
306             // consider time periods to be ordered after general objects
307
result = 1;
308         }
309
310         return result;
311
312     }
313
314     /**
315      * Creates a new instance by parsing a string. The string is assumed to
316      * be in the format "YYYY-MM-DD HH:MM:SS", perhaps with leading or trailing
317      * whitespace.
318      *
319      * @param s the string to parse.
320      *
321      * @return The second, or <code>null</code> if the string is not parseable.
322      */

323     public static Second parseSecond(String JavaDoc s) {
324
325         Second result = null;
326         s = s.trim();
327
328         String JavaDoc daystr = s.substring(0, Math.min(10, s.length()));
329         Day day = Day.parseDay(daystr);
330         if (day != null) {
331             String JavaDoc hmsstr = s.substring(
332                 Math.min(daystr.length() + 1, s.length()), s.length()
333             );
334             hmsstr = hmsstr.trim();
335
336             int l = hmsstr.length();
337             String JavaDoc hourstr = hmsstr.substring(0, Math.min(2, l));
338             String JavaDoc minstr = hmsstr.substring(Math.min(3, l), Math.min(5, l));
339             String JavaDoc secstr = hmsstr.substring(Math.min(6, l), Math.min(8, l));
340             int hour = Integer.parseInt(hourstr);
341
342             if ((hour >= 0) && (hour <= 23)) {
343
344                 int minute = Integer.parseInt(minstr);
345                 if ((minute >= 0) && (minute <= 59)) {
346
347                     Minute m = new Minute(minute, new Hour(hour, day));
348                     int second = Integer.parseInt(secstr);
349                     if ((second >= 0) && (second <= 59)) {
350                         result = new Second(second, m);
351                     }
352                 }
353             }
354         }
355
356         return result;
357
358     }
359
360 }
361
Popular Tags