001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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-2007 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 *
047 */
048
049package org.jfree.data.time;
050
051import java.io.Serializable;
052import java.util.Calendar;
053import java.util.Date;
054
055/**
056 * Wrapper for a <code>java.util.Date</code> object that allows it to be used 
057 * as a {@link RegularTimePeriod}.  This class is immutable, which is a 
058 * requirement for all {@link RegularTimePeriod} subclasses.
059 */
060public class FixedMillisecond extends RegularTimePeriod 
061                              implements Serializable {
062
063    /** For serialization. */
064    private static final long serialVersionUID = 7867521484545646931L;
065    
066    /** The millisecond. */
067    private Date time;
068
069    /**
070     * Constructs a millisecond based on the current system time.
071     */
072    public FixedMillisecond() {
073        this(new Date());
074    }
075
076    /**
077     * Constructs a millisecond.
078     *
079     * @param millisecond  the millisecond (same encoding as java.util.Date).
080     */
081    public FixedMillisecond(long millisecond) {
082        this(new Date(millisecond));
083    }
084
085    /**
086     * Constructs a millisecond.
087     *
088     * @param time  the time.
089     */
090    public FixedMillisecond(Date time) {
091        this.time = time;
092    }
093
094    /**
095     * Returns the date/time.
096     *
097     * @return The date/time.
098     */
099    public Date getTime() {
100        return this.time;
101    }
102
103    /**
104     * This method is overridden to do nothing.
105     * 
106     * @param calendar  ignored
107     * 
108     * @since 1.0.3
109     */
110    public void peg(Calendar calendar) {
111        // nothing to do        
112    }
113
114    /**
115     * Returns the millisecond preceding this one.
116     *
117     * @return The millisecond preceding this one.
118     */
119    public RegularTimePeriod previous() {
120        RegularTimePeriod result = null;
121        long t = this.time.getTime();
122        if (t != Long.MIN_VALUE) {
123            result = new FixedMillisecond(t - 1);
124        }
125        return result;
126    }
127
128    /**
129     * Returns the millisecond following this one.
130     *
131     * @return The millisecond following this one.
132     */
133    public RegularTimePeriod next() {
134        RegularTimePeriod result = null;
135        long t = this.time.getTime();
136        if (t != Long.MAX_VALUE) {
137            result = new FixedMillisecond(t + 1);
138        }
139        return result;
140    }
141
142    /**
143     * Tests the equality of this object against an arbitrary Object.
144     *
145     * @param object  the object to compare
146     *
147     * @return A boolean.
148     */
149    public boolean equals(Object object) {
150        if (object instanceof FixedMillisecond) {
151            FixedMillisecond m = (FixedMillisecond) object;
152            return this.time.equals(m.getTime());
153        }
154        else {
155            return false;
156        }
157
158    }
159
160    /**
161     * Returns a hash code for this object instance.
162     * 
163     * @return A hash code.
164     */
165    public int hashCode() {
166        return this.time.hashCode();
167    }
168
169    /**
170     * Returns an integer indicating the order of this Millisecond object
171     * relative to the specified
172     * object: negative == before, zero == same, positive == after.
173     *
174     * @param o1    the object to compare.
175     *
176     * @return negative == before, zero == same, positive == after.
177     */
178    public int compareTo(Object o1) {
179
180        int result;
181        long difference;
182
183        // CASE 1 : Comparing to another Second object
184        // -------------------------------------------
185        if (o1 instanceof FixedMillisecond) {
186            FixedMillisecond t1 = (FixedMillisecond) o1;
187            difference = this.time.getTime() - t1.time.getTime();
188            if (difference > 0) {
189                result = 1;
190            }
191            else {
192                if (difference < 0) {
193                   result = -1;
194                }
195                else {
196                    result = 0;
197                }
198            }
199        }
200
201        // CASE 2 : Comparing to another TimePeriod object
202        // -----------------------------------------------
203        else if (o1 instanceof RegularTimePeriod) {
204            // more difficult case - evaluate later...
205            result = 0;
206        }
207
208        // CASE 3 : Comparing to a non-TimePeriod object
209        // ---------------------------------------------
210        else {
211            // consider time periods to be ordered after general objects
212            result = 1;
213        }
214
215        return result;
216
217    }
218
219    /**
220     * Returns the first millisecond of the time period.
221     *
222     * @return The first millisecond of the time period.
223     */
224    public long getFirstMillisecond() {
225        return this.time.getTime();
226    }
227
228
229    /**
230     * Returns the first millisecond of the time period.
231     *
232     * @param calendar  the calendar.
233     *
234     * @return The first millisecond of the time period.
235     */
236    public long getFirstMillisecond(Calendar calendar) {
237        return this.time.getTime();
238    }
239
240    /**
241     * Returns the last millisecond of the time period.
242     *
243     * @return The last millisecond of the time period.
244     */
245    public long getLastMillisecond() {
246        return this.time.getTime();
247    }
248
249    /**
250     * Returns the last millisecond of the time period.
251     *
252     * @param calendar  the calendar.
253     *
254     * @return The last millisecond of the time period.
255     */
256    public long getLastMillisecond(Calendar calendar) {
257        return this.time.getTime();
258    }
259
260    /**
261     * Returns the millisecond closest to the middle of the time period.
262     *
263     * @return The millisecond closest to the middle of the time period.
264     */
265    public long getMiddleMillisecond() {
266        return this.time.getTime();
267    }
268
269    /**
270     * Returns the millisecond closest to the middle of the time period.
271     *
272     * @param calendar  the calendar.
273     *
274     * @return The millisecond closest to the middle of the time period.
275     */
276    public long getMiddleMillisecond(Calendar calendar) {
277        return this.time.getTime();
278    }
279
280    /**
281     * Returns a serial index number for the millisecond.
282     *
283     * @return The serial index number.
284     */
285    public long getSerialIndex() {
286        return this.time.getTime();
287    }
288
289}