001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------------
028 * FixedMillisecond.java
029 * ---------------------
030 * (C) Copyright 2002-2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 19-Mar-2002 : Version 1, based on original Millisecond implementation (DG);
038 * 24-Jun-2002 : Removed unnecessary imports (DG);
039 * 10-Sep-2002 : Added getSerialIndex() method (DG);
040 * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041 * 13-Mar-2003 : Moved to com.jrefinery.data.time package and implemented
042 * Serializable (DG);
043 * 21-Oct-2003 : Added hashCode() method (DG);
044 * ------------- JFREECHART 1.0.x ---------------------------------------------
045 * 06-Oct-2006 : Added peg() method (DG);
046 * 28-May-2008 : Fixed immutability problem (DG);
047 *
048 */
049
050 package org.jfree.data.time;
051
052 import java.io.Serializable;
053 import java.util.Calendar;
054 import java.util.Date;
055
056 /**
057 * Wrapper for a <code>java.util.Date</code> object that allows it to be used
058 * as a {@link RegularTimePeriod}. This class is immutable, which is a
059 * requirement for all {@link RegularTimePeriod} subclasses.
060 */
061 public class FixedMillisecond extends RegularTimePeriod
062 implements Serializable {
063
064 /** For serialization. */
065 private static final long serialVersionUID = 7867521484545646931L;
066
067 /** The millisecond. */
068 private long time;
069
070 /**
071 * Constructs a millisecond based on the current system time.
072 */
073 public FixedMillisecond() {
074 this(new Date());
075 }
076
077 /**
078 * Constructs a millisecond.
079 *
080 * @param millisecond the millisecond (same encoding as java.util.Date).
081 */
082 public FixedMillisecond(long millisecond) {
083 this(new Date(millisecond));
084 }
085
086 /**
087 * Constructs a millisecond.
088 *
089 * @param time the time.
090 */
091 public FixedMillisecond(Date time) {
092 this.time = time.getTime();
093 }
094
095 /**
096 * Returns the date/time.
097 *
098 * @return The date/time.
099 */
100 public Date getTime() {
101 return new Date(this.time);
102 }
103
104 /**
105 * This method is overridden to do nothing.
106 *
107 * @param calendar ignored
108 *
109 * @since 1.0.3
110 */
111 public void peg(Calendar calendar) {
112 // nothing to do
113 }
114
115 /**
116 * Returns the millisecond preceding this one.
117 *
118 * @return The millisecond preceding this one.
119 */
120 public RegularTimePeriod previous() {
121 RegularTimePeriod result = null;
122 long t = this.time;
123 if (t != Long.MIN_VALUE) {
124 result = new FixedMillisecond(t - 1);
125 }
126 return result;
127 }
128
129 /**
130 * Returns the millisecond following this one.
131 *
132 * @return The millisecond following this one.
133 */
134 public RegularTimePeriod next() {
135 RegularTimePeriod result = null;
136 long t = this.time;
137 if (t != Long.MAX_VALUE) {
138 result = new FixedMillisecond(t + 1);
139 }
140 return result;
141 }
142
143 /**
144 * Tests the equality of this object against an arbitrary Object.
145 *
146 * @param object the object to compare
147 *
148 * @return A boolean.
149 */
150 public boolean equals(Object object) {
151 if (object instanceof FixedMillisecond) {
152 FixedMillisecond m = (FixedMillisecond) object;
153 return this.time == m.getFirstMillisecond();
154 }
155 else {
156 return false;
157 }
158
159 }
160
161 /**
162 * Returns a hash code for this object instance.
163 *
164 * @return A hash code.
165 */
166 public int hashCode() {
167 return (int) this.time;
168 }
169
170 /**
171 * Returns an integer indicating the order of this Millisecond object
172 * relative to the specified
173 * object: negative == before, zero == same, positive == after.
174 *
175 * @param o1 the object to compare.
176 *
177 * @return negative == before, zero == same, positive == after.
178 */
179 public int compareTo(Object o1) {
180
181 int result;
182 long difference;
183
184 // CASE 1 : Comparing to another Second object
185 // -------------------------------------------
186 if (o1 instanceof FixedMillisecond) {
187 FixedMillisecond t1 = (FixedMillisecond) o1;
188 difference = this.time - t1.time;
189 if (difference > 0) {
190 result = 1;
191 }
192 else {
193 if (difference < 0) {
194 result = -1;
195 }
196 else {
197 result = 0;
198 }
199 }
200 }
201
202 // CASE 2 : Comparing to another TimePeriod object
203 // -----------------------------------------------
204 else if (o1 instanceof RegularTimePeriod) {
205 // more difficult case - evaluate later...
206 result = 0;
207 }
208
209 // CASE 3 : Comparing to a non-TimePeriod object
210 // ---------------------------------------------
211 else {
212 // consider time periods to be ordered after general objects
213 result = 1;
214 }
215
216 return result;
217
218 }
219
220 /**
221 * Returns the first millisecond of the time period.
222 *
223 * @return The first millisecond of the time period.
224 */
225 public long getFirstMillisecond() {
226 return this.time;
227 }
228
229
230 /**
231 * Returns the first millisecond of the time period.
232 *
233 * @param calendar the calendar.
234 *
235 * @return The first millisecond of the time period.
236 */
237 public long getFirstMillisecond(Calendar calendar) {
238 return this.time;
239 }
240
241 /**
242 * Returns the last millisecond of the time period.
243 *
244 * @return The last millisecond of the time period.
245 */
246 public long getLastMillisecond() {
247 return this.time;
248 }
249
250 /**
251 * Returns the last millisecond of the time period.
252 *
253 * @param calendar the calendar.
254 *
255 * @return The last millisecond of the time period.
256 */
257 public long getLastMillisecond(Calendar calendar) {
258 return this.time;
259 }
260
261 /**
262 * Returns the millisecond closest to the middle of the time period.
263 *
264 * @return The millisecond closest to the middle of the time period.
265 */
266 public long getMiddleMillisecond() {
267 return this.time;
268 }
269
270 /**
271 * Returns the millisecond closest to the middle of the time period.
272 *
273 * @param calendar the calendar.
274 *
275 * @return The millisecond closest to the middle of the time period.
276 */
277 public long getMiddleMillisecond(Calendar calendar) {
278 return this.time;
279 }
280
281 /**
282 * Returns a serial index number for the millisecond.
283 *
284 * @return The serial index number.
285 */
286 public long getSerialIndex() {
287 return this.time;
288 }
289
290 }