001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [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 * SimpleTimePeriod.java
029 * ---------------------
030 * (C) Copyright 2002-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 07-Oct-2002 : Added Javadocs (DG);
038 * 10-Jan-2003 : Renamed TimeAllocation --> SimpleTimePeriod (DG);
039 * 13-Mar-2003 : Added equals() method, and Serializable interface (DG);
040 * 21-Oct-2003 : Added hashCode() method (DG);
041 * 27-Jan-2005 : Implemented Comparable, to enable this class to be used
042 *               in the TimeTableXYDataset class (DG);
043 * 02-Jun-2008 : Fixed problem with fields being mutable (DG);
044 *
045 */
046
047package org.jfree.data.time;
048
049import java.io.Serializable;
050import java.util.Date;
051
052/**
053 * An arbitrary period of time, measured to millisecond precision using
054 * <code>java.util.Date</code>.
055 * <p>
056 * This class is intentionally immutable (that is, once constructed, you cannot
057 * alter the start and end attributes).
058 */
059public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable {
060
061    /** For serialization. */
062    private static final long serialVersionUID = 8684672361131829554L;
063
064    /** The start date/time. */
065    private long start;
066
067    /** The end date/time. */
068    private long end;
069
070    /**
071     * Creates a new time allocation.
072     *
073     * @param start  the start date/time in milliseconds.
074     * @param end  the end date/time in milliseconds.
075     */
076    public SimpleTimePeriod(long start, long end) {
077        if (start > end) {
078            throw new IllegalArgumentException("Requires start <= end.");
079        }
080        this.start = start;
081        this.end = end;
082    }
083
084    /**
085     * Creates a new time allocation.
086     *
087     * @param start  the start date/time (<code>null</code> not permitted).
088     * @param end  the end date/time (<code>null</code> not permitted).
089     */
090    public SimpleTimePeriod(Date start, Date end) {
091        this(start.getTime(), end.getTime());
092    }
093
094    /**
095     * Returns the start date/time.
096     *
097     * @return The start date/time (never <code>null</code>).
098     */
099    @Override
100    public Date getStart() {
101        return new Date(this.start);
102    }
103
104    /**
105     * Returns the start date/time in milliseconds.
106     *
107     * @return The start.
108     *
109     * @since 1.0.10.
110     */
111    public long getStartMillis() {
112        return this.start;
113    }
114
115    /**
116     * Returns the end date/time.
117     *
118     * @return The end date/time (never <code>null</code>).
119     */
120    @Override
121    public Date getEnd() {
122        return new Date(this.end);
123    }
124
125    /**
126     * Returns the end date/time in milliseconds.
127     *
128     * @return The end.
129     *
130     * @since 1.0.10.
131     */
132    public long getEndMillis() {
133        return this.end;
134    }
135
136    /**
137     * Tests this time period instance for equality with an arbitrary object.
138     * The object is considered equal if it is an instance of {@link TimePeriod}
139     * and it has the same start and end dates.
140     *
141     * @param obj  the other object (<code>null</code> permitted).
142     *
143     * @return A boolean.
144     */
145    @Override
146    public boolean equals(Object obj) {
147        if (obj == this) {
148            return true;
149        }
150        if (!(obj instanceof TimePeriod)) {
151            return false;
152        }
153        TimePeriod that = (TimePeriod) obj;
154        if (!this.getStart().equals(that.getStart())) {
155            return false;
156        }
157        if (!this.getEnd().equals(that.getEnd())) {
158            return false;
159        }
160        return true;
161    }
162
163    /**
164     * Returns an integer that indicates the relative ordering of two
165     * time periods.
166     *
167     * @param obj  the object (<code>null</code> not permitted).
168     *
169     * @return An integer.
170     *
171     * @throws ClassCastException if <code>obj</code> is not an instance of
172     *                            {@link TimePeriod}.
173     */
174    @Override
175    public int compareTo(Object obj) {
176        TimePeriod that = (TimePeriod) obj;
177        long t0 = getStart().getTime();
178        long t1 = getEnd().getTime();
179        long m0 = t0 + (t1 - t0) / 2L;
180        long t2 = that.getStart().getTime();
181        long t3 = that.getEnd().getTime();
182        long m1 = t2 + (t3 - t2) / 2L;
183        if (m0 < m1) {
184            return -1;
185        }
186        else if (m0 > m1) {
187            return 1;
188        }
189        else {
190            if (t0 < t2) {
191                return -1;
192            }
193            else if (t0 > t2) {
194                return 1;
195            }
196            else {
197                if (t1 < t3) {
198                    return -1;
199                }
200                else if (t1 > t3) {
201                    return 1;
202                }
203                else {
204                    return 0;
205                }
206            }
207        }
208    }
209
210    /**
211     * Returns a hash code for this object instance.  The approach described by
212     * Joshua Bloch in "Effective Java" has been used here - see:
213     * <p>
214     * <code>http://developer.java.sun.com/
215     * developer/Books/effectivejava/Chapter3.pdf</code>
216     *
217     * @return A hash code.
218     */
219    @Override
220    public int hashCode() {
221        int result = 17;
222        result = 37 * result + (int) this.start;
223        result = 37 * result + (int) this.end;
224        return result;
225    }
226
227}