    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2006, 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-2006 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.2 2006/10/06 14:00:13 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     * ------------- JFREECHART 1.0.x ---------------------------------------------
    047     * 06-Oct-2006 : Added peg() method (DG);
    048     *
    049     */
    050
    051    package org.jfree.data.time;
    052
    053    import java.io.Serializable;
    054    import java.util.Calendar;
    055    import java.util.Date;
    056
    057    /**
    058     * Wrapper for a <code>java.util.Date</code> object that allows it to be used
    059     * as a {@link RegularTimePeriod}.  This class is immutable, which is a
    060     * requirement for all {@link RegularTimePeriod} subclasses.
    061     */
    062    public class FixedMillisecond extends RegularTimePeriod
    063                                  implements Serializable {
    064
    065        /** For serialization. */
    066        private static final long serialVersionUID = 7867521484545646931L;
    067
    068        /** The millisecond. */
    069        private Date time;
    070
    071        /**
    072         * Constructs a millisecond based on the current system time.
    073         */
    074        public FixedMillisecond() {
    075            this(new Date());
    076        }
    077
    078        /**
    079         * Constructs a millisecond.
    080         *
    081         * @param millisecond  the millisecond (same encoding as java.util.Date).
    082         */
    083        public FixedMillisecond(long millisecond) {
    084            this(new Date(millisecond));
    085        }
    086
    087        /**
    088         * Constructs a millisecond.
    089         *
    090         * @param time  the time.
    091         */
    092        public FixedMillisecond(Date time) {
    093            this.time = time;
    094        }
    095
    096        /**
    097         * Returns the date/time.
    098         *
    099         * @return The date/time.
    100         */
    101        public Date getTime() {
    102            return this.time;
    103        }
    104
    105        /**
    106         * This method is overridden to do nothing.
    107         *
    108         * @param calendar  ignored
    109         *
    110         * @since 1.0.3
    111         */
    112        public void peg(Calendar calendar) {
    113            // nothing to do
    114        }
    115
    116        /**
    117         * Returns the millisecond preceding this one.
    118         *
    119         * @return The millisecond preceding this one.
    120         */
    121        public RegularTimePeriod previous() {
    122            RegularTimePeriod result = null;
    123            long t = this.time.getTime();
    124            if (t != Long.MIN_VALUE) {
    125                result = new FixedMillisecond(t - 1);
    126            }
    127            return result;
    128        }
    129
    130        /**
    131         * Returns the millisecond following this one.
    132         *
    133         * @return The millisecond following this one.
    134         */
    135        public RegularTimePeriod next() {
    136            RegularTimePeriod result = null;
    137            long t = this.time.getTime();
    138            if (t != Long.MAX_VALUE) {
    139                result = new FixedMillisecond(t + 1);
    140            }
    141            return result;
    142        }
    143
    144        /**
    145         * Tests the equality of this object against an arbitrary Object.
    146         *
    147         * @param object  the object to compare
    148         *
    149         * @return A boolean.
    150         */
    151        public boolean equals(Object object) {
    152            if (object instanceof FixedMillisecond) {
    153                FixedMillisecond m = (FixedMillisecond) object;
    154                return this.time.equals(m.getTime());
    155            }
    156            else {
    157                return false;
    158            }
    159
    160        }
    161
    162        /**
    163         * Returns a hash code for this object instance.
    164         *
    165         * @return A hash code.
    166         */
    167        public int hashCode() {
    168            return this.time.hashCode();
    169        }
    170
    171        /**
    172         * Returns an integer indicating the order of this Millisecond object
    173         * relative to the specified
    174         * object: negative == before, zero == same, positive == after.
    175         *
    176         * @param o1    the object to compare.
    177         *
    178         * @return negative == before, zero == same, positive == after.
    179         */
    180        public int compareTo(Object o1) {
    181
    182            int result;
    183            long difference;
    184
    185            // CASE 1 : Comparing to another Second object
    186            // -------------------------------------------
    187            if (o1 instanceof FixedMillisecond) {
    188                FixedMillisecond t1 = (FixedMillisecond) o1;
    189                difference = this.time.getTime() - t1.time.getTime();
    190                if (difference > 0) {
    191                    result = 1;
    192                }
    193                else {
    194                    if (difference < 0) {
    195                       result = -1;
    196                    }
    197                    else {
    198                        result = 0;
    199                    }
    200                }
    201            }
    202
    203            // CASE 2 : Comparing to another TimePeriod object
    204            // -----------------------------------------------
    205            else if (o1 instanceof RegularTimePeriod) {
    206                // more difficult case - evaluate later...
    207                result = 0;
    208            }
    209
    210            // CASE 3 : Comparing to a non-TimePeriod object
    211            // ---------------------------------------------
    212            else {
    213                // consider time periods to be ordered after general objects
    214                result = 1;
    215            }
    216
    217            return result;
    218
    219        }
    220
    221        /**
    222         * Returns the first millisecond of the time period.
    223         *
    224         * @return The first millisecond of the time period.
    225         */
    226        public long getFirstMillisecond() {
    227            return this.time.getTime();
    228        }
    229
    230
    231        /**
    232         * Returns the first millisecond of the time period.
    233         *
    234         * @param calendar  the calendar.
    235         *
    236         * @return The first millisecond of the time period.
    237         */
    238        public long getFirstMillisecond(Calendar calendar) {
    239            return this.time.getTime();
    240        }
    241
    242        /**
    243         * Returns the last millisecond of the time period.
    244         *
    245         * @return The last millisecond of the time period.
    246         */
    247        public long getLastMillisecond() {
    248            return this.time.getTime();
    249        }
    250
    251        /**
    252         * Returns the last millisecond of the time period.
    253         *
    254         * @param calendar  the calendar.
    255         *
    256         * @return The last millisecond of the time period.
    257         */
    258        public long getLastMillisecond(Calendar calendar) {
    259            return this.time.getTime();
    260        }
    261
    262        /**
    263         * Returns the millisecond closest to the middle of the time period.
    264         *
    265         * @return The millisecond closest to the middle of the time period.
    266         */
    267        public long getMiddleMillisecond() {
    268            return this.time.getTime();
    269        }
    270
    271        /**
    272         * Returns the millisecond closest to the middle of the time period.
    273         *
    274         * @param calendar  the calendar.
    275         *
    276         * @return The millisecond closest to the middle of the time period.
    277         */
    278        public long getMiddleMillisecond(Calendar calendar) {
    279            return this.time.getTime();
    280        }
    281
    282        /**
    283         * Returns a serial index number for the millisecond.
    284         *
    285         * @return The serial index number.
    286         */
    287        public long getSerialIndex() {
    288            return this.time.getTime();
    289        }
    290
    291    }