    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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
    047    package org.jfree.data.time;
    048
    049    import java.io.Serializable;
    050    import 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     */
    059    public 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        public Date getStart() {
    100            return new Date(this.start);
    101        }
    102
    103        /**
    104         * Returns the start date/time in milliseconds.
    105         *
    106         * @return The start.
    107         *
    108         * @since 1.0.10.
    109         */
    110        public long getStartMillis() {
    111            return this.start;
    112        }
    113
    114        /**
    115         * Returns the end date/time.
    116         *
    117         * @return The end date/time (never <code>null</code>).
    118         */
    119        public Date getEnd() {
    120            return new Date(this.end);
    121        }
    122
    123        /**
    124         * Returns the end date/time in milliseconds.
    125         *
    126         * @return The end.
    127         *
    128         * @since 1.0.10.
    129         */
    130        public long getEndMillis() {
    131            return this.end;
    132        }
    133
    134        /**
    135         * Tests this time period instance for equality with an arbitrary object.
    136         * The object is considered equal if it is an instance of {@link TimePeriod}
    137         * and it has the same start and end dates.
    138         *
    139         * @param obj  the other object (<code>null</code> permitted).
    140         *
    141         * @return A boolean.
    142         */
    143        public boolean equals(Object obj) {
    144            if (obj == this) {
    145                return true;
    146            }
    147            if (!(obj instanceof TimePeriod)) {
    148                return false;
    149            }
    150            TimePeriod that = (TimePeriod) obj;
    151            if (!this.getStart().equals(that.getStart())) {
    152                return false;
    153            }
    154            if (!this.getEnd().equals(that.getEnd())) {
    155                return false;
    156            }
    157            return true;
    158        }
    159
    160        /**
    161         * Returns an integer that indicates the relative ordering of two
    162         * time periods.
    163         *
    164         * @param obj  the object (<code>null</code> not permitted).
    165         *
    166         * @return An integer.
    167         *
    168         * @throws ClassCastException if <code>obj</code> is not an instance of
    169         *                            {@link TimePeriod}.
    170         */
    171        public int compareTo(Object obj) {
    172            TimePeriod that = (TimePeriod) obj;
    173            long t0 = getStart().getTime();
    174            long t1 = getEnd().getTime();
    175            long m0 = t0 + (t1 - t0) / 2L;
    176            long t2 = that.getStart().getTime();
    177            long t3 = that.getEnd().getTime();
    178            long m1 = t2 + (t3 - t2) / 2L;
    179            if (m0 < m1) {
    180                return -1;
    181            }
    182            else if (m0 > m1) {
    183                return 1;
    184            }
    185            else {
    186                if (t0 < t2) {
    187                    return -1;
    188                }
    189                else if (t0 > t2) {
    190                    return 1;
    191                }
    192                else {
    193                    if (t1 < t3) {
    194                        return -1;
    195                    }
    196                    else if (t1 > t3) {
    197                        return 1;
    198                    }
    199                    else {
    200                        return 0;
    201                    }
    202                }
    203            }
    204        }
    205
    206        /**
    207         * Returns a hash code for this object instance.  The approach described by
    208         * Joshua Bloch in "Effective Java" has been used here - see:
    209         * <p>
    210         * <code>http://developer.java.sun.com/
    211         * developer/Books/effectivejava/Chapter3.pdf</code>
    212         *
    213         * @return A hash code.
    214         */
    215        public int hashCode() {
    216            int result = 17;
    217            result = 37 * result + (int) this.start;
    218            result = 37 * result + (int) this.end;
    219            return result;
    220        }
    221
    222    }