    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     * SimpleTimePeriod.java
    029     * ---------------------
    030     * (C) Copyright 2002-2007, 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     *
    044     */
    045
    046    package org.jfree.data.time;
    047
    048    import java.io.Serializable;
    049    import java.util.Date;
    050
    051    /**
    052     * An arbitrary period of time, measured to millisecond precision using
    053     * <code>java.util.Date</code>.
    054     * <p>
    055     * This class is intentionally immutable (that is, once constructed, you cannot
    056     * alter the start and end attributes).
    057     */
    058    public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable {
    059
    060        /** For serialization. */
    061        private static final long serialVersionUID = 8684672361131829554L;
    062
    063        /** The start date/time. */
    064        private Date start;
    065
    066        /** The end date/time. */
    067        private Date end;
    068
    069        /**
    070         * Creates a new time allocation.
    071         *
    072         * @param start  the start date/time in milliseconds.
    073         * @param end  the end date/time in milliseconds.
    074         */
    075        public SimpleTimePeriod(long start, long end) {
    076            this(new Date(start), new Date(end));
    077        }
    078
    079        /**
    080         * Creates a new time allocation.
    081         *
    082         * @param start  the start date/time (<code>null</code> not permitted).
    083         * @param end  the end date/time (<code>null</code> not permitted).
    084         */
    085        public SimpleTimePeriod(Date start, Date end) {
    086            if (start.getTime() > end.getTime()) {
    087                throw new IllegalArgumentException("Requires end >= start.");
    088            }
    089            this.start = start;
    090            this.end = end;
    091        }
    092
    093        /**
    094         * Returns the start date/time.
    095         *
    096         * @return The start date/time (never <code>null</code>).
    097         */
    098        public Date getStart() {
    099            return this.start;
    100        }
    101
    102        /**
    103         * Returns the end date/time.
    104         *
    105         * @return The end date/time (never <code>null</code>).
    106         */
    107        public Date getEnd() {
    108            return this.end;
    109        }
    110
    111        /**
    112         * Tests this time period instance for equality with an arbitrary object.
    113         * The object is considered equal if it is an instance of {@link TimePeriod}
    114         * and it has the same start and end dates.
    115         *
    116         * @param obj  the other object (<code>null</code> permitted).
    117         *
    118         * @return A boolean.
    119         */
    120        public boolean equals(Object obj) {
    121            if (obj == this) {
    122                return true;
    123            }
    124            if (!(obj instanceof TimePeriod)) {
    125                return false;
    126            }
    127            TimePeriod that = (TimePeriod) obj;
    128            if (!this.start.equals(that.getStart())) {
    129                return false;
    130            }
    131            if (!this.end.equals(that.getEnd())) {
    132                return false;
    133            }
    134            return true;
    135        }
    136
    137        /**
    138         * Returns an integer that indicates the relative ordering of two
    139         * time periods.
    140         *
    141         * @param obj  the object (<code>null</code> not permitted).
    142         *
    143         * @return An integer.
    144         *
    145         * @throws ClassCastException if <code>obj</code> is not an instance of
    146         *                            {@link TimePeriod}.
    147         */
    148        public int compareTo(Object obj) {
    149            TimePeriod that = (TimePeriod) obj;
    150            long t0 = getStart().getTime();
    151            long t1 = getEnd().getTime();
    152            long m0 = t0 + (t1 - t0) / 2L;
    153            long t2 = that.getStart().getTime();
    154            long t3 = that.getEnd().getTime();
    155            long m1 = t2 + (t3 - t2) / 2L;
    156            if (m0 < m1) {
    157                return -1;
    158            }
    159            else if (m0 > m1) {
    160                return 1;
    161            }
    162            else {
    163                if (t0 < t2) {
    164                    return -1;
    165                }
    166                else if (t0 > t2) {
    167                    return 1;
    168                }
    169                else {
    170                    if (t1 < t3) {
    171                        return -1;
    172                    }
    173                    else if (t1 > t3) {
    174                        return 1;
    175                    }
    176                    else {
    177                        return 0;
    178                    }
    179                }
    180            }
    181        }
    182
    183        /**
    184         * Returns a hash code for this object instance.  The approach described by
    185         * Joshua Bloch in "Effective Java" has been used here - see:
    186         * <p>
    187         * <code>http://developer.java.sun.com/
    188         * developer/Books/effectivejava/Chapter3.pdf</code>
    189         *
    190         * @return A hash code.
    191         */
    192        public int hashCode() {
    193            int result = 17;
    194            result = 37 * result + this.start.hashCode();
    195            result = 37 * result + this.end.hashCode();
    196            return result;
    197        }
    198
    199    }