    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2008, 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     * DateRange.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):   Bill Kelemen;
    034     *
    035     * Changes
    036     * -------
    037     * 22-Apr-2002 : Version 1 based on code by Bill Kelemen (DG);
    038     * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    039     * 23-Sep-2003 : Minor Javadoc update (DG);
    040     * 28-May-2008 : Fixed problem with immutability (DG);
    041     *
    042     */
    043
    044    package org.jfree.data.time;
    045
    046    import java.io.Serializable;
    047    import java.text.DateFormat;
    048    import java.util.Date;
    049
    050    import org.jfree.data.Range;
    051
    052    /**
    053     * A range specified in terms of two <code>java.util.Date</code> objects.
    054     * Instances of this class are immutable.
    055     */
    056    public class DateRange extends Range implements Serializable {
    057
    058        /** For serialization. */
    059        private static final long serialVersionUID = -4705682568375418157L;
    060
    061        /** The lower bound for the range. */
    062        private long lowerDate;
    063
    064        /** The upper bound for the range. */
    065        private long upperDate;
    066
    067        /**
    068         * Default constructor.
    069         */
    070        public DateRange() {
    071            this(new Date(0), new Date(1));
    072        }
    073
    074        /**
    075         * Constructs a new range.
    076         *
    077         * @param lower  the lower bound (<code>null</code> not permitted).
    078         * @param upper  the upper bound (<code>null</code> not permitted).
    079         */
    080        public DateRange(Date lower, Date upper) {
    081            super(lower.getTime(), upper.getTime());
    082            this.lowerDate = lower.getTime();
    083            this.upperDate = upper.getTime();
    084        }
    085
    086        /**
    087         * Constructs a new range using two values that will be interpreted as
    088         * "milliseconds since midnight GMT, 1-Jan-1970".
    089         *
    090         * @param lower  the lower (oldest) date.
    091         * @param upper  the upper (most recent) date.
    092         */
    093        public DateRange(double lower, double upper) {
    094            super(lower, upper);
    095            this.lowerDate = (long) lower;
    096            this.upperDate = (long) upper;
    097        }
    098
    099        /**
    100         * Constructs a new range that is based on another {@link Range}.  The
    101         * other range does not have to be a {@link DateRange}.  If it is not, the
    102         * upper and lower bounds are evaluated as milliseconds since midnight
    103         * GMT, 1-Jan-1970.
    104         *
    105         * @param other  the other range (<code>null</code> not permitted).
    106         */
    107        public DateRange(Range other) {
    108            this(other.getLowerBound(), other.getUpperBound());
    109        }
    110
    111        /**
    112         * Returns the lower (earlier) date for the range.
    113         *
    114         * @return The lower date for the range.
    115         */
    116        public Date getLowerDate() {
    117            return new Date(this.lowerDate);
    118        }
    119
    120        /**
    121         * Returns the upper (later) date for the range.
    122         *
    123         * @return The upper date for the range.
    124         */
    125        public Date getUpperDate() {
    126            return new Date(this.upperDate);
    127        }
    128
    129        /**
    130         * Returns a string representing the date range (useful for debugging).
    131         *
    132         * @return A string representing the date range.
    133         */
    134        public String toString() {
    135            DateFormat df = DateFormat.getDateTimeInstance();
    136            return "[" + df.format(getLowerDate()) + " --> "
    137                + df.format(getUpperDate()) + "]";
    138        }
    139
    140    }