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