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