KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FixedMillisecond.java
28  * ---------------------
29  * (C) Copyright 2002-2005 by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: FixedMillisecond.java,v 1.4 2005/05/19 10:35:27 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
39  * 24-Jun-2002 : Removed unnecessary imports (DG);
40  * 10-Sep-2002 : Added getSerialIndex() method (DG);
41  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
42  * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
43  * Serializable (DG);
44  * 21-Oct-2003 : Added hashCode() method (DG);
45  *
46  */

47
48 package org.jfree.data.time;
49
50 import java.io.Serializable JavaDoc;
51 import java.util.Calendar JavaDoc;
52 import java.util.Date JavaDoc;
53
54 /**
55  * Wrapper for a <code>java.util.Date</code> object that allows it to be used
56  * as a {@link RegularTimePeriod}. This class is immutable, which is a
57  * requirement for all {@link RegularTimePeriod} subclasses.
58  */

59 public class FixedMillisecond extends RegularTimePeriod
60                               implements Serializable JavaDoc {
61
62     /** For serialization. */
63     private static final long serialVersionUID = 7867521484545646931L;
64     
65     /** The millisecond. */
66     private Date JavaDoc time;
67
68     /**
69      * Constructs a millisecond based on the current system time.
70      */

71     public FixedMillisecond() {
72         this(new Date JavaDoc());
73     }
74
75     /**
76      * Constructs a millisecond.
77      *
78      * @param millisecond the millisecond (same encoding as java.util.Date).
79      */

80     public FixedMillisecond(long millisecond) {
81         this(new Date JavaDoc(millisecond));
82     }
83
84     /**
85      * Constructs a millisecond.
86      *
87      * @param time the time.
88      */

89     public FixedMillisecond(Date JavaDoc time) {
90         this.time = time;
91     }
92
93     /**
94      * Returns the date/time.
95      *
96      * @return The date/time.
97      */

98     public Date JavaDoc getTime() {
99         return this.time;
100     }
101
102     /**
103      * Returns the millisecond preceding this one.
104      *
105      * @return The millisecond preceding this one.
106      */

107     public RegularTimePeriod previous() {
108         RegularTimePeriod result = null;
109         long t = this.time.getTime();
110         if (t != Long.MIN_VALUE) {
111             result = new FixedMillisecond(t - 1);
112         }
113         return result;
114     }
115
116     /**
117      * Returns the millisecond following this one.
118      *
119      * @return The millisecond following this one.
120      */

121     public RegularTimePeriod next() {
122         RegularTimePeriod result = null;
123         long t = this.time.getTime();
124         if (t != Long.MAX_VALUE) {
125             result = new FixedMillisecond(t + 1);
126         }
127         return result;
128     }
129
130     /**
131      * Tests the equality of this object against an arbitrary Object.
132      *
133      * @param object the object to compare
134      *
135      * @return A boolean.
136      */

137     public boolean equals(Object JavaDoc object) {
138         if (object instanceof FixedMillisecond) {
139             FixedMillisecond m = (FixedMillisecond) object;
140             return this.time.equals(m.getTime());
141         }
142         else {
143             return false;
144         }
145
146     }
147
148     /**
149      * Returns a hash code for this object instance.
150      *
151      * @return A hash code.
152      */

153     public int hashCode() {
154         return this.time.hashCode();
155     }
156
157     /**
158      * Returns an integer indicating the order of this Millisecond object
159      * relative to the specified
160      * object: negative == before, zero == same, positive == after.
161      *
162      * @param o1 the object to compare.
163      *
164      * @return negative == before, zero == same, positive == after.
165      */

166     public int compareTo(Object JavaDoc o1) {
167
168         int result;
169         long difference;
170
171         // CASE 1 : Comparing to another Second object
172
// -------------------------------------------
173
if (o1 instanceof FixedMillisecond) {
174             FixedMillisecond t1 = (FixedMillisecond) o1;
175             difference = this.time.getTime() - t1.time.getTime();
176             if (difference > 0) {
177                 result = 1;
178             }
179             else {
180                 if (difference < 0) {
181                    result = -1;
182                 }
183                 else {
184                     result = 0;
185                 }
186             }
187         }
188
189         // CASE 2 : Comparing to another TimePeriod object
190
// -----------------------------------------------
191
else if (o1 instanceof RegularTimePeriod) {
192             // more difficult case - evaluate later...
193
result = 0;
194         }
195
196         // CASE 3 : Comparing to a non-TimePeriod object
197
// ---------------------------------------------
198
else {
199             // consider time periods to be ordered after general objects
200
result = 1;
201         }
202
203         return result;
204
205     }
206
207     /**
208      * Returns the first millisecond of the time period.
209      *
210      * @return The first millisecond of the time period.
211      */

212     public long getFirstMillisecond() {
213         return this.time.getTime();
214     }
215
216
217     /**
218      * Returns the first millisecond of the time period.
219      *
220      * @param calendar the calendar.
221      *
222      * @return The first millisecond of the time period.
223      */

224     public long getFirstMillisecond(Calendar JavaDoc calendar) {
225         return this.time.getTime();
226     }
227
228     /**
229      * Returns the last millisecond of the time period.
230      *
231      * @return The last millisecond of the time period.
232      */

233     public long getLastMillisecond() {
234         return this.time.getTime();
235     }
236
237     /**
238      * Returns the last millisecond of the time period.
239      *
240      * @param calendar the calendar.
241      *
242      * @return The last millisecond of the time period.
243      */

244     public long getLastMillisecond(Calendar JavaDoc calendar) {
245         return this.time.getTime();
246     }
247
248     /**
249      * Returns the millisecond closest to the middle of the time period.
250      *
251      * @return The millisecond closest to the middle of the time period.
252      */

253     public long getMiddleMillisecond() {
254         return this.time.getTime();
255     }
256
257     /**
258      * Returns the millisecond closest to the middle of the time period.
259      *
260      * @param calendar the calendar.
261      *
262      * @return The millisecond closest to the middle of the time period.
263      */

264     public long getMiddleMillisecond(Calendar JavaDoc calendar) {
265         return this.time.getTime();
266     }
267
268     /**
269      * Returns a serial index number for the millisecond.
270      *
271      * @return The serial index number.
272      */

273     public long getSerialIndex() {
274         return this.time.getTime();
275     }
276
277 }
278
Popular Tags